Serial Communication Exercises

Marium and I worked together in order to complete these tasks.

Exercise 1 – make something that uses only one sensor on Arduino and makes the ellipse in processing move on the horizontal axis, in the middle of the screen, and nothing on Arduino is controlled by processing

We made it so whenever the potientometer is rotated, the ball moves along the horizontal axis, in the middle of the screen.

Processing code:

let serial; // variable to hold an instance of the serialport library
let portName = "/dev/cu.usbmodem14501"; // fill in your serial port name here
let xPos=0;
let yPos=0;
let onOff=0;
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);
  ellipse(xPos, yPos, 50, 50); // draw the circle
}
// 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();
 
  //check to see that there's actually a string there:
  if (inString.length > 0) {
    let sensors = split(inString, ","); // split the string on the commas
    if (sensors.length== 2) {
      // if there are three elements
      
      xPos = map(sensors[1], 0, 1023, 0, width); // element 0 is the locH
      yPos = height/2; 
      // yPos = map(sensors[1], 550, 250, 0, height); // element 1 is the locV
    }
  }
  serial.write(onOff);
}
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);
while (Serial.available() <= 0) {
Serial.println("0"); // send a starting message
delay(300); // 
}
}
void loop() {
while (Serial.available() > 0) {
// read the incoming byte:
int inByte = Serial.read();
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
}
}

Exercise 2 – make something that controls the LED brightness from processing

We made it so when the mouse hovers on the left side of the canvas, the LED is dim and then the far right of the canvas is the brightest. It works in a gradient, so the point in the middle of the canvas is semi-bright.

Processing code:

let serial; // variable to hold an instance of the serialport library
let portName = "/dev/cu.usbmodem14501"; // fill in your serial port name here
let xPos = 0;
let yPos = 0;
let onOff = 0;
let brightness = 0; 
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);
}
  
// 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();
  
  brightness = mouseX;
  let mapped = map (brightness, 0, 640, 0, 255);
  serial.write(mapped);
}
function serialError(err) {
  print("Something went wrong with the serial port. " + err);
}
function portClose() {
  print("The serial port closed.");
}

Arduino code:

int ledPin = 5;
void setup () {
  Serial.begin(9600);
  pinMode(5, OUTPUT);
  while (Serial.available() <= 0) {
    Serial.println("0,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(ledPin, inByte);
    Serial.println("0");
  }
}

Exercise 3 – take the gravity wind example below 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

We found this exercise the most difficult. We took it step by step by first ensuring we have all the important elements for serial communication, including changes in the index.html. Then we worked on getting the LED to light up when the ball bounces and then turn off. Then we worked on the wind part and controlling with the potentiometer.

Processing code:

let serial; 
let portName = "/dev/cu.usbmodem14401"; 
let velocity;
let gravity;
let position;
let acceleration;
let wind;
let drag = 0.99;
let mass = 50;
let bounce;
let xPos=0;
let yPos=0;
let onOff=0;
// for port connection and handshake 
function setup() {
  createCanvas(640, 360);
  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();
  //xPos = map(inString, 0, 1023, 0, width);
 
  //check to see that there's actually a string there:
  if (inString.length > 0) {
    xPos = map(inString, 0, 1023, 0, width);
  }
  serial.write(onOff);
}
function serialError(err) {
  print("Something went wrong with the serial port. " + err);
}
function portClose() {
  print("The serial port closed.");
}
  
  // code for the actual bouncing, wind, etc. 
  
  noFill();
  position = createVector(width/2, 0);
  velocity = createVector(0,0);
  acceleration = createVector(0,0);
  gravity = createVector(0, 0.5*mass);
  wind = createVector(0,0);
  bounce=map (mass,15,80,.80,.80);
}
function draw() {
  background(255);
  if (!keyPressed){
    
    console.log(xPos);
    velocity.x*=bounce;
  }
  
  wind.x = map(xPos, 0, width, -1, 1);
  
  applyForce(wind);
  applyForce(gravity);
  velocity.add(acceleration);
  velocity.mult(drag);
  position.add(velocity);
  acceleration.mult(0);
  fill(255);
  ellipse(position.x,position.y,mass,mass);
  
  
  if (position.y > height-mass/2 - 40) {
    if (velocity.y > 1) {
      serial.write(255);
    }
    
    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.y=-mass;
    velocity.mult(0);
  }
}

Arduino Code:

void setup() {
  Serial.begin(9600);
  pinMode(5, OUTPUT);
  while (Serial.available() <= 0) {
    Serial.println("0,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(5, inByte);
    int sensorValue = analogRead(A0);
    Serial.print(sensorValue);
    Serial.println();
  }
}

Video 

https://youtube.com/shorts/fyPlnoR_qQQ

Leave a Reply