1: Ellipse
/* * Week 11 Production (1) * * Inputs: * - A1 - 10k potentiometer connected to 5V and GND * */ int interval = 100; int lastMessageTime = 0; int potPin = A1; void setup() { Serial.begin(9600); // initialize serial communications } void loop() { // read the input pin: int potentiometer = analogRead(potPin); // remap the pot value to 0-255: int mappedPotValue = map(potentiometer, 0, 1023, 0, 255); // print the value to the serial port. Serial.println(mappedPotValue); // slight delay to stabilize the ADC: delay(1); delay(100); }
let port; let connectBtn; let baudrate = 9600; let lastMessage = ""; let currX; function setup() { createCanvas(400, 400); background(220); port = createSerial(); let usedPorts = usedSerialPorts(); if (usedPorts.length > 0) { port.open(usedPorts[0], baudrate); } connectBtn = createButton("Connect to Arduino"); connectBtn.position(80, height-60); connectBtn.mousePressed(connectBtnClick); currX = width/2; } function draw() { background("white"); fill('grey'); circle(currX, height/2, 100); let str = port.readUntil("\n"); if (str.length > 0) { // console.log(str); lastMessage = str; } // Display the most recent message text("Last message: " + lastMessage, 10, height - 20); // change button label based on connection status if (!port.opened()) { connectBtn.html("Connect to Arduino"); } else { connectBtn.html("Disconnect"); } // // Move shape based on received value if (!lastMessage) {lastMessage = "127"} currX = map(int(lastMessage), 0, 255, 0, width); currX = floor(currX); // console.log(currX); } function connectBtnClick() { if (!port.opened()) { port.open("Arduino", baudrate); } else { port.close(); } }
2: LED Brightness
/* * Week 11 Production (2) * * Outputs: * - 5 - LED * */ int ledPin = 5; void setup() { Serial.begin(9600); pinMode(LED_BUILTIN, OUTPUT); pinMode(ledPin, OUTPUT); // Blink them so we can check the wiring digitalWrite(ledPin, HIGH); delay(200); digitalWrite(ledPin, LOW); // start the handshake while (Serial.available() <= 0) { digitalWrite(LED_BUILTIN, HIGH); // on/blink while waiting for serial data Serial.println("0,0"); // send a starting message delay(300); // wait 1/3 second digitalWrite(LED_BUILTIN, LOW); delay(50); } } void loop() { // wait for data from p5 before doing something while (Serial.available()) { digitalWrite(LED_BUILTIN, HIGH); // led on while receiving data int brightness = Serial.parseInt(); if (Serial.read() == '\n') { analogWrite(ledPin, brightness); } } digitalWrite(LED_BUILTIN, LOW); }
let port; let baudrate = 9600; // Show button to connect / disconnect let showConnectButton = false; function setup() { createCanvas(640, 480); textSize(20); // Create the serial port port = createSerial(); // If the user previously connected, reopen the same port let usedPorts = usedSerialPorts(); if (usedPorts.length > 0) { port.open(usedPorts[0], baudrate); } // any other ports can be opened via a dialog if (showConnectButton) { connectBtn = createButton('Connect to Arduino'); connectBtn.position(80, 350); connectBtn.mousePressed(setupSerial); } } // Show serial port connection dialog in response to action function setupSerial() { if (!port.opened()) { port.open('Arduino', baudrate); } else { port.close(); } } function draw() { background('white'); fill('black'); if (showConnectButton) { // changes button label based on connection status if (!port.opened()) { connectBtn.html('Connect to Arduino'); } else { connectBtn.html('Disconnect'); } } if (!port.opened()) { text("Disconnected - press space to connect", 20, 30); } else { text("Connected - press space to disconnect", 20, 30); // // Transmit brightness based on mouse position mappedX = floor(map(mouseX, 0, width, 0, 255)); console.log(mappedX); let sendToArduino = mappedX + "\n"; port.write(sendToArduino); } } function keyPressed() { if (key == " ") { setupSerial(); } }
3: Wind Gravity
/* * Week 11 Production (3) * * Inputs: * - A1 - 10k potentiometer connected to 5V and GND * * Outputs: * - 5 - LED * */ int potPin = A1; int ledPin = 5; int interval = 100; int lastMessageTime = 0; void setup() { Serial.begin(9600); pinMode(LED_BUILTIN, OUTPUT); pinMode(potPin, INPUT); pinMode(ledPin, OUTPUT); // Blink them so we can check the wiring digitalWrite(ledPin, HIGH); delay(200); digitalWrite(ledPin, LOW); // start the handshake while (Serial.available() <= 0) { digitalWrite(LED_BUILTIN, HIGH); // on/blink while waiting for serial data Serial.println("127"); // send a starting message delay(300); // wait 1/3 second digitalWrite(LED_BUILTIN, LOW); delay(50); } } void loop() { // wait for data from p5 before doing something while (Serial.available()) { digitalWrite(LED_BUILTIN, HIGH); // led on while receiving data // blink LED based on p5 data int status = Serial.parseInt(); if (Serial.read() == '\n') { digitalWrite(ledPin, status); delay(10); digitalWrite(ledPin, LOW); } if (lastMessageTime > interval) { lastMessageTime = 0; // send mapped potentiometer reading to p5 int potentiometer = analogRead(potPin); int mappedPotValue = map(potentiometer, 0, 1023, 0, 255); Serial.println(mappedPotValue); // slight delay to stabilize the ADC: delay(1); } else { lastMessageTime++; } } digitalWrite(LED_BUILTIN, LOW); }
let velocity; let gravity; let position; let acceleration; let wind; let drag = 0.99; let mass = 50; // // Arduino let port; let connectBtn; let baudrate = 9600; let lastMessage = ""; let showConnectButton = false; function setup() { createCanvas(620, 400); noFill(); position = createVector(width/2, 0); velocity = createVector(0,0); acceleration = createVector(0,0); gravity = createVector(0, 0.5*mass); wind = createVector(0,0); // // Arduino port = createSerial(); let usedPorts = usedSerialPorts(); if (usedPorts.length > 0) { port.open(usedPorts[0], baudrate); } if (showConnectButton) { connectBtn = createButton('Connect to Arduino'); connectBtn.position(80, 300); connectBtn.mousePressed(setupSerial); } } 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; flashLight(); } // // Arduino if (showConnectButton) { if (!port.opened()) { connectBtn.html('Connect to Arduino'); } else { connectBtn.html('Disconnect'); } } fill('black'); if (!port.opened()) { text("Disconnected", 20, 30); } else { text("Connected", 20, 30); } let str = port.readUntil("\n"); if (str.length > 0) { // console.log(str); lastMessage = str; } // Display the most recent message text("Last message: " + lastMessage, 10, height - 20); // // Convert received value to wind.x value mappedPot = map(int(lastMessage), 0, 255, -1, 1); wind.x = mappedPot; let windSpeed = "Wind speed: " + wind.x text(windSpeed.substring(0,20), 10, height - 5); fill('white'); } 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); port.write("0\n"); // reset light } } // // Arduino function setupSerial() { if (!port.opened()) { port.open('Arduino', baudrate); } else { port.close(); } } function flashLight() { if (port.opened()) { port.write("1\n"); // port.write("0\n"); } }