Week 11 – Serial Communication Exercises

Serial Communication 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:

I kept the Arduino and p5 code from the handshake example intact, and added this line of code in p5 to take the value obtained from the potentiometer to control the x-position of the circle.

// draw a circle, alpha value controls the x-position of the circle
circle(map(alpha, 0, 1023, 0, 640), 240, 50)

2. Make something that controls the LED brightness from p5:

Again using the Arduino and p5 code from the handshake example, I added some code to the “keyPressed” function in p5, so that different values would be sent to the “left” LED in Arduino, changing its brightness.

function keyPressed() {
  
  // set up connection with p5:
  if (key == " ") {
    setUpSerial();
  }
  
  //control the brightness of the LED by sending different values to "left"
  if (key == "1"){
    left = 10;
  }
  
  if (key == "2"){
    left = 100;
  }
  
  if(key == "3"){
    left = 255;
  }
}

Exercise 2 P5 code

3. Take the gravity wind example 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:

Video demonstration

I transplanted all the code of the gravity wind example into the handshake example, and then added two bits of code to complete the assignment. The first part sends either a “1” or a “0” to the “left” LED in Arduino to turn on and off the LED when the ball bounces.

//send value to "left" LED in arduino 
   // depending on y-position of ball 
  
  // if y-position is greater than 330, light up the LED
  if(position.y >= 330){
    left = 1;
  }
  
  // if y-position is greater than 335, turn off the LED
  if(position.y <= 335){
    left = 0;
  }

The second bit maps the potentiometer values to 1 and -1, allowing the user to control the wind by turning the potentiometer.

//control wind with potentiometer
wind.x = map(alpha, 0, 1023, -1, 1);

Exercise 3 P5 code

 

Resources:

Izah and Zion’s code helped me in completing this assignment.

Leave a Reply