Exercise 1
Make something that uses only one sensor on arduino and makes the ellipse in processing move on the horizontal axis, in the middle of the screen, and nothing on arduino is controlled by processing
For this exercise we used the potentiometer to control the movement of the ellipse
Video:
https://youtu.be/HZbzQkfixq8
Arduino
//Exercise 1 const int pinPont = A0; void setup() { Serial.begin(9600); Serial.println("0"); } void loop() { while (Serial.available()) { if (Serial.read() == '\n') { //read the values of the potentiometer int sensor = analogRead(pinPont); delay(1); //print the value Serial.println(sensor); } } }
Processing
//Exercise 1 import processing.serial.*; Serial myPort; int xPos = 0; int yPos; void setup() { size(960, 720); printArray(Serial.list()); String portname=Serial.list()[3]; // "/dev/cu.usbmodem101" println(portname); myPort = new Serial(this, portname, 9600); myPort.clear(); myPort.bufferUntil('\n'); } void draw() { background(255); yPos = height/2; ellipse(xPos, yPos, 30, 30); } void serialEvent(Serial myPort) { String s=myPort.readStringUntil('\n'); s=trim(s); if (s!=null) { //read value from Arduino (potentiometer) println(s); int value = int(s); //map the value to a position in range of the width xPos=(int)map(value, 0, 1023, 0, width); } myPort.write("\n"); }
Exercise 2
Make something that controls the LED brightness from processing.
For this exercise we used the x value of the position of the mouse to control the brightness of the LED.
Video: https://youtu.be/oJ8sW842y5c
Arduino
//Exercise 2 int ledPin = 5; float brightness; void setup() { Serial.begin(9600); Serial.println("0"); pinMode(ledPin, OUTPUT); } void loop() { while (Serial.available()) { //get the brightness value from processing brightness = Serial.parseFloat(); if (Serial.read() == '\n') { //turn on the LED with the given brightness analogWrite(ledPin, brightness); Serial.println(brightness); } } }
Processing
//Exercise 2 import processing.serial.*; Serial myPort; int xPos = 0; int yPos; int brightness; void setup() { size(960, 720); printArray(Serial.list()); String portname=Serial.list()[3]; // "/dev/cu.usbmodem101" println(portname); myPort = new Serial(this, portname, 9600); myPort.clear(); myPort.bufferUntil('\n'); } void draw() { background(255); //map the mouseX position to the range of birghtness of the LED brightness = int(map(mouseX, 0, width, 0, 255)); yPos = height/2; ellipse(mouseX, yPos, 30, 30); } void serialEvent(Serial myPort) { String s=myPort.readStringUntil('\n'); s=trim(s); //write brightness so that Arduino can use it myPort.write(brightness+ "\n"); }
Exercise 3
Take the gravity wind example (https://github.com/aaronsherwood/introduction_interactive_media/blob/master/processingExamples/gravityExamples/gravityWind/gravityWind.pde) and make it so every time the ball bounces one led lights up and then turns off, and you can control the wind from one analog sensor.
The analog sensor we used was the potentiometer.
Video: https://youtu.be/iU62kg-Mad8
Video: https://youtu.be/CvLVrLY0g4A
Arduino
int left = 0; int right = 0; int light1 = 0; int LED1 = LOW; void setup() { Serial.begin(9600); Serial.println("0,0"); pinMode(2, OUTPUT); pinMode(5, OUTPUT); } void loop() { while (Serial.available()) { // right = Serial.parseInt(); //get whether LED should be turned on from Processing light1 = Serial.parseInt(); Serial.println(light1); if (Serial.read() == '\n') { if (light1 == 1) { LED1 = HIGH; } else { LED1 = LOW; } digitalWrite(5, LED1); //get the vvalues from the potentiometer int sensor = analogRead(A0); delay(1); // delay(1); Serial.println(sensor); // Serial.print(','); // Serial.println(sensor2); //Serial.println(LED1); } } }
Processing
import processing.serial.*; Serial myPort; PVector velocity; PVector gravity; PVector position; PVector acceleration; PVector wind; float drag = 0.99; float mass = 50; float hDampening; boolean light; int valueCapacitor; void setup() { size(640,360); noFill(); position = new PVector(width/2, 0); velocity = new PVector(0,0); acceleration = new PVector(0,0); gravity = new PVector(0, 0.5*mass); wind = new PVector(0,0); hDampening=map(mass,15,80,.98,.96); printArray(Serial.list()); String portname=Serial.list()[3]; println(portname); myPort = new Serial(this,portname,9600); myPort.clear(); myPort.bufferUntil('\n'); } void draw() { background(255); if (!keyPressed){ wind.x= valueCapacitor; velocity.x*=hDampening; } applyForce(wind); applyForce(gravity); velocity.add(acceleration); velocity.mult(drag); position.add(velocity); acceleration.mult(0); ellipse(position.x,position.y,mass,mass); if (position.y > height-mass/2) { velocity.y *= -0.9; // A little dampening when hitting the bottom position.y = height-mass/2; } if (position.y == height - mass/2){ light = true; } } void applyForce(PVector force){ // Newton's 2nd law: F = M * A // or A = F / M PVector f = PVector.div(force, mass); acceleration.add(f); } void keyPressed(){ if (keyCode==LEFT){ wind.x=-1; } if (keyCode==RIGHT){ wind.x=1; } if (key==' '){ mass=random(15,80); position.y=-mass; velocity.mult(0); } } void serialEvent(Serial myPort) { String s=myPort.readStringUntil('\n'); s=trim(s); if (s!=null) { int value = int(s); valueCapacitor = (int)map(value, 0, 1023, 0, width*.01); } // we need to use velocity,y so that the light turns off when it is not moving // if we use position, the blinking is delayed if (round(velocity.y) < 0) { myPort.write(1 + "\n"); println(1); } else { myPort.write(0 + "\n"); println(0); } }