Week 11: exercises

  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

    let rVal = 0;
    let alpha = 255;
    let left = 0;
    let right = 0;
    
    
    function setup() {
      createCanvas(640, 360);
      noFill();
      //create an ellipse with x position changing and y (300) position constant 
    }
    
    function draw() {
      background(255);
      
      
    
      if (!serialActive) {
        fill(0)
        text("Press Space Bar to select Serial Port", 20, 30);
      } else {
        fill(0)
        text("Connected", 20, 30);
      }
      let xPos = 300;
      xPos = map(alpha, 0, 1023, 0,640);
      ellipse(xPos, 150, 50, 50);
    
      console.log(alpha);
    }
    
    function keyPressed() {
      if (key == " ") {
        // important to have in order to start the serial connection!!
        setUpSerial();
      }
    }

    For this question, we decided to create a simple ellipse that changes direction based on the values from the potentiometer.

  2. make something that controls the LED brightness from p5t
    function keyPressed() {
      if (key == " ") {
        // important to have in order to start the serial connection!!
        setUpSerial();
      }
      if (key== "1"){
        left = 50;
      }
      
      if (key== "2"){
        left = 100;
      }
      if (key== "3"){
        left = 255;
      }
      if (key =="4"){
        right = 50;
      }
      if (key =="5"){
        right = 100;
      }
      if (key =="6"){
        right =255;
      }
    }

    This one was pretty simple. Pressing the keys 1-6 increases/decreases the brightness of the left or right LED light. 1-3 correspond to the left, while 4-6 correspond to the right led.

  3. take the gravity wind example (https://editor.p5js.org/aaronsherwood/sketches/I7iQrNCul) 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.
let rVal = 0;
let alpha = 255;
let left = 0;
let right = 0;
let velocity;
let gravity;
let position;
let acceleration;
let wind;
let drag = 0.99;
let mass = 20;

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

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
      position.y = height-mass/2;
    
    }
  console.log(position.y);
  if(position.y >= 330){
    left = 1;
  }
  else if(position.y <= 330){
    left = 0;
  }
  console.log(rVal);
  wind.x = map(alpha, 0, 1023, -1, 1);
  

 

  if (!serialActive) {
    fill(0)
    text("Press Space Bar to select Serial Port", 20, 30);
  } else {
    fill(0)
    text("Connected", 20, 30);
  }
}
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();
  }
  if(key == "1"){
    left = 1;
  }
  
}

For changing the wind direction, we followed similar logic to the first question. The ball changes direction based on values mapped from the potentiometer.

 

Leave a Reply