Recitation 10: Workshops Documentation by Eleanor Wade

Serial Communication Workshop:

Using a button on the Arduino as a sensor to control the sketch on processing.  We used multiple value Arduino to Processing sample code.  

This recitation was very helpful in learning some necessary techniques in serial communication, with sensors that are beyond that of a potentiometer.  This will definitely be useful when moving forward with my final project as I will  definitely be using a color sensor to translate colored tags into specific factual information and images on the screen.  In this way, it was particularly beneficial to me to be able review the information that we previously had learned regarding arduino to processing.  

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


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


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

  // use the values like this!
  // sensorValues[0] : pot 0 - 1023
  // sensorValues[1] : button switch 0, 1

  // add your code
  float posX = map(sensorValues[0], 0, 1023, 0, 500); 
 int size;
 if (sensorValue[1] == 0) {
   size = 50;
 } else { 
   size = 200;
 }
 
  
  
ellipse (300, 300, size, size);
  //
}



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

One value Processing to Arduino Serial Communication: 

Arduino Code:

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

#include <Servo.h>
Servo myservo;
int val;

char valueFromProcessing;
//int ledPin = 13;

void setup() {
Serial.begin(9600);
myservo.attach(9);
}

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

val = valueFromProcessing;
val = map(val, 0, 500, 0, 180);
myservo.write(val);
delay(15);

// if (valueFromProcessing == ‘H’) {
// digitalWrite(ledPin, HIGH);
// } else if (valueFromProcessing == ‘L’) {
// digitalWrite(ledPin, LOW);
// } else {
// something esle
// }

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

In using the map function to map the values of canvas size, we specifically changed the code to this:

val = map(val, o, 500, 0, 180);

Leave a Reply