Recitation 8: Serial Communication by Jiahui Zhang

Overview:

This week we review how to use serial communication to communicate between the Processing and the Arduino. We have two exercises in class to upload the data from Arduino to Processing to draw lines by the potentiometers and from Processing to Arduino to ring a buzzer. The code provided contains something I do not understand quite well, but the instruction of where to add the code is really clear. Overall, it is not hard and quite funny.

Exercise 1: Make a Processing Etch A Sketch

For this exercise, we used the Arduino to send two values from the potentiometers to the Processing by serial communication. I first built up the circuit and then downloaded the codes provided by the class (which are under the folder: multi variables, Arduino to Processing). I change a little bit and add a function to draw the circuit.

Here are the changes applied to all codes used in the Processing:

int NUM_OF_VALUES = 2;

myPort = new Serial(this, Serial.list()[ 2 ], 9600);

Here is the function I added into the draw loop:

ellipse(sensorValues[0],sensorValues[1],100,100);

This is how it looks like.

After finishing the ellipse one, I start trying to draw as the video does. So I change the functions inside of the draw loop, which using lines connecting the previous point and the current point to make the drawing continuous. To make this fulfills, I add two more variables to store the value of previous sensor values.

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

float[] sensorValues;      /** this array stores values from Arduino **/
float x;
float y;

void setup() {
  size(800, 800);
  background(255);
  setupSerial();
  x=0;
  y=0;
}


void draw() {
  updateSerial();
  line(sensorValues[0],sensorValues[1],x,y);
  x=sensorValues[0];
  y=sensorValues[1];

}

Exercise 2: Make a musical instrument with Arduino

The codes need to be changed into the codes provided under the folder: multi variables, Processing to Arduino. The circuit remains unchanged, but only a buzzed needs to be added. Meanwhile, there are a few changes inside the code as follows:

In Processing:

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++) {
    if(mousePressed){
    values[0] = mouseX;  /** Feel free to change this!! **/
    values[1] = mouseY;
    }
  }

In Arduino:

void loop() {
getSerialData();
tone(9,values[0],1000/values[1]);

The thing works, but it sounds quite awful:

Leave a Reply