Exercise 1:
P5js code for exercise 1:
// variable to hold an instance of the p5.webserial library: const serial = new p5.WebSerial(); // HTML button object: let portButton; let inData; // for incoming serial data let outByte = 0; // for outgoing data function setup() { createCanvas(400, 300); // make the canvas // check to see if serial is available: if (!navigator.serial) { alert("WebSerial is not supported in this browser. Try Chrome or MS Edge."); } // if serial is available, add connect/disconnect listeners: navigator.serial.addEventListener("connect", portConnect); navigator.serial.addEventListener("disconnect", portDisconnect); // check for any ports that are available: serial.getPorts(); // if there's no port chosen, choose one: serial.on("noport", makePortButton); // open whatever port is available: serial.on("portavailable", openPort); // handle serial errors: serial.on("requesterror", portError); // handle any incoming serial data: serial.on("data", serialEvent); serial.on("close", makePortButton); } function draw() { background(0); fill(255); text("sensor value: " + inData, 30, 50); } // if there's no port selected, // make a port select button appear: function makePortButton() { // create and position a port chooser button: portButton = createButton("choose port"); portButton.position(10, 10); // give the port button a mousepressed handler: portButton.mousePressed(choosePort); } // make the port selector window appear: function choosePort() { if (portButton) portButton.show(); serial.requestPort(); } // open the selected port, and make the port // button invisible: function openPort() { // wait for the serial.open promise to return, // then call the initiateSerial function serial.open().then(initiateSerial); // once the port opens, let the user know: function initiateSerial() { console.log("port open"); } // hide the port button once a port is chosen: if (portButton) portButton.hide(); } // pop up an alert if there's a port error: function portError(err) { alert("Serial port error: " + err); } // read any incoming data as a string // (assumes a newline at the end of it): function serialEvent() { inData = Number(serial.read()); console.log(inData); } // try to connect if a new serial port // gets added (i.e. plugged in via USB): function portConnect() { console.log("port connected"); serial.getPorts(); } // if a port is disconnected: function portDisconnect() { serial.close(); console.log("port disconnected"); } function closePort() { serial.close(); }
Arduino code for exercise 1:
void setup() { Serial.begin(9600); // initialize serial communications } void loop() { // read the input pin: int potentiometer = analogRead(A0); // remap the pot value to fit in 1 byte: int mappedPot = map(potentiometer, 0, 1023, 0, 255); // print it out the serial port: Serial.write(mappedPot); // slight delay to stabilize the ADC: delay(1); // Delay so we only send 10 times per second and don't // flood the serial connection delay(100); }
Demo:
Exercise 2:
P5js code:
let serial; // variable to hold an instance of the serialport library let portName = "COM3"; // fill in your serial port name here let xPos=0; let yPos=240; let onOff=0; let val; function setup() { createCanvas(640, 480); serial = new p5.SerialPort(); // make a new instance of the serialport library serial.on("list", printList); // set a callback function for the serialport list event serial.on("connected", serverConnected); // callback for connecting to the server serial.on("open", portOpen); // callback for the port opening serial.on("data", serialEvent); // callback for when new data arrives serial.on("error", serialError); // callback for errors serial.on("close", portClose); // callback for the port closing serial.list(); // list the serial ports serial.open(portName); // open a serial port } function draw() { background(255); val = map(mouseX, 0, width, 0, 255); } // get the list of ports: function printList(portList) { // portList is an array of serial port names for (let i = 0; i < portList.length; i++) { // Display the list the console: print(i + " " + portList[i]); } } function serverConnected() { print("connected to server."); } function portOpen() { print("the serial port opened."); } function serialEvent() { // read a string from the serial port // until you get carriage return and newline: let inString = serial.readLine(); serial.write(val); } function serialError(err) { print("Something went wrong with the serial port. " + err); } function portClose() { print("The serial port closed."); }
Arduino code:
void setup() { Serial.begin(9600); pinMode(2, OUTPUT); pinMode(5, OUTPUT); while (Serial.available() <= 0) { Serial.println("0,0"); // send a starting message delay(200); } } void loop() { while (Serial.available() > 0) { // read the incoming byte: int inByte = Serial.read(); analogWrite(5,inByte); Serial.println(); } }
Exercise 3:
P5js code:
let serial; // variable to hold an instance of the serialport library let portName = "COM7"; // fill in your serial port name here let velocity; let gravity; let position; let acceleration; let wind; let drag = 0.99; let mass = 50; let windVal; 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); serial = new p5.SerialPort(); // make a new instance of the serialport library serial.on("list", printList); // set a callback function for the serialport list event serial.on("connected", serverConnected); // callback for connecting to the server serial.on("open", portOpen); // callback for the port opening serial.on("data", serialEvent); // callback for when new data arrives serial.on("error", serialError); // callback for errors serial.on("close", portClose); // callback for the port closing serial.list(); // list the serial ports serial.open(portName); // open a serial port } // get the list of ports: function printList(portList) { // portList is an array of serial port names for (let i = 0; i < portList.length; i++) { // Display the list the console: print(i + " " + portList[i]); } } function serverConnected() { print("connected to server."); } function portOpen() { print("the serial port opened."); } function serialEvent() { // read a string from the serial port // until you get carriage return and newline: let inString = serial.readLine(); print(inString); if (inString.length > 0){ windVal = map(inString, 0, 1023, -3, 3); } } function serialError(err) { print("Something went wrong with the serial port. " + err); } function portClose() { print("The serial port closed."); } function draw() { background(255); wind.x = windVal; 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 - 25){ serial.write(255); } else serial.write(0); if (position.y > height-mass/2) { velocity.y *= -0.9; // A little dampening when hitting the bottom position.y = height-mass/2; } } 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.x=width/2; position.y=-mass; velocity.mult(0); } }
Arduino Code:
Arduino code void setup() { Serial.begin(9600); pinMode(2, OUTPUT); while (Serial.available() <= 0) { Serial.println("0"); // send a starting message delay(300); // wait 1/3 second } } void loop() { while (Serial.available() > 0) { // read the incoming byte: int inByte = Serial.read(); analogWrite(2, inByte); int WindPower = analogRead(A0); Serial.println(WindPower); } }