Recitation 8: Serial Communication – Kevin Nader

Introduction:

In this recitation we were tasked with making both an Etch A Sketch and a musical instrument. The reason we were tasked with making these devices was in order to familiarize us with serial communication. We accomplished this by simultaneously utilizing processing and arduino. 

Process:

Etch-A-Sketch in action:

Etch A Sketch

Etch-A-Sketch Code:

Writing the code for this was rather easy because it was very similar to what we did in class the day before recitation. The only modifications required were changing the ellipses to lines.

// 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 posx2;
int posy2;


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();
}


void draw() {
  updateSerial();
  printArray(sensorValues);

  stroke(255);
  line(sensorValues[0], sensorValues[1], posx2, posy2);
  
  posx2 = sensorValues[0] ;
  posy2 = sensorValues[1] ;

}



void setupSerial() {
  printArray(Serial.list());
  myPort = new Serial(this, Serial.list()[19], 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]);
        }
      }
    }
  }
}

/*
  AnalogReadSerial

  Reads an analog input on pin 0, prints the result to the Serial Monitor.
  Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/AnalogReadSerial
*/

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue1 = analogRead(A0);
  int sensorValue2 = analogRead(A1);
  // print out the value you read:
  Serial.print(sensorValue1);
  Serial.print(",");
  Serial.print(sensorValue2);
  Serial.println();
  delay(1);        // delay in between reads for stability
}

Musical Instrument in action:

IMG_4879

Musical Instrument Code:

Coding this was an absolute nightmare. Not because I didn’t know what I was doing, but because I mismanaged my variables. It took help from Professor Marcela Godoy to smooth things out.

// IMA NYU Shanghai
// Interaction Lab
// This code sends one value from Processing to Arduino 

import processing.serial.*;

Serial myPort;
int valueFromArduino;

int High;
int Med;
int Low;

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

  printArray(Serial.list());
  // this prints out the list of all available serial ports on your computer.

  myPort = new Serial(this, Serial.list()[19], 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.
}


void draw() {
  // to send a value to the Arduino
  High = height;
  Med = 2*height/3;
  Low = height/3;
  if (mousePressed && mouseY > 0 && mouseY < Low) {
    myPort.write('L');
  } else if (mousePressed && mouseY > Low && mouseY < Med) {
    myPort.write('M');
  } else if (mousePressed && mouseY > Med && mouseY < High) {
    myPort.write('H');
  } else {
    myPort.write('N');
  }
  //if (mouseX > width/2) {
  //  myPort.write('H');
  //} else {
  //  myPort.write('L');
  //}
}

// IMA NYU Shanghai
// Interaction Lab
// This code receives one value from Processing to Arduino
int valueFromProcessing;

void setup() {
  Serial.begin(9600);
  pinMode(8, OUTPUT);

}
//
//void draw() {
//  if (mouseX > width / 2) {
//    myPort.write('H');
//  } else {
//    myPort.write('L');
//  }
//}

void loop() {
  // to receive a value from Processing
  while (Serial.available()) {
    valueFromProcessing = Serial.read();
  }
  analogWrite(8, valueFromProcessing);

  if (valueFromProcessing == 'H') {
    //digitalWrite(8, HIGH);
    tone(8, 3000);
  } else if (valueFromProcessing == 'N') {
    //    digitalWrite(8, LOW);
    noTone(8);
  }
  else if (valueFromProcessing == 'M') {
    //digitalWrite(8, HIGH);
    tone(8, 2000);
  }
  else if (valueFromProcessing == 'L') {
    //digitalWrite(8, HIGH);
    tone(8, 1000);
  } 
  // something esle



  // too fast communication might cause some latency in Processing
  // this delay resolves the issue.
  delay(100);
}

Reflection:

I found this recitation to be a lot of fun. Being able to recreate such a staple of my childhood was great. The lesson I learned from this recitation was that I need to stay organized when coding. Things got a little bit out of hand, and I suspect because I was working with two programing softwares simultaneously. Learning to stay organized while wiring hardware was important, but I argue that remaining organized while coding is even more important. 

Leave a Reply