Serial Communication Exercises

Exercise 1

Moving ellipse with potentiometer

p5JS Code

let serial; // variable to hold an instance of the serialport library
let portName = "/dev/cu.usbmodem1412201"; // fill in your serial port name here
let xPos=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, width/2, 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 sensorVal = inString;
    xPos = map(sensorVal, 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.");
}

Arduino Code

void setup() {
  Serial.begin(9600);
  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();
    int sensorValue = analogRead(A0);
    Serial.println(sensorValue);
  }
}

Exercise 2

Control brightness with MouseX position

p5JS Code

let serial; // variable to hold an instance of the serialport library
let portName = "/dev/cu.usbmodem1412201"; // fill in your serial port name here
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(220);
}

// 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(ledPin, 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(ledPin, inByte);
    Serial.println("0");
  }
}

Exercise 3

The light turns on and off with the ball bouncing

Control wind with a potentiometer

p5JS Code

let serial; // variable to hold an instance of the serialport library
let portName = "/dev/cu.usbmodem1412201"; // fill in your serial port name here

let velocity;
let gravity;
let position;
let acceleration;
let wind = 0;
let drag = 0.99;
let mass = 50;
let hDampening;
let onOff = 0;

function setup() {
  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

  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);
  hDampening = map(mass, 15, 80, 0.98, 0.96);
}

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

    //     // turning on/off led
    if (onOff) {
      onOff = 0;
    } else {
      onOff = 1;
    }
    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);
}

// 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 keyPressed() {
  if (key == " ") {
    mass = random(15, 80);
    position.y = -mass;
    velocity.mult(0);
  }
}

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();
  if (inString != "") {
    if (inString > 8 && inString < 12) {
      wind.x = 0;
    } else if (inString >= 12) {
      wind.x = 1;
    } else if (inString >= 0 && inString < 12) {
      wind.x = -1;
    }
  }

  serial.write(onOff);
}

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(ledPin, 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();

   int pmValue = analogRead(A0);
   int wind = map(pmValue, 0, 1023, 0, 20);
   
   digitalWrite(ledPin, inByte);
   Serial.println(wind);
  }
}

 

Yunho Lee – Final Project Idea

I am planning to make a game that can be controlled through joysticks, sensors, and buttons. My final goal is to make an RPG type of map where characters can move around the map, farm items, and use those items to solve a specific puzzle. Solving the puzzle will require interaction with the sensors (for example, blows off dust by blowing at the sound sensor) The main goal of the game will be to escape from a room, by finding all the clues and items.

It will require several OOP for stages, characters,  items, UIs, and so on.

I hope I can make a “save” function, but I am not sure if I will have time to accomplish that.

final project idea

for the final project, I have a few ideas, some are more creative than practical use, I’ll mention two but a third could be further working on the instrument from last week.

the first is recreating something like this:

or even this:

using p5js to create the design, and then using Arduino to change the color with someone’s body or some sort of control and if possible I would like to sync the design with music. the idea is to have someone’s body create a generative art piece, perhaps it could even be to create the art with their motion.

the second idea is to recreate a game like polar bowler

where I would use p5js for a screen and Arduino for the game’s controls and sound, which I could also use with p5js.

i could perhaps combine and use the other design as a background.

Final Project Proposal – VS Fighting Game

Idea

My PSP consumed a large chunk of my childhood, specifically, a game called Tekken: Dark Resurrection. I want to recreate this game, except this time instead of entering commands through buttons and joysticks, like in the original, I want to use proximity and pressure sensors linked to Arduino and computer vision on p5.js. My game will be played with physical movements by the user, which is captured using sensors and camera. Specific movements will correspond to logical fighting moves (eg. punches, kicks, blocks).

Possible Challenges:
  • Computer being able to capture user movements
  • Computer being able to distinguish between user movements
  • In-game mechanics such as damage system and avatar animations

 

Tekken 5: Dark Resurrection, it was so good, even on the limited hardware of the PSP, what do you think of it? : r/Tekken

Final Project Idea

I would like to attempt at incorporating science into my final project by making an interface where the user can pick and choose different genes to swap in and out, and through that they’ll learn what diseases come about with these mutations. I see a 3D DNA strand that is rotatable, perhaps with the use of LEDs to specify various locations that one could interact with and have a screen pop up on the side with the results of their interaction.

This project would require a lot of scientific research, just because I think that the specificity of a disease’s genetic origin may not be super clear. But I want the user interaction to be a distance away from the projected screen to give it a more future scientific feel. I also want to be able to explore the use of cameras and with that have the user’s entire body interact with the piece instead of just one part.

Final Project Idea

For my final project, I was thinking about a way of making an interactive basketball shooting game in which the user would control the direction and power using sensors.

The game would have a basketball spawn at different distances and angles from the hoop and there would be an arrow in front of the ball controlled by a potentiometer to allow the user to aim at the hoop correctly. The more shots the user makes the more difficult the shots will be.

There would also be a power meter on the screen which could be controlled by a flex sensor so that the user can bend it until the right power required is reached. The basketball will start moving when the flex sensor is released by the user, to simulate a flick.

The game would have a timer that runs and the user would have to score as much as possible within this time.

 

Dev: Final project proposal

Ideation 

For the final project, I want to build a car that will feature both autopilot mode and manual driving mode. The user will have the choice to decide which mode they want to choose to drive the car.

If the user chooses the autopilot mode, the car will be able to move around and avoid all obstacles with (obstacle detections) and also be able to drive automatically on a predefined course/ path. Whereas for the manual mode, the user will be able to control the car through p5.js using the arrows. (perhaps connect it to a controller)

I also plan to build a driving course that the user can drive in and test the autopilot mode. I’m planning to have a line following feature where the car will follow the line on the road. (Even make decisions where possible, Maybe that is too complicated?)

P5.js

The p5.js will show a window with its instructions and feature for the 2 modes (autopilot mode and manual mode) allowing the user to choose their option. The arrows will allow the user to control the car’s movement and drive the car around the course

Arduino Components needed:

  • 2 IR Range Sensors
  • 4 wheels
  • 1 ultrasonic sensor
  • 1 Servo
  • 1 LDR
  • L298N motor driver 
  • Battery DC
  • few LEDs

Final Project Idea

Visual Generative Art (?)
I loved one of the class examples of how sound was generated upon touching a screen. This led to me planning to create a generative art piece that detects hand motions using Computer Vision/ Arduino’s Camera to generate designs on P5js. I am mainly concerned about finding the right detector.
If this doesn’t work, I plan on using pressure sensors covered with a white/black screen upon which the P5js art would be projected. Touching this screen would generate designs (flowers?)

Nicholas- Final Project Concept

Idea

I wanted to make something that feels like it has real world consequences, so my idea for the final project is a real life stock trading application. I want to create an integration that creates an interactive experience for the end user.

Implementation

There are 3 components to this idea: A physical method to trading stocks, a display of profits, and a web server that connects the integration to the outside world. The user should be able to choose to either buy Stock A, Stock B, or skip the purchase. There will also be a button to liquidate all stocks and turn a profit.

Arduino Implementation:

  1. A potentiometer that the user turns to select stock A, stock B, or skip
  2. A button that the user can press to liquidate stocks
  3. A servo that pushes down an airhorn upon liquidation

P5js Implentation:

  1. A display with the text of Stock A, B or skip
  2. A Graph with current value of stocks
  3. Text showing how much liquid and stock money the user has

Web Server Implementation:

  1. Send new stock data to P5JS
  2. Send wallet data to p5js
  3. Send purchase requests to Alpaca Stock API

The catch

its real money that I run away with at the end of the semester.