Recitation 8: Serial Communication——Yixuan Liu(Recitation Instructor: Younghyun Chung, Eric Parren)

In this week’s recitation, I had two exercise practice. The first one was to send instruction from the Audrino to the Processing. By sending certain instructions, there will be outcomes appearing through the Processing. I chose to connect the potentiometer which is able to send instructions, then I can use it to draw circles on the screen.  And the second is the opposite process which is from Processing to the Audrino. 

Here is the basic schematic for each circuit. Actually, the basic theory is the same. From this diagram, we just need to replace the potentiometer to the buzzer. Then it becomes the second circuit.

And here is the video for the first exercise :

recitaton10

I can move the circle from twisting the potentiometer.

Here is the code for processing:

import processing.serial.*;

String myString = null;
Serial myPort;
int fromArduino;

int NUM_OF_VALUES = 2; /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/
int[] sensorValues; /** this array stores values from Arduino **/

void setup() {
size(500, 500);
printArray(Serial.list());
myPort = new Serial (this, Serial. list() [0],9600);
setupSerial();
}

void draw() {
updateSerial();
while(myPort. available()>0){
fromArduino = myPort.read();
}
background(fromArduino);
ellipse (map(fromArduino,0,225,0,width),height/3,fromArduino,fromArduino);
printArray(sensorValues);

}

void setupSerial() {
printArray(Serial.list());

myPort.clear();

myString = myPort.readStringUntil( 10 ); // 10 = ‘\n’ Linefeed in ASCII
myString = null;

sensorValues = new int[2];
}

void updateSerial() {
while (myPort.available() > 0) {
myString = myPort.readStringUntil( 10 ); // 10 = ‘\n’ Linefeed in ASCII
if (myString != null) {
String[] serialInArray = split(trim(myString), “,”);
if (serialInArray.length == 2) {
for (int i=0; i<serialInArray.length; i++) {
sensorValues[i] = int(serialInArray[i]);
}
}
}
}
}

And for the second exercise, I chose to use the keyPress code to achieve the instruction. If people press the “UP,” “DOWN,” and “RIGHT,” the buzzer will have a different type of sounds.

Here is the video:

And here is the code in the processing:

import processing.serial.*;

int NUM_OF_VALUES = 2; /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/

Serial myPort;
String myString;

// This is the array of values you might want to send to Arduino.
int values[] = new int[NUM_OF_VALUES];

void setup() {
size(500, 500);
background(0);

printArray(Serial.list());
myPort = new Serial(this, Serial.list()[ 3 ], 9600);
myString = myPort.readStringUntil( 10 ); // 10 = ‘\n’ Linefeed in ASCII
myString = null;
}

void draw() {
background(0);

// changes the values
for (int i=0; i<values.length; i++) {
values[i] = i; /** Feel free to change this!! **/
if (keyPressed == true) {
if (keyCode == UP) {
values[0] = 1;
values[1] = 1;
}
if (keyCode == DOWN) {
values[0] = 1;
values[1] = 2;
}
if (keyCode == RIGHT) {
values[0] = 1;
values[1] = 3;
}
} else {
values[0] = 0;
}
}

sendSerialData();

echoSerialData(200);
}

void sendSerialData() {
String data = “”;
for (int i=0; i<values.length; i++) {
data += values[i];
//if i is less than the index number of the last element in the values array
if (i < values.length-1) {
data += “,”; // add splitter character “,” between each values element
}
//if it is the last element in the values array
else {
data += “n”; // add the end of data character “n”
}
}
//write to Arduino
myPort.write(data);
}

void echoSerialData(int frequency) {
if (frameCount % frequency == 0) myPort.write(‘e’);

String incomingBytes = “”;
while (myPort.available() > 0) {
//add on all the characters received from the Arduino to the incomingBytes string
incomingBytes += char(myPort.read());
}
//print what Arduino sent back to Processing
print( incomingBytes );
}

Since for this recitation, we got the sample code for most of our exercise, hence, when I saw the code, I first felt confused and did not know how to start. Fortunately, I figured out at the end. This is also the first time we learn how to build the connection between the Audrino and processing which means we can finally have both visual and physical tools for our study. 

Leave a Reply