Serial Communication Project

Project: Communicating between Arduino and Processing

Process:

  1. It was hard to come up a project idea when you know a little about Arduino and Processing. I got inspired from a “handshaking” video in Instagram that someone made it. In Arduino, the “handshaking” method is called call-and-response method which means send multiple variables using from Arduino to processing, and processing doesn’t response until one of them call the program.
Call-and -response circuit

2. Once setup Arduino and processing. My idea is to use FSR sensors control a moving circle to move left and right, up and down to get through all objects on the screen. Basically it’s a game. However, when the circle overlapped with anther objects on the screen, they both will turn red.

3. The challenge part was to write code for using two sensors (FSR)instead of four sensors to control left and right, up and down. I tried by using a different pressure on sensors, but they didn’t stable enough. So, it was better to use four sensors to control different directions.

Experimental photos

3. Final project

4. Arduino + Processing code

/*
Serial Call and Response
Language: Wiring/Arduino
This program sends an ASCII A (byte of value 65) on startup
and repeats that until it gets some data in.
Then it waits for a byte in the serial port, and
sends three sensor values whenever it gets a byte in.
Thanks to Greg Shakar and Scott Fitzgerald for the improvements
The circuit:
* potentiometers attached to analog inputs 0 and 1
* pushbutton attached to digital I/O 2
Created 26 Sept. 2005
by Tom Igoe
modified 24 April 2012
by Tom Igoe and Scott Fitzgerald
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/SerialCallResponse
*/
int firstSensor = 0; // first analog sensor
int secondSensor = 0; // second analog sensor
int thirdSensor = 0; // digital sensor
int inByte = 0; // incoming serial byte
void setup() {
// start serial port at 9600 bps:
Serial.begin(9600);
/*while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}*/
while (Serial.available() <= 0) {
Serial.print(‘A’); // send a capital A
delay(300);
}
pinMode(2, INPUT); // digital sensor is on digital pin 2
//establishContact(); // send a byte to establish contact until receiver responds
}
void loop() {
// if we get a valid byte, read analog ins:
if (Serial.available() > 0) {
// get incoming byte:
inByte = Serial.read();
// read first analog input, divide by 4 to make the range 0–255:
firstSensor = analogRead(A0) ;
// delay 10ms to let the ADC recover:
delay(10);
// read second analog input, divide by 4 to make the range 0–255:
secondSensor = analogRead(1) ;
// read switch, map it to 0 or 255L
thirdSensor = map(digitalRead(2), 0, 1, 0, 255);
// send sensor values:
Serial.write(firstSensor);
Serial.write(secondSensor);
Serial.write(thirdSensor);
}
}
//processing sketch to run with this example
import processing.serial.*;
import java.util.*;
int bgcolor; // Background color
int fgcolor; // Fill color
Serial myNewPort; // The serial port
int[] serialInArray = new int[3]; // Where we’ll put what we receive
int serialCount = 0; // A count of how many bytes we receive
int xpos, ypos; // Starting position of the ball
boolean firstContact = false; // Whether we’ve heard from the microcontroller
int diam=40;
int a=300;
int b=300;
//int Xspacing;
//int numObject=0;
List<PVector> balls;
void setup() {
size(1000, 800); // Stage size
//noStroke(); // No border on the next thing drawn
// Set the starting position of the ball (middle of the stage)
xpos = 250;
ypos = height — 10;
// Print a list of the serial ports for debugging purposes
// if using Processing 2.1 or later, use Serial.printArray()
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my FTDI adaptor, so I open Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you’re using.
String portName = Serial.list()[1];
myNewPort = new Serial(this, portName, 9600);
balls = new ArrayList<PVector>();
initPos();
}
void initPos() {
for (int i = 0; i< 10; i++) {
for (int j = 0; j< 10; j++) {
////make random balls
balls.add(new PVector(i * 100 + random(50), j * 100 + random(50)));
}
}
}
void draw() {
background(bgcolor);
for (int i = 0; i< balls.size (); i++) {
PVector p = balls.get(i);
//Make other balls turn red when the moving object hits on it.
if (Math.sqrt((p.x-xpos)*(p.x-xpos)+(p.y-ypos)*(p.y-ypos)) <=diam/2+10) {

pushMatrix();
textSize(32);
fill(2232, 250, 28);
text(“Try play again!”, width/2–100, height/2);
popMatrix();
fill(255, 88, 66);
} else {
fill(fgcolor);
}
ellipse(p.x, p.y, diam, diam);
}
//moving-object
fill(fgcolor);
ellipse(xpos, ypos, 20, 20);
if (ypos<0) {
textSize(32);
fill(255, 46, 50);
text(“Wow, congratulations!”, width/2–150, height/2);
}
if (ypos>height) {
textSize(32);
fill(2232, 250, 28);
text(“Try play again!”, width/2–100, height/2);
}
}
//call-and-reponse
void serialEvent(Serial myPort) {
// read a byte from the serial port:
int inByte = myPort.read();
// if this is the first byte received, and it’s an A,
// clear the serial buffer and note that you’ve
// had first contact from the microcontroller.
// Otherwise, add the incoming byte to the array:
if (firstContact == false) {
if (inByte == ‘A’) {
myPort.clear(); // clear the serial port buffer
firstContact = true; // you’ve had first contact from the microcontroller
myPort.write(‘A’); // ask for more
}
} else {
// Add the latest byte from the serial port to array:
serialInArray[serialCount] = inByte;
serialCount++;
// If we have 3 bytes:
if (serialCount > 2 ) {
//move-left
if (15<serialInArray[0] && serialInArray[0] < 100 ) {
xpos = xpos + serialInArray[0]/50;
}
//move-right
if (200<serialInArray[0]) {
xpos = xpos -serialInArray[0]/50;
}
println(serialInArray[0]);
//move-down
if (15<serialInArray[1] && serialInArray[1] < 100 ) {
ypos = ypos + serialInArray[1]/50;
}
//move-up
if (200<serialInArray[1]) {
ypos = ypos -serialInArray[1]/50;
}
//xpos = serialInArray[0];
// ypos = serialInArray[1];
fgcolor = serialInArray[2];
// print the values (for debugging purposes only):
//println(xpos + “\t” + ypos + “\t” + fgcolor);
// Send a capital A to request new sensor readings:
myPort.write(‘A’);
// Reset serialCount:
serialCount = 0;
}
}
}