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;
Arduino:
const int knobLed = A0; //sign up variable void setup() { Serial.begin(9600); //set the baud rate Serial.println("0"); //start of the handshake, start the call-response pattern pinMode(A0, INPUT); } void loop() { while (Serial.available()) { //whenever there is anything available in serial buffer, do if (Serial.read() == '\n') { //whenever there is new line, it means that all info came correctly int sensor = analogRead(knobLed); //read from potentiometer delay(1); Serial.println(sensor); //send info to the processing } } }
Processing:
import processing.serial.*; //import library Serial myPort; //create serial object int xPos=0; int yPos; void setup() { size(960, 720); yPos=height/2; printArray(Serial.list()); //printing array String portname=Serial.list()[1]; //choose the correct port number! println(portname); myPort = new Serial(this, portname, 9600); //set the baud rate myPort.clear(); //clear the buffer myPort.bufferUntil('\n'); //wait until new line } void draw() { background(255); ellipse(xPos, yPos, 30, 30); } void serialEvent(Serial myPort) { //whenever receives incoming serial message, this function is called String s=myPort.readStringUntil('\n'); //read the string until new line character s=trim(s); //safeguard to trim extra characters (whitespaces) if (s!=null) { //check to make sure it's not null println(s); int value = int(s); //sending value s xPos=(int)map(value, 0, 1023, 0, width); } myPort.write("\n"); //other side of the handshake, send back over to arduino }
Exercise 2: make something that controls the LED brightness from processing;
Arduino:
float brightness; void setup() { Serial.begin(9600); //set the baud rate Serial.println("0"); //start the handshake pinMode(5, OUTPUT); } void loop() { while (Serial.available()) { brightness = Serial.parseFloat(); //receive brightness info in a string from processing if (Serial.read() == '\n') { //whenever there is new line, it means that all info came correctly analogWrite(5, brightness); //change the brightness of LED accordingly Serial.print("\n"); //send new line to processing } } }
Processing:
import processing.serial.*; Serial myPort; //create serial object float brightness; //variable to send to arduino void setup(){ size(960,720); printArray(Serial.list()); String portname=Serial.list()[1]; //choose the correct port number! println(portname); myPort = new Serial(this,portname,9600); //set the baud rate myPort.clear(); //clear the buffer myPort.bufferUntil('\n'); //wait until new line } void draw(){ background(255); ellipse(mouseX,mouseY,30,30); brightness = map(height-mouseY, 0, height, 0, 255); } void serialEvent(Serial myPort){ //whenever receives incoming serial message, this function is called println(brightness); myPort.write(int(brightness)+"\n"); //other side of the handshake, send back over to arduino }
Exercise 3: take the gravity wind example 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;
Arduino:
const int knobLed = A0; const int led1 = 3; int onOff = 0; void setup() { Serial.begin(9600); //set the baud rate Serial.println("0"); //start the handshake } void loop() { while (Serial.available()) { onOff = Serial.parseInt(); //receive onOff info from processing if (Serial.read() == '\n') { //whenever there is new line, it means that all info came correctly int sensor = analogRead(knobLed); //read from potentiometer delay(1); Serial.println(sensor); //send info to processing if (onOff == 1) { analogWrite(led1, 255);//turn on LED } else { analogWrite(led1, 0); //turn off LED } } } }
Processing:
PVector velocity; PVector gravity; PVector position; PVector acceleration; PVector wind; float drag = 0.99; float mass = 50; float hDampening; import processing.serial.*; Serial myPort; //create serial object int knob; void setup() { size(640,360); noFill(); position = new PVector(0, 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); String portname=Serial.list()[1]; //choose the correct port number! myPort = new Serial(this,portname,9600); //set the baud rate myPort.clear(); //clear the buffer myPort.bufferUntil('\n'); //wait until new line } void draw() { background(255); if (!keyPressed){ wind.x=knob; 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; } } 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){ //whenever receives incoming serial message, this function is called String s=myPort.readStringUntil('\n'); //read the string until new line character s=trim(s); //safeguard to trim extra characters (whitespaces) if (s!=null){ //check to make sure it's not null println(s); int values[]=int(split(s,',')); //sending value s knob=(int)map(values[0],0,1023,0, width*.01); } if (round(velocity.y) < 0) { myPort.write(1 + "\n"); } else { myPort.write(0 + "\n"); } }
Video: