xxxxxxxxxx
68
let serial;
let video;
let recording = false;
function setup() {
createCanvas(640, 480);
video = createCapture(VIDEO);
video.size(320, 240);
video.hide();
// Add a button to connect to Arduino
let connectButton = createButton("Connect Arduino");
connectButton.mousePressed(connectArduino);
}
async function connectArduino() {
try {
const port = await navigator.serial.requestPort();
serial = new Serial(port);
serial.onReceive = serialEvent;
serial.open();
} catch (error) {
console.error('Error connecting to Arduino:', error);
}
}
async function serialEvent(event) {
const reader = event.target.readable.getReader();
try {
while (true) {
const { value, done } = await reader.read();
if (done) break;
let data = new TextDecoder().decode(value);
let parts = data.split(':');
let command = parts[0];
value = parts[1];
if (command === 'START' && !recording) {
startRecording();
} else if (command === 'STOP' && recording) {
stopRecording();
}
}
} catch (error) {
console.error('Error reading serial data:', error);
} finally {
reader.releaseLock();
}
}
function draw() {
background(255);
image(video, 0, 0);
}
function startRecording() {
console.log("Recording started");
recording = true;
// Implement recording logic here
}
function stopRecording() {
console.log("Recording stopped");
recording = false;
// Implement stop recording logic here
}