Exercise 1:
//arduino exercise #1 void setup() { Serial.begin(9600); Serial.println("0,0"); } void loop() { while (Serial.available()) { if (Serial.read() == '\n') { int sensor = analogRead(A0); delay(1); Serial.print(sensor); Serial.println(','); } } } //processing exercise #1 import processing.serial.*; Serial myPort; int xPos=0; int yPos=height/2; void setup() { size(960, 720); 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); ellipse(xPos, yPos, 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); } } myPort.write(xPos+','+"\n"); } //arduino exercise #2 int left = 0; int right = 0; void setup() { Serial.begin(9600); Serial.println("0,0"); pinMode(3, OUTPUT); } void loop() { while (Serial.available()) { right = Serial.parseInt(); left = Serial.parseInt(); if (Serial.read() == '\n') { analogWrite(3, right); Serial.print(0); Serial.print(','); Serial.println(0); } } }
Exercise 2:
//arduino exercise #2 int left = 0; int right = 0; void setup() { Serial.begin(9600); Serial.println("0,0"); pinMode(3, OUTPUT); } void loop() { while (Serial.available()) { right = Serial.parseInt(); left = Serial.parseInt(); if (Serial.read() == '\n') { analogWrite(3, right); Serial.print(0); Serial.print(','); Serial.println(0); } } } //processing exercise #2 import processing.serial.*; Serial myPort; int xPos=0; void setup(){ size(960,720); 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); } 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(mouseX,0,width, 0, 255); } } myPort.write(xPos+","+0+"\n"); } //arduino exercise #3 void setup() { Serial.begin(9600); Serial.println("0,0"); pinMode(3, OUTPUT); } void loop() { while (Serial.available()) { int onOff = Serial.parseInt(); if (Serial.read() == '\n') { digitalWrite(3, onOff); int sensor = analogRead(A0); delay(1); Serial.println(sensor); } } }
Exercise 3:
//arduino exercise #3 void setup() { Serial.begin(9600); Serial.println("0,0"); pinMode(3, OUTPUT); } void loop() { while (Serial.available()) { int onOff = Serial.parseInt(); if (Serial.read() == '\n') { digitalWrite(3, onOff); int sensor = analogRead(A0); delay(1); Serial.println(sensor); } } } //processing exercise #3 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 onOff=false; float analog; void setup() { size(640, 360); String portname=Serial.list()[3]; println(portname); myPort = new Serial(this, portname, 9600); myPort.clear(); myPort.bufferUntil('\n'); 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); } void draw() { background(255); if (!keyPressed) { wind.x= analog; 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){ onOff = true; } else{ onOff = false; } } void serialEvent(Serial myPort) { String s=myPort.readStringUntil('\n'); s=trim(s); int values = parseInt(s); if (s!=null) { analog =map(values, 0, 1023, -3, 3); } myPort.write(int(onOff)+"\n"); } 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); } }
Exercise 3 video:
https://www.youtube.com/shorts/TAmqSXp7FqE