Recitation #8 Serial Communication, Megan See

Exercise 1: Make a Processing Etch A Sketch



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 previousX;
int previousY;


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


void draw() {
  updateSerial();
  printArray(sensorValues);
  color(255);
  stroke(255);
  strokeWeight(10);
  //line( sensorValues[0], sensorValues[1], 50,50);
  line( previousX, previousY, sensorValues[0], sensorValues[1]);
  previousX = sensorValues[0];
  previousY = 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 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]);
        }
      }
    }
  }
}

When making the Etch-a-sketch I spent the entire recitation period, a lot more time on it than anticipated. After adding the map function it got a lot easier, but I spent so long trying to figure out what was wrong. I realized at the end that the commas were what was keeping everything from running the right way. I think the user is almost completely in control of the etch-a-sketch which gives it a high level of interactivity. It is engaging and the output depends solely on the user. 

Exercise 2: Make a musical instrument with Arduino

This one was a bit harder for me even though I didn’t think it would be. The start was very similar to the first exercise and used tones like my midterm project. I think that it is also pretty interactive, but I think that the etch a sketch is better because the user can make something that they made at the end. This does too if you’re thinking about the other senses. It brings up the ides of combining the two to use numerous senses to make the best experience for the user. 

Leave a Reply