xxxxxxxxxx
53
let redSwitch1 = 0;
let redSwitch2 = 0;
let greenSwitch1 = 0;
let greenSwitch2 = 0;
function setup() {
createCanvas(640, 480);
textSize(18);
}
function draw() {
background(255);
// Log switch states to the console for debugging
if (!serialActive) {
text("Press Space Bar to select Serial Port", 20, 30);
} else {
text("Connected", 20, 30);
}
}
function keyPressed() {
if (key == " ") {
// Important to have to start the serial connection
setUpSerial();
}
}
// This function will be called by the web-serial library
// with each new *line* of data.
let switchStates = [0, 0, 0, 0]; // Initial state
function readSerial(data) {
if (data != null) {
// Split the received string into an array of integers
switchStates = data.split(',').map(Number);
// Log switch states to the console for debugging
console.log("Switch States:", switchStates);
}
// Send data back to Arduino for handshake
// writeSerial("ACK\n");
// Send data back to Arduino for handshake
let sendToArduino = redSwitch1 + "," + redSwitch2 + "," + greenSwitch1 + "," + greenSwitch2 + "\n";
writeSerial(sendToArduino);
}