Exercise 1
- Drawing an ellipse and making it move horizontally based on photoresistor
Code:
let x=0; function setup() { createCanvas(400,400); textSize(18); } function draw() { if (serialActive){ background("white"); ellipse(map(x,0,1023,0,400),height/2,50,50); }else{ background("white"); console.log("not connected"); } } function keyPressed() { if (key == " ") { // important to have in order to start the serial connection!! setUpSerial(); } } function readSerial(data) { if (data != null) { x = int(data); }else{ x = 0; } } //Arduino code // void setup() { // Serial.begin(9600); // pinMode(A0, INPUT); // } // void loop() { // Serial.println(analogRead(A0)); // }
Exercise 2
Code:
let bright=0; function setup() { createCanvas(400,400); textSize(18); } function draw() { if (serialActive){ background("white"); text(bright,10,10); }else{ background("white"); console.log("not connected"); } } function keyPressed() { if (key == " ") { // important to have in order to start the serial connection!! setUpSerial(); } } function mousePressed(){ if(serialActive){ bright+=1; writeSerial(bright); } } function readSerial(data) { } //Arduino code // int brightness; // void setup() { // Serial.begin(9600); // pinMode(9, OUTPUT); // } // void loop() { // brightness = Serial.parseInt(); // analogWrite(9,brightness); // }
Exercise 3
- Turning on LED when the ball bounces and use photoresistor to control wind movement
Circuit
Video:
Code:
let velocity; let gravity; let position; let acceleration; let wind; let drag = 0.99; let mass = 50; let bounce = 0; function setup() { createCanvas(640, 360); noFill(); position = createVector(width/2, 0); velocity = createVector(0,0); acceleration = createVector(0,0); gravity = createVector(0, 0.5*mass); wind = createVector(0,0); } function draw() { background(255); if(!serialActive){ console.log("PRESS a TO CONNECT"); } else{ 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; bounce = 1; }else{ bounce = 0; } } } function applyForce(force){ // Newton's 2nd law: F = M * A // or A = F / M let f = p5.Vector.div(force, mass); acceleration.add(f); } function keyPressed(){ if (keyCode==LEFT_ARROW){ wind.x=-1; } if (keyCode==RIGHT_ARROW){ wind.x=1; } if (key==' '){ mass=random(15,80); position.y=-mass; velocity.mult(0); } if (key == "a") { // important to have in order to start the serial connection!! setUpSerial(); } } function readSerial(data) { if (data != null) { console.log(data); wind.x = map(int(data), 0, 1023, -2, 2); writeSerial(bounce + '\n'); } } //Arduino code // int bounce; // void setup() { // Serial.begin(9600); // pinMode(9,OUTPUT); // while (Serial.available() <= 0) { // Serial.println("0"); // send a starting message // } // } // void loop() { // while(Serial.available()){ // bounce = Serial.parseInt(); // if (Serial.read() == '\n') { // digitalWrite(9,bounce); // } // } // int sensor = analogRead(A1); // Serial.println(sensor); // // Serial.println(analogRead(A0)); // }