Week 11 Exercises

Exercise 1

function draw() {
  background(250, 200, 152);

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

    // ellipse
    noStroke();
    fill(255, 0, 0);
    ellipse(map(rVal, 0, 1023, 0, width), height / 2, 100, 50);
  }
}

Link to the sketch: Sketch Exercise 1 

Exercise 2

function draw() {
  background(250, 200, 152);

  if (!serialActive) {
    print("Press Space Bar to select Serial Port");
  } else {
    print("Connected");
    
    brightness = Math.floor(map(rVal, 0, 1023, 0,255));
    right = Math.floor(brightness);
  }
}

The only change in Arduino code I made was using analogWrite instead of digitalWrite.

Link to the sketch: Sketch Exercise 2

Exercise 3

function draw() {
  
  background(250,200,152);
  
  if (!serialActive) {
    print("Press Space Bar to select Serial Port");
  } else {
    if (state == 1){
    
    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;
    } 

    if (position.y >= height - mass/2 - 5) {
      left = 1;
    } else {
      left = 0;
    }

  wind.x = map(rVal, 0, 1023, -1, 1)
    }
  }
}

For this sketch, I used the state machine to allow the Arduino to load before the ball starts to fall. Otherwise, the LEDs wouldn’t work properly.

Link to the sketch: Sketch Exercise 3

Video demonstration: VIDEO

Leave a Reply