Serial Control

1 & 3

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.
Taking 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

2

Using a slider to control the LED brightness from p5

Arduino Code

int ledPin = 2; // connect LED to pin 9
int brightness = 0;

void setup() {
  Serial.begin(9600); // start serial communication
  pinMode(ledPin, OUTPUT); // set LED pin as an output
}

void loop() {
  // read incoming serial data
  if (Serial.available() > 0) {
    brightness = Serial.read(); // read the incoming value
    brightness = constrain(brightness, 0, 255); // constrain the value between 0 and 255
    analogWrite(ledPin, brightness); // set the LED brightness
  }
  delay(10); // wait a short time before repeating the loop
}

P5.js code

let slider; // slider to control LED brightness
let serial;

function setup() {
  serial = new p5.SerialPort();
  
  serial.open("COM3");
  
  createCanvas(400, 400); // create a canvas
  slider = createSlider(0, 255, 0); // create a slider
  slider.position(50, 50); // set the position of the slider
}

function draw() {
  background(220); // clear the canvas

  let val = slider.value(); // get the slider value
  text("LED Brightness: " + val, 50, 50); // display the brightness value

  // send data to Arduino over serial
  if (frameCount % 10 == 0) { // limit the rate of sending data to avoid overloading the serial port
    serial.write(val); // send the slider value over serial
  }
}

 

 

 

Leave a Reply