Group members : Liya Rafeeq, Kashish Satija
Exercise 11.1 :
-
Make something that uses only one sensor on Arduino and makes the ellipse in p5 move on the horizontal axis, in the middle of the screen, and nothing on arduino is controlled by p5 – for this we used a potentiometer. We mapped the values of the potentiometer to change the X coordinate of the ellipse, making it move along the horizontal axis.
P5.JS CODE :
let port; let connectBtn; let baudrate = 9600; function setup() { createCanvas(400, 400); background(220); port = createSerial(); //serial connection let usedPorts = usedSerialPorts(); if (usedPorts.length > 0) { port.open(usedPorts[0], baudrate); } let connectBtn = createButton("Connect to Serial"); connectBtn.mousePressed(() => port.open(baudrate)); } function draw() { //read from the serial port, complete string till the "" let str = port.readUntil("\n"); if (str.length > 0) { background("white"); ellipse(int(str),200,40,40); } }
ARDUINO CODE:
void setup() { Serial.begin(9600); // initialize serial communications } void loop() { // read the input pin: int potentiometer = analogRead(A1); // remap the pot value to 0-400: int mappedPotValue = map(potentiometer, 0, 1023, 0, 400); // print the value to the serial port. Serial.println(mappedPotValue); // slight delay to stabilize the ADC: delay(1); // Delay so we only send 10 times per second and don't // flood the serial connection leading to missed characters on the receiving side delay(100); }
Exercise 11.2 :
2. Make something that controls the LED brightness from p5. For this, we made a circle that moves along the Y axis. According to the Y coordinates, the LED turns brighter or lower.
P5.JS. CODE:
let port; let connectBtn; let baudrate = 9600; function setup() { createCanvas(255, 285); port = createSerial(); // in setup, we can open ports we have used previously // without user interaction let usedPorts = usedSerialPorts(); if (usedPorts.length > 0) { port.open(usedPorts[0], baudrate); } else { connectBtn = createButton("Connect to Serial"); connectBtn.mousePressed(() => port.open(baudrate)); } } function draw() { background(220); circle(128,mouseY,30,30) let sendtoArduino = String(mouseY) + "\n" port.write(sendtoArduino); }
ARDUINO CODE:
int led = 5; void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(led, OUTPUT); } void loop() { // put your main code here, to run repeatedly: while (Serial.available()) { digitalWrite(LED_BUILTIN, HIGH); // led on while receiving data int brightness = Serial.parseInt(); //get slider value from p5 if (Serial.read() == '\n') { analogWrite(led, brightness); } } }
Exercise 11.3:
Take the gravity wind example (https://editor.p5js.org/aaronsherwood/sketches/I7iQrNCul) 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: For this, we used the potentiometer as the analog sensor.
P5.JS CODE:
let baudrate = 9600; let velocity; let gravity; let position; let acceleration; let wind; let drag = 0.99; let mass = 50; let str=""; let val; let heightOfBall = 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); port = createSerial(); // in setup, we can open ports we have used previously // without user interaction let usedPorts = usedSerialPorts(); if (usedPorts.length > 0) { port.open(usedPorts[0], baudrate); } else { connectBtn = createButton("Connect to Serial"); connectBtn.mousePressed(() => port.open(baudrate)); } } function draw() { background(255); 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; heightOfBall = 1; } else { heightOfBall = 0; } str = port.readUntil("\n"); val=int(str); if (!isNaN(val)) { breeze(val); } } 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 breeze(val){ if (val<400){ wind.x=-1; } else if (val>500 && val<900){ wind.x=1; } else { wind.x=0 } let sendToArduino = String(heightOfBall) + "\n"; port.write(sendToArduino); } function keyPressed(){ if (key==' '){ mass=random(15,80); position.y=-mass; velocity.mult(0); } }
ARDUINO CODE:
int led = 5; void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(led, OUTPUT); } void loop() { // put your main code here, to run repeatedly: while (Serial.available()) { digitalWrite(LED_BUILTIN, HIGH); // led on while receiving data int brightness = Serial.parseInt(); //get slider value from p5 if (Serial.read() == '\n') { analogWrite(led, brightness); } } }
VIDEO and SCHEMATIC: