Topic Selection:

Option 2: p5 to Arduino, using mouseX and mouseY to control something (light)

The assignment I chose is to control the brightness of an LED on the p5 interface by clicking the screen with the mouse

and I have found some tutorials from YouTube to help me understand how it could work :https://www.youtube.com/watch?v=yThUrgBkZ2o:

Untitled

Arduino code:

const int ledPin=3;

void setup(){
  Serial.begin(9600);
  pinMode(ledPin,OUTPUT);
}

void loop(){
  int brightness;
  if(Serial.available() > 0){
    brightness = Serial.read();
    analogWrite(ledPin,brightness);
  }
}

Untitled

P5 Code:

// serial communication to a microcontroller to adjust brightness
// arduino code can be found here : [<https://gist.github.com/shfitz/6b6c349f883a44b95fed8546d5525c20>](<https://gist.github.com/shfitz/6b6c349f883a44b95fed8546d5525c20>)
// borrowed in part from [<https://editor.p5js.org/REAS/sketches/S1TNUPzim>](<https://editor.p5js.org/REAS/sketches/S1TNUPzim>)
let serial; // variable for the serial object
let bright = 0; // variable to hold the data we're sending
let dark, light; // variables to hold the bgcolor