Recitation 10: Workshop by Eric Shen

In the first part of the recitation, we learned more about one of the most important functions in Arduino and Processing, which is map() function. After the detailed introduction and the examples provided by the fellows, I think that I have completely mastered this function. 

Later in the recitation, we chose one workshop among three workshops, and I chose the serial communication workshop in order to better apply it in my final project.  

In the workshop, we had two exercises, the first one is communication from Arduino to Processing, using a button and a potentiometer map( ) function was used in this exercise.

The code for 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 = 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() {
  background(0);
  updateSerial();
  printArray(sensorValues);

  // use the values like this!
  // sensorValues[0] 
float posX = map(sensorValues[0],0,1023,0,width);
int size ;
if(sensorValues[1] == 0){
  size=50;} else {
    size = 200;
  }
ellipse(posX,mouseY,size,size);

  // add your code

  //
}



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

The second exercise is to transfer data from Processing to servo motors in Arduino. And in order to make it more fun, we used the data from processing to make the fan of the servo motor to move back and forth. 

The code for Arduino:

// IMA NYU Shanghai
// Interaction Lab
// This code receives one value from Processing to Arduino
#include <Servo.h>
Servo myservo;
int val;

char valueFromProcessing;

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

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

}

val = valueFromProcessing; // reads the value of the potentiometer (value between 0 and 1023)
// val = map(val, 0, 500, 0, 180); // scale it to use it with the servo (value between 0 and 180)
// myservo.write(val); // sets the servo position according to the scaled value
//Serial.print(val);
// // too fast communication might cause some latency in Processing
// // this delay resolves the issue.
delay(10);
}

The code for Processing: 

// IMA NYU Shanghai
// Interaction Lab
// This code sends one value from Processing to Arduino 
import processing.serial.*;
Serial myPort;
int valueFromArduino;


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()[13], 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
  if (mousePressed) {
    myPort.write(mouseX);
  } else {
    myPort.write(0);
  }
 

Leave a Reply