Week 12 – Final Project: Whac-A-Note

Concept:

With the original foundation of the whac-a-mole system of arduino circuit being completed by a hammer whacking the mole, I added an extra element of music notes to make this a rhythm game. Users must hit the several moles (I am currently thinking of six) which will be spread in three different directions: left, right, and center. The interface will show a series of notes, each correlated to a specific mole and the user must hit the mole at a correct timing and order in order to score points. Missing the mole or hitting the incorrect mole will deduct points and if too many notes are missed, the user will lose the game.

Implementation:

-Arduino:

The arduino board will have an incomplete series of circuits that can be connected only when the hammer contacts the mole which will be made with some conductors and attached to wires. When the circuit is connected, an LED bulb will be lit and will send a signal to p5js some data that the mole was hit. It will also connect the buzzer which will then play a specific note correlated to the mole.

-P5js:

The p5js will show the interface of starting the game and the in-game node sequences. The node will be going across from one part of the screen to the other side, and when the node reaches a certain point in the screen, users must hit the correlating mole at that time in order to score points. I still haven’t figured the exact method of p5js loading this sequence of node. My current two possible ideas are either it is generated using some random function, or it would load a preloaded existing tune. During the actual implementation stage, I plan to try out both and see which is better to decide this.

Potential issues / Future work:

Like I mentioned before, I need to decide on the methodology of the loading of the sequence of nodes and part of this problem is feasibility. Since the core factor of a rhythm game is the music of it, I need to make sure that this is executed well during implementation. I also need to confirm that the serial communication between p5js and arduino can be done by completing a circuit via connecting wires. Other than that, I’m a bit worried about the physical layout of the whole project with long wires and several objects big and strong enough to withstand some whacking… But I am very excited to work on this project since I really love rhythm games and I also love some whacking.

Final project proposal: Neuro-Jump

Background :                                                                                                                                      After we learnt about the future of design and having read the rant on the future of interaction design by bret vector. I started thinking deeper about the concept of interactivity and how we as the future generation of designers and technologists can design innovative and authentic interaction experiences that differ from the “glass screen”.

Solution:                                                                                                                                     Using a different interactive gadget that allows the user to immerse in a designed universe without using their fingers or hands!

Using electrodes would allow the detection of EMG signals of the muscle and that will make it possible to use “muscle flexes” as input that can be detected, transferred and used as main input in the final product..         Concept:                                                                                                                                     My idea is to create an interactive P5 video game that is based on a retro style game such as ( Racing games, Tetris or even flappy bird). I would like to use the Backyards brain product( Arduino+Shield+ Electrodes) and combine that with p5, where the video game/ virtual environment will be created. I might also develop another game that some how incorporate muscle flexes to commit some sort of action in the game/ virtual environment.

Arduino:                                                                                                                                            Other than the Arduino circuit, shield and the electrodes already provided. I would like to incorporate a buzzer and LEDs that will allow to add a visual and a sonic signals to the game making it more immersive and adding another layer to the game by including sound and light signals that depend on the either the strength or the length of the flex. The EMG detected signals will then be transferred by the Arduino to the p5 which will cause certain actions to happen in the game

P5:                                                                                                                                                        As I mentioned above the P5.js side of the project will probably be a game based on either a retro style game or another game that requires repetitive action such as flappy bird or bounce ball.

Issues:                                                                                                                                                   I would like to make the game very responsive so I need to check that the hardware and the P5.js game are both at tune so that the game does not lag when the EMG signal has not been detected.

 

Week 12 – Final Project Proposal Revision

Concept

I want to create an interactive game where the user will use the joystick to move the avatar and find the lost objects. The idea for this project came from this interactive web-based experience called The Healing Forest. Initially, I was thinking of creating a Fruit Ninja game as a group project because I wanted to make the user interact with the game using the camera  but then I figured out it would lack the physical computing part so I decided to settle on the idea of using a joystick. 

  • There will be a forest (or another setting) where the user will search for lost items by moving the avatar
  • The flashlight will illuminate the avatar’s path wherever it will go and other parts of the background will be dark

Arduino

Input:

  • Joystick will be used to control the movement of the avatar
  • Buttons
    • Will be used to switch between themes
    • Will be used to jump from obstacles such as stone, etc.

Output:

  • LEDs will be used to indicate a warning when obstacle is nearby

p5.js

Output:

  • p5.js will provide a gaming interface by displaying avatar, setting, etc. It will receive the x and y coordinates from the joystick and will parse them to the gaming screen to move the avatar accordingly. It will also send a signal to the Arduino when an obstacle is nearby to light up the warning LED.

Further Improvements

I want to add some stages before and after locating objects, but I’m still brainstorming on those. One of the ideas is to add another stage where the user would need to sort/place the found objects based on different attributes or some output could be displayed based on the items the user collects.

Final Project: Report #2 – Finalized Concept

Finalized Concept:

My finalized concept is to construct an Arduino Radar Sensor and make a game out of it! Essentially, I would have a bunch of random items all over the desk and it’s the user’s job to find the treasure in this pile of mess within a certain amount of time.

The only threat is, there is a radar constantly moving back and forth around a servo motor trying to detect the user. If the radar detects the user, and they are within firing range they would lose! However, if the user manages to find the correct piece of treasure before being noticed and within the time limit then they win! They would compete with each other to try and get the treasure at the fastest time possible!

Arduino:

For the Arduino, I was going to implement a Servo motor. I would make this motor move back and forth at a fixed speed and the angle at which the Servo motor rotates would be sent to p5js which would route to the visual representation of the radar.

Then I would mount the ultrasonic sensor onto the Servo motor. The distance that the user is from the sensor would be sent to p5js and translated in the range of the map and be drawn onto the radar. If the user is detected within some range then it would trigger the losing mechanism.

For the losing mechanism, I though I could incorporate a solenoid and it would essentially knock some mini model of a human, indicating they got shot and basically lost.

Additionally, I would add a pressure sensor on the table for the treasure. As the treasure rests on the table, it will apply pressure on the sensor, so when it’s picked up, the sensor has less weight so it implies the user picked it up and won! This would signal p5js to stop the radar and stop the timer and indicate that the user has won.

I was also thinking of adding a potentiometer to the circuit to try and challenge the user. This potentiometer would change the speed of the servo motor. So that way it maps the area even faster making it more challenging not to be noticed.

P5js:

For p5js, I wanted to simply create a visual for the radar, sort of like the image I posted last time:

So, as the Servo motor rotates, the line rotates around the circle as well. The ultrasonic sensor would produce some point on this radar to detect a person is there. If they are outside the range then it will not map.

Additionally, I wanted to also draw a timer somewhere so that the user can detect how much time they have left.

I also plan to add some music and some cool radar sound effects through p5js to keep the experience a little more fun and realistic.

Afterthoughts:

I may utilize the createCapture() function on p5js (camera) as that may be useful as a backup or to even double down on the scanning. Perhaps, I could use this to detect the user and use the ultra sonic sensor to detect how far they are. Maybe I could use the camera to communicate with Arduino to move the laser pointing or the nerf gun or whatever to point exactly at their body or something so it doesn’t get in their eye.

 

Week 12: Final Project Proposal Revision

Concept

In light of the complexity of my initial plan in making a canvas made of LEDs to let the user draw, I decided to simplify my Final project. The former idea required a lot of LEDs which can not all be supported simultaneously by the Arduino Uno. Thus, I am restricting to a small number of LEDs to bring my project to fruition.

I am planning on two things:

  1. Visual Memory: I will use a 4×4 LED grid to blink LEDs in some pattern. The user is then required to repeat the pattern onto the p5Js console in order to win. This game will increase in difficulty as the user clears the levels.
  2. Text Marquee: On the same 4×4 LED grid, I will create a test marquee which would show text that the user enters onto the p5Js console.

Implementation

Arduino

All the LEDs will be controlled by the Arduino, however, the behaviour of the Arduino will change between the two modes:

  1. For the memory game, the Arduino will generate random co-ordinates which correspond to the LED, and send them to p5Js. It will then wait for further command that instructs it to either repeat the pattern again, or create a new pattern. The difficulty level of the game can be changed with a button or a sensor hooked up to the Arduino.
  2. As for the text marquee, the Arduino will wait for the text the user types and converts it to an array of co-ordinates corresponding to the LED which should be lit up to display that text/letter.
P5Js

The p5Js will incorporate all the user interactivity:

  1. It will provide with a gaming interface for the memory game. It receives the co-ordinates of the LED which it will parse to its gaming screen to register if the user is correct or wrong. It will also send a signal to the Arduino to change the pattern or repeat the pattern
  2. For the text marquee, the p5Js is used as an input device for sending text to the Arduino to later be printed on the LEDs.

 

Week 11 – Final Project Proposal

Sound Canvas – a sound visualizing musical instrument 

Concept

A sound visualization instrument that allows you to both make music and visualize it on the p5 screen. Someone who cannot hear can see the visualization of a sound, and hence will enjoy the instrument in a different way. Similarly, someone who cannot see can hear the sounds, and hence enjoy it differently too. Overall, the Sound Canvas concept presents a unique and accessible approach to music creation and enjoyment, integrating sound visualization, interactivity, and artistic elements.

  • 3 different tone types (fast/medium/slow) – change according to brightness of light in the room – dimmer the light, calmer the tone
      • Light sensor to specify ranges of brightness in which each tone plays so that if brightness within a certain range, a certain type of music will be played in the background 
      • The tone might be played on the buzzer or the computer (TBD)
      • The background color of the p5 screen will represent the tone type, for example, fast will be represented by a specific color such as red
  • 3-6 Buttons to play additional sounds/notes when pressed
      • Each button will have a specific sound associated to it
      • When pressed, the associated sound will be played (added on top of the background music)
      • The sound will either be played on the computer or the buzzer (TBD)
      • Whenever a button is pressed, an LED will light up as feedback
  • Tone volume/pitch can be changed using either a distance sensor or a potentiometer (TBD)
      • volume/pitch displayed on the p5 screen
  • Sound Visualization on the p5 screen with shapes/patterns/colors
      • Inspiration: https://patatap.com/ 
      • Every button when pressed will also display a pattern/color/shape associated to the button on the p5 screen

Arduino

For the Arduino end, as mentioned, sensors and buttons will be arranged in a box. I might integrate a motor as well that moves something, for example a music record, according to the sensor values.

Inputs and Outputs:

    • From light sensor – will send it to p5
    • From distance sensor/potentiometer – will send it to p5/use to control volume of buzzer
    • From buttons – will light up corresponding LEDs and send to p5 
    • From P5 – indication if instrument turned on/off from p5 screen – use to turn corresponding power LED on/off
    • LEDs to indicate if instrument is on or if any button is pressed (feedback)
    • Buzzer (to play background sound/button sounds)

P5

The P5 screen will first present instructions on how to use the instrument. Once the experience begins, the screen will show a visualization of the music created using input from the sensors. This way, every user will, in a way, create an art piece that is theirs. So, not only can the user change their motion/touch to change the output sound but to also add something to their sketch. Also, P5 will have some buttons that, when pressed, will send output to Arduino – for example LEDs light up when Start button is pressed or if sensor value exceeds a threshold or maybe different tone/sound/volume options available on screen that will be used to change the output melody.

Inputs and outputs

    • Use light sensor value from Arduino to change tone type and screen background color
    • Use inputs from buttons that Arduino sends to draw shapes/patterns on the screen
    • Display details such as volume/tone type in the corner of the screen

Final Project Proposal – Radar

Concept

As someone who is interested in physical computing, I have been inspired by previous DIY radar projects that use ultrasonic sensors and servo motors to detect objects in real-time. However, to make my project more original and challenging, I will use P5.js, a JavaScript library for creative coding, instead of Processing, which is commonly used in similar projects. By incorporating P5.js, I hope to create a unique and interactive radar system that combines hardware and software components. The concept of the project is to build a radar system that can detect objects and display their location and distance in real-time. The system will consist of an ultrasonic sensor, a servo motor, an Arduino board, and a computer running a P5.js program.

Technical aspect

For the Arduino program, I plan to use the ultrasonic sensor to detect the distance of objects in front of it. I will then use a servo motor to rotate the sensor and sweep it across a 180-degree arc. At each angle, the program will read the distance measurement and send this data to the P5.js program over the serial port. Additionally, I plan to control an LED to indicate the presence of an object within a certain distance threshold.

The P5.js program will receive the data from the Arduino program and use it to display the location of the detected object on a radar screen. Using polar coordinates, the program will map the distance and angle of each object and display them as a dot on the radar screen. The program will also include a graphical user interface (GUI) that allows me to adjust the distance threshold and other parameters.

To communicate between the Arduino and P5.js programs, I will send data over the serial port. The Arduino program will send the angle and distance of each detected object, as well as the state of the LED. The P5.js program will use this data to update the radar screen and the GUI.

Arduino + p5js in class exercises (Saamia and Khadija)

Exercise 1:

For our first exercise, we used a light sensor as the analog input value for the movement of the ellipse on the p5js screen. The rVal from Arduino is used to plot the x coordinate of the ellipse.

p5js code:

//exercise 1 p5js
let rVal = 0;
let alpha = 255;

function setup() {
  createCanvas(640, 480);
  textSize(18);
}

function draw() {
  
  if (key == " ") 
  {
    // important to have in order to start the serial connection!!
    setUpSerial();
  }
  
  // one value from Arduino controls the background's red color
  background(map(rVal, 0, 1023, 0, 255), 255, 255);

  // the other value controls the text's transparency value
  fill(255, 0, 255, map(alpha, 0, 1023, 0, 255));

  if (!serialActive) 
  {
    text("Press Space Bar to select Serial Port", 20, 30);
  } else 
  {
    ellipse(rVal/2,240,100,100);
  }
}

function keyPressed() {
  if (key == " ") 
  {
    // important to have in order to start the serial connection!!
    setUpSerial();
  }
}

// This function will be called by the web-serial library
// with each new line of data. The serial library reads
// the data until the newline and then gives it to us through
// this callback function
function readSerial(data) {
  ////////////////////////////////////
  //READ FROM ARDUINO HERE
  ////////////////////////////////////

  if (data != null) 
  {
    // make sure there is actually a message
    // split the message
    let fromArduino = split(trim(data), ",");
    // if the right length, then proceed
    if (fromArduino.length == 2) 
    {
      // only store values here
      // do everything with those values in the main draw loop
      
      // We take the string we get from Arduino and explicitly
      // convert it to a number by using int()
      // e.g. "103" becomes 103
      rVal = int(fromArduino[0]);
      alpha = int(fromArduino[1]);
    }
  }
}

Arduino Code:

//exercise 1 arduino

void setup() {
  Serial.begin(9600); 
  pinMode(LED_BUILTIN, OUTPUT);
// start the handshake
  while (Serial.available() <= 0) 
{
    Serial.println("0,0"); // send a starting message
    delay(300);            // wait 1/3 second
  }
}
void loop() 
{
  // wait for data from p5 before doing something
    while (Serial.available()) 
{
    digitalWrite(LED_BUILTIN, HIGH); // led on while receiving data

  // Read sensor value
  int sensorValue = analogRead(A0);
   Serial.print(sensorValue);
  // Map sensor value to screen width
  int screenValue = map(sensorValue, 0, 1023, 0, 800);

  // Send mapped value to p5.js
  Serial.println(screenValue);
  delay(50); //    for stability
}
digitalWrite(LED_BUILTIN, LOW);
}

Exercise 2:

In the second exercise, we implemented a slider to adjust the brightness of an LED using Arduino. The current position of the slider, accessed through slider.value(), was stored as the variable “brightness”, and subsequently transmitted to Arduino as the new brightness level for the LED.

p5js Code:

//exercise 2 p5js

let brightness;
function setup() 
{
  createCanvas(640, 480);
  textSize(18);
  slider = createSlider(0, 255, 0); 
  slider.position(85, 140);
}

function draw() 
{
  if (!serialActive) 
{
    text("Press Space Bar to select Serial Port", 20, 30);
  } 
else 
{
    text("connected", 20, 30);
    brightness = slider.value();
  }
  brightness = slider.value();
  //readSerial();
  if(mouseIsPressed)
{
    readSerial();
  }
}

function mouseIsPressed()
{
  readSerial();
}

function keyPressed() {
  if (key == " ") 
{
    // important to have in order to start the serial connection!!
    setUpSerial();
  }
}
function readSerial(data) 
{
    console.log(brightness);
    let sendToArduino = brightness+"\n";
    writeSerial(sendToArduino);

}

Arduino Code:

//exercise 2 arduino

int ledpin = 5;
void setup() {
  // Start serial communication so we can send data
  // over the USB connection to our p5js sketch
  Serial.begin(9600);

  // Outputs on these pins
  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) 
{
    Serial.println("0");
    delay(300);
  }
}

void loop() {
  // wait for data from p5 before doing something
  while (Serial.available()) 
{
    int brightness = Serial.parseInt();

    if(Serial.read() == '\n')
{
      //Serial.println(brightness);
      analogWrite(ledpin, brightness);
    }
    analogWrite(ledpin, brightness);
  }
 
}

Exercise 3:

For this last exercise, we used an ultrasonic sensor to sense the distance and use this variable to change the wind movement in the p5js movement of the ball. The data values of the sensor ranged from 0 to around 3000 and therefore for any value below 1000 the wind blew from left to right and if the value was above 1000 wind and the ball moved towards the left.

p5js Code:

//p5js exercise 3
//declare variables
let velocity;
let gravity;
let position;
let acceleration;
let wind;
let drag = 0.99;
let mass = 50;
let value = 1401;

function setup() 
{
  createCanvas(640, 360); //create canvas
  noFill();
  position = createVector(width/2, 0);
  velocity = createVector(0,0);
  acceleration = createVector(0,0);
  gravity = createVector(0, 0.5*mass);
  wind = createVector(0,0);
}

function display()
{
  text("Press Space Bar to Start Serial Port", width/2 - 109, height/2 - 5);
}

function draw() {
background(255);
if (serialActive) //if the serial is active
{
  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) //if the ball touches the bottom
      {
      velocity.y *= -0.9; // A little dampening when hitting the bottom
      position.y = height-mass/2;
      }
}
else //if serial not active
  {
  fill(0);
  display();
  }
}

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 (key==' ')
  {
  setUpSerial();
  }

if (keyCode == UP_ARROW) //if up arrow is pressed
  {
  mass=random(15,80);
  position.y=-mass;
  velocity.mult(0);
  }
}

function readSerial(data) //call back function
{
let sendToArduino = value + "\n"; //sends value to Arduino with \n added
writeSerial(sendToArduino); //write to Arduino
  if (data != null) //if some information is received
  {
  console.log(data);
    if (data < 1000) //if the distance is less than 1000
    {
    wind.x = 1; //the wind blows to the right
    }
    else if (data > 1000) //if the distance is more than 1000
    {
    wind.x = -1; //the wind blows to the left
    }
  }
}

Arduino Code:

//exercise 3 arduino code
int value = 0;
const int trigPin = 7; //trig pin of Sensor
const int echoPin = 13; //echo pin of Sensor
int distance = 0; //distance data from sensor

void setup()
{
Serial.begin(9600); // Start serial communication at 9600 baud

//set the sensor pins as output and input 
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

while (Serial.available() <= 0)
{
  Serial.println(1400);
}
}

void loop()
{
//wait for p5js
while (Serial.available())
{
sensorReading(); //reading data from sensor
value = Serial.parseInt(); //parsing from the serial written data from p5js
}
}

//to read the sensor and find distance
void sensorReading()
{
//Send a short low pulse
digitalWrite(trigPin, LOW);
delay(2); //delay to avoid complications
digitalWrite(trigPin, HIGH); //sends a high pulse for 10 microseconds
delay(10);
digitalWrite(trigPin, LOW); //turn off the ping pin
distance = pulseIn(echoPin, HIGH); //Measure the duration of the ultrasonic pulse and calculate the distance
Serial.println(distance); //print the serial from distance
}

Video:

week-15 exercise + final idea

Exercise 1: The value obtained from the potentiometer makes the ellipse in p5 move on the horizontal axis, in the middle of the screen, and nothing on Arduino is controlled by p5.

p5.js

let rVal = 0;
let xPos = 0;
let rad = 50;


function setup() {
  createCanvas(400, 400);
  textSize(18);
}  

function draw() {
  background(255);

  if (!serialActive) {
    text("Press Space Bar to select Serial Port", 20, 30);
  } else {
    text("Connected", 20, 30);
    
    xPos = map(rVal, 0, 900, 0, 400);
    // Print the current values
    text('rVal = ' + str(rVal), 20, 50);
    ellipse(xPos, 200, rad, rad);
    text('xPos = ' + str(xPos), 20, 70);

  }
  
}

function keyPressed() {
  if (key == " ") {
    // important to have in order to start the serial connection!!
    setUpSerial();
  }
}


function readSerial(data) {
  ////////////////////////////////////
  //READ FROM ARDUINO HERE
  
  if (data != null) {
    
    let fromArduino = split(trim(data), ",");

    if (fromArduino.length == 1) {
      rVal = fromArduino[0];
    }

    //////////////////////////////////
    //SEND TO ARDUINO HERE (handshake)
    //////////////////////////////////
    let sendToArduino = 1 + "\n";
    writeSerial(sendToArduino);
  }
}

Arduino

void setup() {
  Serial.begin(9600);
  //pinMode(LED_BUILTIN, OUTPUT);

//start the handshake
  while (Serial.available() <= 0) {
    //digitalWrite(LED_BUILTIN, HIGH);
    Serial.println("0,0"); // send a starting message
    delay(300);
    //digitalWrite(LED_BUILTIN, LOW);
    delay(50);

  }
}

void loop() {
  // wait for data from p5 before doing something
  while (Serial.available()) {
     //digitalWrite(LED_BUILTIN, HIGH);
    int isMoving = Serial.parseInt();
    if (Serial.read() == '\n') {
      int photoVal = analogRead(A0); 
      delay(5);
      Serial.println(photoVal);
   }
  }
  //digitalWrite(LED_BUILTIN, LOW);
}

Exercise 2: Change the LED brightness by moving the ellipse on p5.js left or right.

let left = 0;
let right = 0;
let brightness = 0;
let rad = 50;

function setup() {
  createCanvas(400, 400);
  textSize(18);
}  

function draw() {
  background(255);

  if (!serialActive) {
    text("Press Space Bar to select Serial Port", 20, 30);
  } else {
    text("Connected", 20, 30);

    //the x position of the cirlce will control the brightness of the LED
    xPos = mouseX;
    ellipse(xPos, 200, rad, rad);
    brightness = map(xPos, 0, 400, 0, 255);
    brightness = int(brightness);
    
    // Print the current values
    text('Brightness = ' + str(brightness), 20, 60);
    text('xPos = ' + str(xPos), 20, 90);

  }
  
}

function keyPressed() {
  if (key == " ") {
    // important to have in order to start the serial connection!!
    setUpSerial();
  }
}


function readSerial(data) {
  ////////////////////////////////////
  //READ FROM ARDUINO HERE
  ////////////////////////////////////
  
  if (data != null) {
    
    let fromArduino = split(trim(data), ",");
    if (fromArduino.length == 1) {
      // we're not getting input from arduino
    }
    //////////////////////////////////
    //SEND TO ARDUINO HERE (handshake)
    //////////////////////////////////
    let sendToArduino = brightness + "\n";
    writeSerial(sendToArduino);
  }
}

Arduino

int ledPin = 5;

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(ledPin, OUTPUT);
  // 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') {
      delay(5);
      Serial.println("1");
    }
    analogWrite(ledPin, brightness);
  }
  digitalWrite(LED_BUILTIN, LOW);
}

Exercise 3: Alter the gravity wind example to make the LED light up and turn off every time the ball bounces , and control the wind using potentiometer.

p5.js

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

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);
}

function draw() {
  background(255);
  
  if (!serialActive) {
    text("Press Space Bar to select Serial Port", 20, 30);
  } else {
    applyForce(wind);
    applyForce(gravity);
    velocity.add(acceleration);
    velocity.mult(drag);
    position.add(velocity);
    acceleration.mult(0);

    fill(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;
      isBouncing = 1;
    } else {
      isBouncing = 0;
    }
  }
}

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 (key == " ") {
    // important to have in order to start the serial connection!!
    setUpSerial();
  } else if (key == "ENTER") {
    mass = random(15, 80);
    position.y = -mass;
    velocity.mult(0);
  }
}

function readSerial(data) {
  if (data != null) {
    // make sure there is actually a message
    // split the message
    let fromArduino = split(trim(data), ",");
    // if the right length, then proceed
    if (fromArduino.length == 1) {
      // only store values here
      // do everything with those values in the main draw loop
      let potValue = int(fromArduino[0]);
      
      //change the wind based on the potentiometer value
      wind.x = map(potValue, 0, 900, -5, 5);
    }

    //////////////////////////////////
    //SEND TO ARDUINO HERE (handshake)
    //////////////////////////////////
    let sendToArduino = isBouncing + "\n";
    writeSerial(sendToArduino);
  }
}

Arduino

int ledPin = 5;
const int potPin = A0;

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(potPin, INPUT);
  
  // 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
    digitalWrite(ledPin, LOW);
    int isBouncing = Serial.parseInt();
    if (Serial.read() == '\n') {
      int potValue = analogRead(potPin);
      delay(5);
      Serial.println(potValue);
    }
    // Set LED brightness based on whether bouncing.
    if (isBouncing == 1) {
      digitalWrite(ledPin, HIGH);
    } else {
      digitalWrite(ledPin, LOW);
    }
  }
  digitalWrite(LED_BUILTIN, LOW);
}

Final Project Idea

Concept

For the final project, me and Sanjana are thinking of creating a Virtual Fruit Ninja. We want to take the concept of this mobile game to the next level by creating an immersive and interactive game experience allowing players to slice fruits virtually with their hands! The following project and our love for the game fruit ninja is the inspiration for this project: (https://naziafakhruddin.medium.com/gesture-controlled-flower-art-with-ml5-js-posenet-and-p5-js-61b98fa3fc68)

For this project, we are thinking of using the ml5 library, built on top of Tensor.js, and the PoseNet function to accurately track the position of the player’s hand in real time through the camera video. The goal is to create a seamless integration between the player’s hand movements and a virtual sword or blade in the game, simulating the experience of slicing real fruits with a knife.

The game mechanics will be intuitive and engaging. Players can wave their hands across the screen to slice the fruits as they appear, and the PoseNet function will accurately track their hand movements in real time, providing precise and responsive slicing actions. We will also leverage p5.js to create visually appealing graphics and animations, creating an engaging game environment that enhances the overall gameplay experience.

Challenge

One of the main challenges of this project will be optimizing the game’s performance to ensure smooth gameplay and minimize latency. We will need to carefully understand the output of the PoseNet model and use the information to update the virtual sword’s position and orientation in real-time.

Week 10: Arduino-P5 exercieces (Youssef-Arsalan)

Exercise 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.

P5 link:   https://editor.p5js.org/Arslan108/sketches/DVc3r1-YY            Arduino script:

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
// const int ledPin = 3;
// int duty = 10;
// defines variables
long duration;
int distance;
void setup() {
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  // pinMode(ledPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  Serial.begin(9600); // Starts the serial communication
}
void loop() {
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance = duration * 0.034 / 2;
  // Prints the distance on the Serial Monitor * 0.034 / 2
  // analogWrite(ledPin, duty);
  Serial.println(distance);
  delay(150);
}

The Ball class defines the properties and methods for the ball, including its position, velocity, and radius. The update() method updates the ball’s horizontal position and velocity according to the gravity, bounce factor, and the sensor value that is taken from the ultrasonic distance sensor. The show() method is responsible for displaying the ball on the canvas.

Exercise2: Make something that controls the LED brightness from p5

P5 link: https://editor.p5js.org/Arslan108/sketches/rSn6NmdHy.                         Arduino script:

int ledPin = 3;


void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  // put your setup code here, to run once:
  

}

void loop() {
  while (Serial.available()) {
    // digitalWrite(LED_BUILTIN, HIGH); // led on while receiving data

    int duty = Serial.parseInt();
    if (Serial.read() == '\n') {
      analogWrite(ledPin, duty);
    }
 

}
}

The code allows the control the intensity of the light emitted by the LED depending on the Y coordinate of the mouse. The higher the Y-coordinate is the brighter the LED is.

Exercise 3: Make something that controls the LED brightness from p5.

P5 link: https://editor.p5js.org/Arslan108/sketches/yhLl1hYsD.                                      Arduino script:

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int ledPin = 4;
// int duty = 10;
// defines variables
long duration;
int distance;
void setup() {
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(ledPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  Serial.begin(9600); // Starts the serial communication
}
void loop() {
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance = duration * 0.034 / 2;
  // Prints the distance on the Serial Monitor * 0.034 / 2
  int duty = Serial.parseInt();
    if (Serial.read() == '\n') {
      digitalWrite(ledPin, duty);
    }
  Serial.println(distance);
  delay(150);
  // digitalWrite(ledPin, low);
}

We have kept the code form exercise 1 but we have added the lines that send the light up order to the LED, every time the ellipse get in contact with the right or the left borders of the canvas.

let sendToArduino = light + "\n";
writeSerial(sendToArduino);
light = 0;