xxxxxxxxxx
70
//Modified Professor Sherwood's program: https://editor.p5js.org/aaronsherwood/sketches/q2Pl77SWl
let brightness = 0;
function setup() {
createCanvas(640, 480);
textSize(18);
}
function draw() {
background(255);
if (!serialActive) {
text("Press Space Bar to select Serial Port", 20, 30);
} else {
text("Connected", 20, 30);
}
// Click on place on canvas to adjust brightness
if (mouseIsPressed) {
brightness = round(map(mouseX, 0, 640, 0, 255));
print(brightness);
}
text("Brightness level is: " + brightness, 200, 200);
}
function keyPressed() {
if (key == " ") {
// important to have in order to start the serial connection!!
setUpSerial();
}
}
//Value of brightness is sent to Arduino
function readSerial(data) {
if (data != null) {
//SEND TO ARDUINO HERE
let sendToArduino = brightness + "\n";
writeSerial(sendToArduino);
}
}
//Arduino Code
/*
//Initialize Variables
int brightness;
int led1 = 5;
void setup() {
Serial.begin(9600);
pinMode(5, OUTPUT); //Set pin
// Starts connection
while (Serial.available() <= 0) {
Serial.println("0,0"); // Starting Message
delay(300);
}
}
void loop() {
// Wait for data from p5 before doing something
if (Serial.available()) {
brightness = Serial.parseInt(); //Arduino brightness = p5 brightness
Serial.println(brightness);
delay(30);
analogWrite(led1, brightness); // Updates brightness of LED
}
}
*/