xxxxxxxxxx
51
let serial;
let video;
let recording = false;
// let serialActive = false; // Declare serialActive variable here
function setup() {
createCanvas(640, 480);
video = createCapture(VIDEO);
video.size(320, 240);
video.hide();
textSize(20);
}
function draw() {
background(255);
image(video, 0, 0);
if (!serialActive) {
fill(0);
text("Press space bar to connect to Arduino", 60, 60);
}
// Change brightness of LED based on mouse position
let brightness = map(mouseY, 0, width, 255, 0);
// Send the brightness value to Arduino
if (serialActive) {
let sendToArduino = brightness + "\n";
serial.write(sendToArduino);
}
}
function keyPressed() {
if (key === " " && !serialActive) {
connectArduino();
}
}
async function connectArduino() {
try {
const port = await navigator.serial.requestPort();
serial = new Serial(port, { baudRate: 9600 });
serial.onReceive = () => { };
serialActive = true;
console.log("Serial connection established");
} catch (error) {
console.error('Error connecting to Arduino:', error);
}
}