Recitation 10: Serial Communication by Isabel Brack

Overview:

In this recitation we went to a workshop on mapping and then using serial communication to connect Arduino with Processing. The serial communication workshop leader had no major requirements for our recitation other performing serial communication, while using a mapping function. So following the instructor’s lead I mapped a potentiometer to the Y coordinate of a moving ellipse and the X coordinate was mouse X. Also, I used a button serial communication to change the color of the ellipse. I then used this serial communication in my final project code like the instructor suggested using buttons to switch the image faces.

This recitation mainly follows exactly what our instructor was doing. First we connected the circuit with the potentiometer and the button. Next we looked at the serial communication code for A to P and altered it to use one digital sensor and one analog. Finally we altered the processing code to map the potentiometer and to use a bullion to control the color of the ellipse which was most helpful as we want to use a button to control Processing in our final project.

Code for moving ellipse:

// 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 **/


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


void draw() {
  updateSerial();
  printArray(sensorValues);
   background(0);
  float posx= map (sensorValues[0],0,1023,0,255);
   ellipse(posx,mouseY,50,50);
   if (sensorValues[1]==1){
     fill(random(255));
   }

  // use the values like this!
  // sensorValues[0] 

  // add your code

  //
}



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

Code for Final:

This is a work in progress code for the final project.

Arduino:

// IMA NYU Shanghai
// Interaction Lab
// For sending multiple values from Arduino to Processing

void setup() {
Serial.begin(9600);
pinMode(9,INPUT);
}

void loop() {
int sensor1 = digitalRead(9);
int sensor2 = digitalRead(8);
int sensor3 = digitalRead(10);
int sensor4 = digitalRead(6);
int sensor5 = digitalRead(7);

// keep this format
Serial.print(sensor1);
Serial.print(“,”); // put comma between sensor values
Serial.print(sensor2);
Serial.print(“,”);
Serial.print(sensor3);
Serial.print(“,”);
Serial.print(sensor4);
Serial.print(“,”);
Serial.print(sensor5);
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);
}

Processing:

// 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 = 5;   /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/
int[] sensorValues;      /** this array stores values from Arduino **/

int maxImages = 2; // Total # of images
int imageIndex = 0; // Initial image to be displayed

 
// Declaring three arrays of images.
PImage[] a = new PImage[maxImages]; 
PImage[] b = new PImage[maxImages]; 
PImage[] c = new PImage[maxImages]; 
void setup() {

  setupSerial();
   size(240,150);
 
  // Puts  images into eacu array
  // add all images to data folder
  for (int i = 0; i < a.length; i ++ ) {
    a[i] = loadImage( "eyes" + i + ".jpeg" ); 
  }
  for (int i = 0; i < b.length; i ++ ) {
    b[i] = loadImage( "Unknown-15.jpeg"); 
  }
  for (int i = 0; i < c.length; i ++ ) {
    c[i] = loadImage( "Unknown-14.jpeg" ); 
  }

}


void draw() {
  updateSerial();
  printArray(sensorValues);
image(a[imageIndex],0,0);
image(b[imageIndex],0,height/3*1);
image(c[imageIndex],0,height/3*2);


imageIndex = constrain (imageIndex, 0,0);
imageIndex = constrain (imageIndex, 0, height/3*1);
imageIndex = constrain (imageIndex, 0, height/3*2);  

  // use the values like this!
  // sensorValues[0] 

  // add your code
if (sensorValues[0] == 1 || sensorValues[1]== 1 || sensorValues[2] ==1|| sensorValues[3] ==1|| sensorValues[4] ==1){
 //imageIndex += 1;
  imageIndex = int(random(a.length));
  imageIndex = int(random(b.length));
  imageIndex = int(random(c.length));
  sensorValues[0] = 0;
 sensorValues[1]= 0;
 sensorValues[2] =0; 
 sensorValues[3] = 0;
 sensorValues[4] = 0;
}

 

  // use the values like this!
  // sensorValues[0] 

  // add your code

  //
}



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

Leave a Reply