Exercise1
void setup() { Serial.begin(9600); } void loop() { int sensor1 = analogRead(A0); int sensor2 = analogRead(A1); Serial.print(sensor1); Serial.print(","); // put comma between sensor values Serial.print(sensor2); Serial.println(); // add linefeed after sending the last sensor value delay(100); }
Exercise2
import processing.serial.*; PImage photo; 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; int x; void setup() { size(800, 800); background(0); setupSerial(); x = width/2; photo = loadImage("mouse.jpg"); photo.resize(100, 100); } void draw() { background(0); getSerialData(); printArray(sensorValues); if (sensorValues[0]>600) { x=x+20; } if (sensorValues[1]>600) { x=x-20; } image(photo, x, height-100); if (x>width-100) { x=width-100; } if (x<0) { x=0; } } void setupSerial() { printArray(Serial.list()); myPort = new Serial(this, Serial.list()[ 2 ], 9600); myPort.clear(); 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]); } } } } }
import processing.video.*; String[] cameras = Capture.list(); Capture cam; float blue, pblue; import processing.sound.*; SinOsc sine; Env env; float attackTime = 0.002; float sustainTime = 0.005; float sustainLevel = 0.2; float releaseTime = 0.3; int s = 20; void setup() { size(800,800); printArray(cameras); cam = new Capture(this, cameras[1]); cam.start(); sine = new SinOsc(this); env = new Env(this); //sine.play(); } void draw() { background(0); if (cam.available()) { cam.read(); } color c = cam.get(width/2, height/2); blue = blue(c); float difference = abs(blue-pblue); for (int i=0; i<width; i = i + s) { for (int j=0; j<height; j = j + s) { float size = map(blue, 0, 255, 5, 80); color all =cam.get(i, j); fill(all); noStroke(); rect(i, j, size, size); } } if (difference>10) { sine.play(); sine.freq(map(blue, 0, 255, 100, 800)); env.play(sine, attackTime, sustainTime, sustainLevel, releaseTime); } pblue=blue; }