Recitation 9: Media Controller by Amy DeCillis

Media Controller

Arduino Code

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

void loop() {
  int sensor1 = analogRead(A0);
  int sensor2 = analogRead(A1);
  int sensor3 = analogRead(A2);

//  sensor1 = map(sensor1,0,1023,0,100);
  sensor1 = map(sensor1,0,1023,0,255);
  sensor2 = map(sensor2,0,1023,0,255);
  sensor3 = map(sensor3,0,1023,0,255);

  // keep this format
  Serial.print(sensor1);
  Serial.print(",");  // put comma between sensor values
  Serial.print(sensor2);
  Serial.print(",");  // put comma between sensor values
  Serial.print(sensor3);
  Serial.println(); // add linefeed after last value

  delay(100);
}

Processing Code

import processing.serial.*;

String myString = null;
Serial myPort;

int NUM_OF_VALUES = 3;   
int[] sensorValues;      

PImage photo;
void setup() {
  size(1100, 700);
  background(0);
  setupSerial();
  photo = loadImage("door.jpg");
}

void draw() {
  updateSerial();
  printArray(sensorValues);
  imageMode(CENTER);
  image(photo, width/2, height/2);
  tint(sensorValues[0],sensorValues[1],sensorValues[2]);
}

void setupSerial() {
  printArray(Serial.list());
  myPort = new Serial(this, Serial.list()[1], 9600);

  myPort.clear();

  myString = myPort.readStringUntil( 10 ); 
  myString = null;

  sensorValues = new int[NUM_OF_VALUES];
}

void updateSerial() {
  while (myPort.available() > 0) {
    myString = myPort.readStringUntil( 10 ); 
    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]);
        }
      }
    }
  }
}

Reflection

Leave a Reply