This week, we had to complete these three exercises:
-
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
-
make something that controls the LED brightness from processing
-
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
I was able to complete 2 of these in class, and the third one required some figuring out. The Arduino and Processing codes will be inserted for each in order here. I had to attach 2 videos at the end for exercise 3 because my phone memory was acting up and not letting me record long videos. This was the best I could manage.
Exercise 1
This exercise still has the code to take inputs from both sensors, in case we wanted to change it up.
Processing
import processing.serial.*; Serial myPort; int xPos=0; int yPos=0; boolean onOff=false; boolean onOff2=false; void setup(){ size(960,720); printArray(Serial.list()); String portname=Serial.list()[2]; println(portname); myPort = new Serial(this,portname,9600); myPort.clear(); myPort.bufferUntil('\n'); } void draw(){ background(255); ellipse(xPos,height/2,30,30); } void serialEvent(Serial myPort){ String s=myPort.readStringUntil('\n'); s=trim(s); if (s!=null){ println(s); int values[]=int(split(s,',')); if (values.length==2){ xPos=(int)map(values[0],0,1023,0, width); yPos=(int)map(values[1],0,1023,0, height); } } myPort.write(int(onOff)+","+int(onOff2)+"\n"); }
Arduino
int left = 0; int right = 0; void setup() { Serial.begin(9600); Serial.println("0,0"); pinMode(2, OUTPUT); pinMode(5, OUTPUT); } void loop() { while (Serial.available()) { right = Serial.parseInt(); left = Serial.parseInt(); if (Serial.read() == '\n') { digitalWrite(2, right); digitalWrite(5, left); int sensor = analogRead(A0); delay(1); int sensor2 = analogRead(A1); delay(1); Serial.print(sensor); Serial.print(','); Serial.println(sensor2); } } }
Exercise 2
Processing
import processing.serial.*; Serial myPort; int xPos=0; int yPos=0; boolean onOff=false; boolean onOff2=false; void setup(){ size(960,720); printArray(Serial.list()); String portname=Serial.list()[2]; println(portname); myPort = new Serial(this,portname,9600); myPort.clear(); myPort.bufferUntil('\n'); } void draw(){ background(255); ellipse(mouseX,mouseY,30,30); } void serialEvent(Serial myPort){ String s=myPort.readStringUntil('\n'); myPort.write((int)map(mouseY, height, 0, 0, 255)+"\n"); }
Arduino
int brightness; void setup() { Serial.begin(9600); Serial.println("0,0"); pinMode(2, OUTPUT); pinMode(5, OUTPUT); } void loop() { while (Serial.available()) { brightness = Serial.parseInt(); if (Serial.read() == '\n') { analogWrite(5, brightness); Serial.println("0"); } } }
Exercise 3
Processing
PVector velocity; PVector gravity; PVector position; PVector acceleration; PVector wind; float drag = 0.99; float mass = 50; float hDampening; int sensor = 0; int sensor_prev = 0; float pos_prev = 0; import processing.serial.*; Serial myPort; int xPos=0; int yPos=0; boolean onOff=false; boolean onOff2=false; int bounce = 0; 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()[2]; println(portname); myPort = new Serial(this,portname,9600); myPort.clear(); myPort.bufferUntil('\n'); } void draw() { background(255); bounce = 0; if (sensor==0){ wind.x=0; 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((int)position.y >= height-mass/2 && (int)position.y != pos_prev){ bounce = 1; } if (position.y > height-mass/2) { pos_prev = (int)position.y; velocity.y *= -0.9; // A little dampening when hitting the bottom position.y = height-mass/2; } } 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 (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 i = int(s); sensor = (int)map(i, 0, 1023, 0, width); if(sensor>sensor_prev){ wind.x=1; } else if(sensor<sensor_prev){ wind.x=-1; } sensor_prev = sensor; } myPort.write(bounce+"\n"); }
Arduino
int onOff; void setup() { Serial.begin(9600); Serial.println("0,0"); pinMode(2, OUTPUT); pinMode(5, OUTPUT); } void loop() { while (Serial.available()) { onOff = Serial.parseInt(); if (Serial.read() == '\n') { int sensor = analogRead(A0); digitalWrite(5, onOff); Serial.println(sensor); } } }
I used the value of the sensor compared to its previous value to move the wind left and right, instead of the left and right keys. I was initially going to leave the LED on at the end after updating bounce in the existing if block, but then I thought the same logic could be applied to the y position of the ball too and I added another if block to accommodate that.
Note: I’m not able to transfer the longer video for now, I’ll try to get it up as soon as I can.
That’s it for now, no long rambling post this time!