For recitation 8, the focus was on serial communication where there were tasks to create Arduino to processing serial communication and processing to Arduino serial communication.
Exercise 1
For exercise 1, the task was to create an etch a sketch. This wasn’t too difficult as potentiometers sent serial communication to processing to draw lines. One potentiometer controlled the x of the processing and the other controlled the y coordinate. A video will be attached to show this learning.
Processing Code
// IMA NYU Shanghai // Interaction Lab // For receiving multiple values from Arduino to Processing /* * Based on the readStringUntil() example by Tom Igoe * https://processing.org/reference/libraries/serial/Serial_readStringUntil_.html */ import processing.serial.*; String myString = null; Serial myPort; float x, y;//current float m, n;//previous 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); background(0); setupSerial(); m = 0; n = 0; } void draw() { updateSerial(); printArray(sensorValues); float x = map(sensorValues[0], 0, 1023, 0, width); float y = map (sensorValues[1], 0, 1023, 0, height); fill(255); stroke(255); strokeWeight(5); line(m, n, x, y); //update previous m = x; n = y; //stroke(5); //frameRate(1); // line (0,0, x, y); //} // if (sensorValues[1]==1){ // background (0); // fill(255); // circle (50,50,50); //} // use the values like this! // sensorValues[0] // add your code // } void setupSerial() { printArray(Serial.list()); myPort = new Serial(this, Serial.list()[ 2 ], 9600); // WARNING! // You will definitely get an error here. // Change the PORT_INDEX to 0 and try running it again. // And then, check the list of the ports, // find the port "/dev/cu.usbmodem----" or "/dev/tty.usbmodem----" // and replace PORT_INDEX above with the index number of the port. myPort.clear(); // Throw out the first reading, // in case we started reading in the middle of a string from the sender. myString = myPort.readStringUntil( 10 ); // 10 = '\n' Linefeed in ASCII myString = null; sensorValues = new int[NUM_OF_VALUES]; } 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 == NUM_OF_VALUES) { for (int i=0; i<serialInArray.length; i++) { sensorValues[i] = int(serialInArray[i]); } } } } }
Arduino Code // IMA NYU Shanghai // Interaction Lab // For sending multiple values from Arduino to Processing void setup() { Serial.begin(9600); } void loop() { int potentiometer1 = analogRead(A0); int potentiometer2 = analogRead(A1); // keep this format Serial.print(potentiometer1); Serial.print(","); // put comma between sensor values Serial.print(potentiometer2); Serial.println(); // add linefeed after sending the last sensor value // too fast communication might cause some latency in Processing // this delay resolves the issue. delay(100); } Exercise 2 For exercise 2, I focused on making the buzzer's frequency react to the coordinates of the x-coordinate of the trackpad. This helped make it more focused and interactive where a video will be attached.
// IMA NYU Shanghai // Interaction Lab /** * This example is to send multiple values from Processing to Arduino. * You can find the arduino example file in the same folder which works with this Processing file. * Please note that the echoSerialData function asks Arduino to send the data saved in the values array * to check if it is receiving the correct bytes. **/ 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()[ 2 ], 9600); // check the list of the ports, // find the port "/dev/cu.usbmodem----" or "/dev/tty.usbmodem----" // and replace PORT_INDEX above with the index of the port myPort.clear(); // Throw out the first reading, // in case we started reading in the middle of a string from the sender. 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[0]= mouseX;// values[1] = mouseY; // sends the values to Arduino. sendSerialData(); // This causess the communication to become slow and unstable. // You might want to comment this out when everything is ready. // The parameter 200 is the frequency of echoing. // The higher this number, the slower the program will be // but the higher this number, the more stable it will be. echoSerialData(20); } 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) { //write character 'e' at the given frequency //to request Arduino to send back the values array 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 ); }
Arduino Code
// IMA NYU Shanghai // Interaction Lab /** This example is to send multiple values from Processing to Arduino. You can find the Processing example file in the same folder which works with this Arduino file. Please note that the echo case (when char c is 'e' in the getSerialData function below) checks if Arduino is receiving the correct bytes from the Processing sketch by sending the values array back to the Processing sketch. **/ #define NUM_OF_VALUES 3 /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/ /** DO NOT REMOVE THESE **/ int tempValue = 0; int valueIndex = 0; float x; float y; /* This is the array of values storing the data from Processing. */ int values[NUM_OF_VALUES]; void setup() { Serial.begin(9600); pinMode(8, OUTPUT); } void loop() { getSerialData(); x = map(values[0], 0, 500, 2000, 5000); y = map(values[1], 0, 500, 100, 200); tone(8, x, y); } //recieve serial data from Processing void getSerialData() { if (Serial.available()) { char c = Serial.read(); //switch - case checks the value of the variable in the switch function //in this case, the char c, then runs one of the cases that fit the value of the variable //for more information, visit the reference page: https://www.arduino.cc/en/Reference/SwitchCase switch (c) { //if the char c from Processing is a number between 0 and 9 case '0'...'9': //save the value of char c to tempValue //but simultaneously rearrange the existing values saved in tempValue //for the digits received through char c to remain coherent //if this does not make sense and would like to know more, send an email to me! tempValue = tempValue * 10 + c - '0'; break; //if the char c from Processing is a comma //indicating that the following values of char c is for the next element in the values array case ',': values[valueIndex] = tempValue; //reset tempValue value tempValue = 0; //increment valuesIndex by 1 valueIndex++; break; //if the char c from Processing is character 'n' //which signals that it is the end of data case 'n': //save the tempValue //this will b the last element in the values array values[valueIndex] = tempValue; //reset tempValue and valueIndex values //to clear out the values array for the next round of readings from Processing tempValue = 0; valueIndex = 0; break; //if the char c from Processing is character 'e' //it is signalling for the Arduino to send Processing the elements saved in the values array //this case is triggered and processed by the echoSerialData function in the Processing sketch case 'e': // to echo for (int i = 0; i < NUM_OF_VALUES; i++) { Serial.print(values[i]); if (i < NUM_OF_VALUES - 1) { Serial.print(','); } else { Serial.println(); } } break;
Additional Homework
The additional homework for this week revolved around using boolean true and boolean false to create the stars appearance and disappearance. I struggled at first to understand how boolean would be used as I made it flicker when I pressed the button, however after asking a fellow to assist me in understanding how the drawing works, I was able to figure it out to be boolean. A video will be attached as well as a photo of the circuit.
Processing Code :
// IMA NYU Shanghai // Interaction Lab // For receiving multiple values from Arduino to Processing /* * Based on the readStringUntil() example by Tom Igoe * https://processing.org/reference/libraries/serial/Serial_readStringUntil_.html */ import processing.serial.*; String myString = null; Serial myPort; int NUM_OF_VALUES = 2; /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/ int[] sensorValues; /** this array stores values from Arduino **/ int h=0; int k=0; boolean b = false; boolean c=false; void setup() { size(600, 600); background(0); setupSerial(); } void draw() { updateSerial(); printArray(sensorValues); background (0); if (sensorValues[0]==1 &&sensorValues[0]!=h) { b=!b; } if (sensorValues[1]==1 &&sensorValues[1]!=k) { c=!c; } if (b) { star(100, 100, 80, 100, 40); } if (c) { star(250, 300, 30, 70, 5); } h=sensorValues[0]; k=sensorValues[1]; } void setupSerial() { printArray(Serial.list()); myPort = new Serial(this, Serial.list()[2], 9600); // WARNING! // You will definitely get an error here. // Change the PORT_INDEX to 0 and try running it again. // And then, check the list of the ports, // find the port "/dev/cu.usbmodem----" or "/dev/tty.usbmodem----" // and replace PORT_INDEX above with the index number of the port. myPort.clear(); // Throw out the first reading, // in case we started reading in the middle of a string from the sender. myString = myPort.readStringUntil( 10 ); // 10 = '\n' Linefeed in ASCII myString = null; sensorValues = new int[NUM_OF_VALUES]; } void star(float x, float y, float radius1, float radius2, int npoints) { float angle = TWO_PI / npoints; float halfAngle = angle/2.0; beginShape(); for (float a = 0; a < TWO_PI; a += angle) { float sx = x + cos(a) * radius2; float sy = y + sin(a) * radius2; vertex(sx, sy); sx = x + cos(a+halfAngle) * radius1; sy = y + sin(a+halfAngle) * radius1; vertex(sx, sy); } endShape(CLOSE); } 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 == NUM_OF_VALUES) { for (int i=0; i<serialInArray.length; i++) { sensorValues[i] = int(serialInArray[i]); } } } } }
Arduino code:
// IMA NYU Shanghai // Interaction Lab // For sending multiple values from Arduino to Processing void setup() { Serial.begin(9600); } void loop() { int button1 = digitalRead(7); int button2 = digitalRead(8); // keep this format Serial.print(button1); Serial.print(","); // put comma between sensor values Serial.print(button2); Serial.println(); // add linefeed after sending the last sensor value // too fast communication might cause some latency in Processing // this delay resolves the issue. delay(100); }
Leave a Reply