xxxxxxxxxx
139
const serial = new p5.WebSerial();
let portButton;
let inData = 0;
let inData2 = 0;
let currentState = "normal";
let isConnected = false;
function setup() {
createCanvas(800, 600);
frameRate(10); // Set frame rate to 10 fps to match Arduino's data sending rate
allSerialStuff();
}
function draw() {
background(220);
// Determine the current state
if (inData2 > 1000) {
currentState = "really happy";
} else if ((inData >= 1 && inData <= 10) || inData2 > 500) {
currentState = "happy";
} else {
currentState = "normal";
}
// Draw the centered color state
let centerX = width / 2;
let centerY = height / 2;
let size = 300;
if (currentState === "normal") {
fill(0); // Black for normal state
ellipse(centerX, centerY, size, size);
} else if (currentState === "happy") {
fill(255, 0, 0); // Red for happy state
ellipse(centerX, centerY, size, size);
} else if (currentState === "really happy") {
// Gradient for really happy state
for (let i = size; i > 0; i--) {
let inter = map(i, 0, size, 0, 1);
let c = lerpColor(color(255, 165, 0), color(255, 255, 0), inter);
fill(c);
ellipse(centerX, centerY, i, i);
}
}
// Display sensor values and connection status
fill(0); // Black text
textSize(16);
textAlign(LEFT, TOP);
text(`Distance: ${inData.toFixed(2)} cm`, 10, 10);
text(`Pressure: ${inData2}`, 10, 30);
text(
`Connection Status: ${isConnected ? "Connected" : "Disconnected"}`,
10,
50
);
text(`State: ${currentState}`, 10, 70);
}
function serialEvent() {
let inString = serial.readLine();
if (inString.length > 0) {
let sensors = split(inString, ",");
if (sensors.length === 2) {
inData = parseFloat(sensors[0]);
inData2 = parseInt(sensors[1]);
isConnected = true;
console.log(`Received: Distance=${inData}, Pressure=${inData2}`); // Debug output
}
}
}
function allSerialStuff() {
if (!navigator.serial) {
alert("WebSerial is not supported in this browser. Try Chrome or MS Edge.");
}
serial.getPorts();
serial.on("noport", makePortButton);
serial.on("portavailable", openPort);
serial.on("requesterror", portError);
serial.on("data", serialEvent);
serial.on("close", portClosed);
navigator.serial.addEventListener("connect", portConnect);
navigator.serial.addEventListener("disconnect", portDisconnect);
}
function makePortButton() {
portButton = createButton("choose port");
portButton.position(10, 100);
portButton.mousePressed(choosePort);
}
function choosePort() {
if (portButton) portButton.show();
serial.requestPort();
}
function openPort() {
serial.open({ baudRate: 9600 }).then(initiateSerial).catch(serialError);
}
function initiateSerial() {
console.log("port open");
isConnected = true;
if (portButton) portButton.hide();
}
function serialError(err) {
console.error("Serial port error:", err);
isConnected = false;
}
function portError(err) {
alert("Serial port error: " + err);
}
function portConnect() {
console.log("port connected");
isConnected = true;
serial.getPorts();
}
function portDisconnect() {
console.log("port disconnected");
isConnected = false;
}
function portClosed() {
console.log("port closed");
isConnected = false;
makePortButton();
}
function closePort() {
serial.close();
isConnected = false;
}