Week 11: In-class coding exercises

Below are the codes for each of our in class exercises including what was using in P5 and Arduino. After exercise 3’s code, you can find the video of the LED lighting up with the ball bouncing.

Exercise 1

P5 code

let distance = 0;

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

function draw() {
  if (!serialActive) {
     //setting up the serial port information
    background(255);
    text("Press Space Bar to select Serial Port", 20, 30);
  } else {
    background(255);
    text("Connected", 20, 30);
    //reading the distance sent from arduino onto width of computer
    let mappedDistance = map(distance, 0, 50, 0, width); 
    //preventing ball from extending beyond the screen
    mappedDistance = constrain(mappedDistance, 0, width); 
    ellipse(mappedDistance, height / 2, 25, 25);
    //actually drawing the circle
  }
}

function keyPressed() {
  if (key == " ") {
    setUpSerial(); 
  }
}

function readSerial(data) {
  //reading the distance value given by arduino
  if (data != null) {
    distance = int(data); 
  }
}

Arduino Code

const int trigPin = 10;
const int echoPin = 9;
long timeTraveled; //variables for the distance monitor
int distance;

void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT); 
  pinMode(echoPin, INPUT); 
}

void loop() {
  // clear whatever the trig pin. has read
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // 10 microsecond delay for each new reading
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  //read the values sent in through echo pin
  timeTraveled = pulseIn(echoPin, HIGH, 38000);
  // use this conversion to calculate in centimeters
  distance = timeTraveled * 0.034 / 2;
 /* if (distance > 500){
    distance = pulseIn(echoPin, HIGH);
  }*/
  //display in serial monitor
  Serial.println(distance);
  delay(50);
}

Exercise 2

P5 code

let brightnessValue = 0; 

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

function draw() {
  background(250);
  //setting up the serial port information
  if (!serialActive) {
    text("Press Space Bar to select Serial Port", 20, 30);
  } else {
    text("Connected", 20, 30);
  //depending on where the mouse is from left to right, sending that value as a brifghtness value to arduino
    brightnessValue = int(map(mouseX, 0, width, 0, 255));
    text('Brightness: ' + str(brightnessValue), 20, 50);
    let sendToArduino = brightnessValue + "\n";
    //printing that value in serial monitor arduino
    writeSerial(sendToArduino);
  }
}

function keyPressed() {
  if (key == " ") {
    setUpSerial();
  }
}

function readSerial(data) {
  //just to avoid no readSerial error saving data
  if (data != null) {
    let receivedData = data
  }
}

Arduino code

int ledPin = 9; 
int brightness = 0; 

void setup() {
  pinMode(ledPin, OUTPUT); 
  Serial.begin(9600); 
}

void loop() {
  if (Serial.available() > 0) {
    //making sure its not trying to run unless receiving from p5
    String brightnessString = Serial.readStringUntil('\n');
    //reading the converted value from the computer
    brightness = brightnessString.toInt(); //converting to an integer
    //illuminating the led to the newly converted brightness value
    analogWrite(ledPin, brightness);
    Serial.println(brightness); //adding to serial monitor
  }
}

Exercise 3

P5 code

let velocity;
let gravity;
let position;
let acceleration;
let wind;
let drag = 0.99;
let mass = 50;
let potValue = 512;
let running = true;

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);
  //created this flag variable to get the ball to drop again
  if (!running) {
    textSize(24);
    fill(0);
    text("Press 's' to run again", width / 2 - 100, height / 2);
    return;
  }
  //mapping the value of the potentiometer to which direction the ball falls 
  wind.x = map(potValue, 0, 1023, -2, 2);
  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;
    position.y = height - mass / 2;
  //standard s
    if (serialActive) {
      writeSerial("Ball bounnced\n");
      //sending this to arduino each time ball hits ground
    }
  }
  if (position.x < -mass || position.x > width + mass) {
    //preventing the ball from being registered when it rolls off the screen
    running = false;
  }
}
function applyForce(force) {
  let f = p5.Vector.div(force, mass);
  acceleration.add(f);
}

function keyPressed() {
  if (key == ' ') {
    //opening setup pop up window
    setUpSerial();
  }

  if (key == 's') {
    //preparing a new ball to drop resetting the values 
    position = createVector(width / 2, 0);
    velocity = createVector(0, 0);
    acceleration = createVector(0, 0);
    running = true;
  }
}
function readSerial(data) {
  //just to avoid no readSerial error saving data
  if (data != null) {
    potValue = int(data);
  }
}

Arduino code

const int pmPin = A0;  
const int ledPin = 9;   

void setup() {
  pinMode(ledPin, OUTPUT);   
  Serial.begin(9600);       
}

void loop() {
  int potValue = analogRead(pmPin); //read potentiometer value
  Serial.println(potValue); //printing in serial monitor for p5
  if (Serial.available() > 0) { //make sure data is coming in to p5
    String event = Serial.readStringUntil('\n');
    command.trim(); //reading each newline of data
    if (event == "BOUNCE") { //sent from p5
      // turning the led on when bounce message is received
      digitalWrite(ledPin, HIGH);
      delay(100);  
      digitalWrite(ledPin, LOW);
    }
  }
  delay(50); 
}

When this code runs, the first ball is dropped from the original code and then you are prompted to open the serial connection I just cut that out of the video.

Leave a Reply