week 11: class 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.

I took the existing Arduino and p5 code that we saw in class, and simply added this line in the p5 loop() function:

// move ellipse
ellipse(map(alpha, 0, 1023, 0, 640), width/2, 55, 55);

This allowed me to control the horizontal position of the ellipse by turning the potentiometer.

 

2. make something that controls the LED brightness from p5Again, starting with the same code, I just made some adjustments. Here’s the relevant part from the p5 code:

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

    // Print the current values
    text("rVal = " + str(rVal), 20, 50);
    text("alpha = " + str(alpha), 20, 70);
    
    mouseXMapped = map(mouseX, 0, width, 0, 255);
    right = int(mouseXMapped);
    mouseYMapped = map(mouseY, 0, height, 0, 255);
    left = int(mouseYMapped);
  }

This just informs the Arduino of the mouse’s X and Y coordinates.

On the Arduino code, I connected both LEDS to PWM capable ports, and simply added these two lines:

analogWrite(leftLedPin, left);
analogWrite(rightLedPin, right);

The brightness of the left LED increases as the cursor moves to the bottom of the p5 window, and the brightness of the right LED increases as the cursor moves to the right edge of the p5 window.

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 sensorVideo

demo: https://www.youtube.com/shorts/-m7XMTdTVoUI

made the potentiometer control the wind speed (mapped from -1 to 1), and the blue LED blinks whenever the ball comes in contact with the ground. Here’s the complete p5 code, and I left the Arduino code almost untouched, except for a small delay after the LED blinks once, just to make it less jittery.

if (Serial.read() == '\\n') {
      digitalWrite(leftLedPin, left);
      if (left == HIGH) {
        delay (100);
      }
      int sensor = analogRead(A0);
      delay(5);
      int sensor2 = analogRead(A1);
      delay(5);
      Serial.print(sensor);
      Serial.print(',');
      Serial.println(sensor2);
    }

 

Leave a Reply