Recitation 9: Media & Controls

Part I: Arduino to Processing

Video:IMG_3246

Circuit:

For this exercise, I chose to use potentiometer as the interaction service. I remember we did an exercise on the potentiometer as controller in our previous recitation session. So I used that source code and developed it. (So that is why the circuit diagram is the same as the previous recitation session). The potentiometer is adjusting the two “haoye” pictures moving on the x and y axis of the canvas. This was elaborated from the array lectures, I also used this image.

Arduino Code:

void setup() {
  Serial.begin(9600);
}

void loop() {
  // read the input on analog pin 0:
  int sensor1 = analogRead(A0);
  int sensor2 = analogRead(A1);
  // print out the value you read:
  Serial.print(sensor1);
  Serial.print(","); 
  Serial.print(sensor2);
  Serial.println();
  delay(100);        
}

 

Processing Code:

import processing.serial.*;

PImage photo;
PImage photo1;

int NUM_OF_VALUES_FROM_ARDUINO = 2;   /** YOU MUST CHANGE THIS ACCORDING TO YOUR PROJECT **/
int sensorValues[];      /** this array stores values from Arduino **/

String myString = null;
Serial myPort;


void setup() {
  size(1023, 1023);
  photo = loadImage("haoye.jpg");
  //background(0);
  setupSerial();
}

void draw() {
  getSerialData();
  printArray(sensorValues);
  background(0);
  image(photo,sensorValues[0],200);
  image(photo,200,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_FROM_ARDUINO];
}

void getSerialData() {
  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_FROM_ARDUINO) {
        for (int i=0; i<serialInArray.length; i++) {
          sensorValues[i] = int(serialInArray[i]);
        }
      }
    }
  }
}

 

Part II: Processing to Arduino

Leave a Reply

Your email address will not be published. Required fields are marked *