xxxxxxxxxx
60
let plantHeight = 50; // Initial height of the plant
let lightValue = 0; // Initial value from Arduino's photocell sensor
function setup() {
createCanvas(640, 480);
textSize(18);
setUpSerial();
}
function draw() {
background(220);
// Display plant
fill(34, 139, 34); // Green color for the plant
rect(width / 2 - 10, height - plantHeight, 20, plantHeight);
// Display light value
fill(0);
text('Light Value: ' + lightValue, 20, 60);
if (!serialActive) {
text("Press Space Bar to select Serial Port", 20, 30);
} else {
text("Connected", 20, 30);
}
// Adjust plant height based on light value
if (lightValue > 50) {
let growthRate = map(lightValue, 50, 1023, 0, 5); // Adjust the growth rate as needed
plantHeight += growthRate;
// Constrain plant height to prevent it from growing indefinitely
plantHeight = constrain(plantHeight, 10, height - 30);
}
}
function keyPressed() {
if (key == " ") {
// important to have in order to start the serial connection!!
setUpSerial();
}
}
// This function will be called by the web-serial library
function readSerial(data) {
if (data != null) {
// Parse the incoming data from Arduino
let sensorValues = split(trim(data), ",");
if (sensorValues.length == 2) {
// Update the light value
lightValue = int(sensorValues[0]);
}
}
}