xxxxxxxxxx
255
var serial; // variable to hold an instance of the serialport library
var portSelector; // dropdown menu for selecting COM port
var connectButton; // button to connect to the selected COM port
var materialType = 'Unknown';
var minWidth = 600; // set min width and height for canvas
var minHeight = 400;
var width, height; // actual width and height for the sketch
var isAutomaticMode = true;
var outData; // for data output
let latestData = 'waiting for data'; // variable to hold the input data from Arduino
var material = 'N/A';
var messagePort = '';
var message = '';
function setup() {
// set the canvas to match the window size
if (window.innerWidth > minWidth) {
width = window.innerWidth;
} else {
width = minWidth;
}
if (window.innerHeight > minHeight) {
height = window.innerHeight;
} else {
height = minHeight;
}
// set up canvas
createCanvas(width, height);
noStroke();
}
function draw() {
// set background to black
background(0);
fill(0);
rect(0, 0, width / 2, height); // left half
// draw the text - left
fill(255);
textSize(16);
if (latestData.startsWith('status:')) {
text('Status: ' + latestData.substring(7), 30, 80);
} else {
text('Status: Not connected yet', 30, 80);
}
text('Port Status: ' + messagePort, 30, 100);
text(isAutomaticMode ? 'Automatic (Active)' : 'Automatic', 30, 160);
textSize(18);
if (latestData.startsWith('material:')) {
material = latestData.substring(9);
}
text('Material Type: ' + material, 30, 180);
rect(width / 2, 0, width / 2, height);
text('Message: ' + message, 30, 120);
// draw the separator between frames
fill(255);
rect(width / 2 - 0.5, 0, 1, height);
// right side: read the value from browser
rect(width / 2, 0, width / 2, height);
fill(0);
textSize(16);
text(isAutomaticMode ? 'Manual' : 'Manual (Active)', width - 220, 160);
textSize(18);
text('Material Type: ' + materialType, width - 220, 180);
// Buttons
rect(width / 2 + 30, 100, 120, 40, 10); // Set Mode button
rect(width / 2 + 30, 150, 120, 40, 10); // Plastic button
rect(width / 2 + 30, 200, 120, 40, 10); // General button
fill(255);
textSize(16);
text('Set Mode', width / 2 + 60, 125);
text('Plastic', width / 2 + 60, 175);
text('General', width / 2 + 60, 225);
//connects serial port
if (!serialActive) {
textSize(10);
text("Press Space Bar to select Serial Port", 100, 30);
} else {
textSize(10);
text("Connected",100,30);
}
}
function keyPressed() {
if (key == " ") {
// important to have in order to start the serial connection!!
setUpSerial();
}
}
function mousePressed() {
// Check if buttons are pressed
if (mouseX > width / 2 + 30 && mouseX < width / 2 + 150) {
if (mouseY > 100 && mouseY < 140) {
// Set Mode button pressed
isAutomaticMode = !isAutomaticMode; // Toggle between automatic and manual mode
print('Set Mode: ' + (isAutomaticMode ? 'Automatic' : 'Manual'));
outData = isAutomaticMode ? 'Auto\n' : 'Manual\n';
print(outData);
writeSerial(outData);
} else if (mouseY > 150 && mouseY < 190) {
// Plastic button pressed
materialType = 'Plastic\n';
outData = 11;
for (let index = 0; index < 10; index++) {
print(outData);
writeSerial('angle:' + outData + '\n');
}
} else if (mouseY > 200 && mouseY < 240) {
// General button pressed
materialType = 'General\n';
outData = 158;
for (let index = 0; index < 10; index++) {
print(outData);
writeSerial('angle:' + outData + '\n');
}
}
}
}
function readSerial(data) {
if (data != null) {
console.log(data);
latestData = data;
}
}
//ardiuno code
/*
#include <Servo.h>;
Servo GateServo;
#define capSensor A3 //Pin A3 (analog)
#define irSensor 4 //Pin 4 (digital)
#define GateServoPin 3 // GateServo pin
bool openTrash = false;
bool autoMode = true;
//Plastic goes to angle 11
//General goes to angle 158
//The closing angle of the gate which is 85 degree
int initialServoPosition = 85;
int anglePlastic = 11;
int angleGeneral = 150;
int capValue;
unsigned long GateServoReturnTime = 0;
const unsigned long GateServoReturnDelay = 1000; // 1 seconds in milliseconds
void setup()
{
Serial.begin(9600);
GateServo.attach(GateServoPin);
GateServo.write(initialServoPosition);
pinMode(irSensor, INPUT);
Serial.println("status:Automatic wasting machine is ready");
}
void loop()
{
while (Serial.available() > 0) {
// Read the angle value from serial
String incomingData = Serial.readStringUntil('\n');
//If the mode is manual enable controlling by the p5js panel
if (!autoMode) {
// Check if the incoming data starts with "angle:"
if (incomingData.startsWith("angle:")) {
// Extract the angle value from the incoming data
int angleValue = incomingData.substring(6).toInt();
// Process the angle value if it is within the valid range
if (angleValue >= 11 && angleValue <= 158) {
// Serial.println("Received angle: " + String(angleValue));
// Set the GateServo angle
GateServo.write(angleValue);
// Reset the timer each time a valid angle is received
openTrash = true;
GateServoReturnTime = millis();
}
}
}
if (incomingData.startsWith("Auto")) {
autoMode = true;
// Serial.println("Auto mode: " + String(autoMode));
}
else if (incomingData.startsWith("Manual")){
autoMode = false;
// Serial.println("Manual mode: " + String(autoMode));
}
}
// Check if it's time to return the GateServo to 85 degrees
if (millis() - GateServoReturnTime >= GateServoReturnDelay && openTrash) {
openTrash = false;
GateServo.write(initialServoPosition);
// Serial.println("Trash Closed");
}
bool irValue = digitalRead(irSensor);
capValue = analogRead(capSensor); //Save value that is read from the analog pin A3 to the variable capValue
delay(10);
// Serial.print("Capacitive sensor Value: ");
// Serial.println(capValue);
// Serial.print("IR sensor Value: ");
// Serial.println(irValue);
//If the mode is automatic enable controlling by the arduino sensor
if (autoMode) {
//If an object detected by the IR sensor then check the capacitive sensor
if (capValue <= 250 && !irValue) {
Serial.println("material:Plastic");
GateServo.write(anglePlastic);
openTrash = true;
GateServoReturnTime = millis();
delay(100);
}
else if (capValue >= 600 && !irValue) {
Serial.println("material:General");
GateServo.write(angleGeneral);
openTrash = true;
GateServoReturnTime = millis();
delay(100);
}
}
}
*/