Recitation 10 – Workshops – Kevin Nader

Introduction:

For this recitation I chose to work on my serial communication with Young. I chose this because I felt that my understanding of it was not up to par. This was particularly important considering how I intended to use serial communication for my final project. Young explained serial communication to those attending the workshop by walking us through the creation of two devices.

The Devices:

Coming up for a name for the first device is relatively difficult because it does not really have an intended purpose. It was created simply to teach us serial communication. Here is a clip of the device in action. 

The device utilized a potentiometer to move the ellipse along the x axis of the background, mouse placement was used to determine the ellipse position with respects to the y axis. If the button was pressed, it would cause the ellipse to grow.

These are two clips of the hardware in action:

This clip is showing that the button in charge of making the ellipse grow functions properly 

This clips shows the potentiometer responsible for moving the ellipse along the x axis is working

Arduino Code:

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


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

void loop() {
  int sensor1 = analogRead(A0);
  int sensor2 = digitalRead(8);

  // keep this format
  Serial.print(sensor1);
  Serial.print(",");  // put comma between sensor values
  Serial.print(sensor2);
  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 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);

  background (0);

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

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

      ellipse(posX, mouseY, size, size);
  }



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

Similar to the first device, the second device was relatively simple as well. This device was a processing sketch where, if clicked, would trigger a servo motor to move. Here is a clip of that device.

As you can see, the device works as intended.

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;


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

Processing Code: 

Unfortunately, due to issues with my computer’s storage, the processing code was lost.

Reflection:

Overall, this workshop was very beneficial to me. I’m glad I was given the opportunity to brush up on my serial communication skills. Hopefully I can take these skills with me into my final. 

Leave a Reply