xxxxxxxxxx
207
let isConnected = false;
let handX;
let handY;
let video;
let handpose;
let obstacleDetected = false;
let carMovingBack = false;
let programStarted = false; // Variable to track if the program has started
let instructionsImage;
let introImage;
let main;
function preload() {
// Load the instructions image
instructionsImage = loadImage('instructions.png');
introImage = loadImage('introscreen-1.png');
main= loadImage('MAIN-1.png')
}
function keyPressed() {
// Check if the "s" key is pressed
if (!programStarted) {
if (key === 'i' || key === 'I') {
// Show instructions
if (introScreenActive) {
instructions();
}
} else if (key === 's' || key === 'S') {
// Start the program
programStarted = true;
startRobot();
}
}
}
let introScreenActive = true;
//Create video capture from the webcam
function setup() {
createCanvas(800, 600);
// Setup serial connection
serial = new p5.SerialPort();
serial.on('connected', () => {
isConnected = true;
console.log('Connected to Arduino');
});
serial.on('error', () => {
isConnected = false;
console.log('Error connecting to Arduino');
});
serial.open('/dev/tty.usbmodem11201', { baudRate: 9600 });
serial.on('data', () => {
// Read incoming data from Arduino if needed
});
video = createCapture(VIDEO);
video.size(400, 400);
video.hide();
// Load the hand tracking model
handpose = ml5.handpose(video, startRobot);
handpose.on('predict', gotHands);
}
function introScreen() {
image(introImage, 0, 0, width, height);
if (introScreenActive && (keyIsPressed && (key === 'i' || key === 'I'))) { // Check for "i" or "I" key press
instructions();
}
}
function instructions() {
introScreenActive = false;
image(main, 0, 0, width, height);
}
function draw() {
if (!programStarted) {
introScreen();
return; // Exit the draw function if the program has not started
}
image(main, 0, 0, width, height);
if (handX && handY) {
// Display the position of the tracked hand
ellipse(handX, handY, 50);
// Determine the movement based on the hand position
if (handY > height / 2) {
if (handX < width / 3) {
moveLeft();
fill(255, 255, 0); // Yellow color for the left direction
} else if (handX < 2 * width / 3) {
moveBackward();
fill(255, 0, 0); // Red color for the backward direction
} else {
moveRight();
fill(0, 0, 255); // Blue color for the right direction
}
} else {
moveForward();
fill(0, 255, 0); // Green color for the forward direction
}
} else {
stopRobot();
fill(255); // White color for no movement
}
// Check if hands are out of the frame
if (handX < 0 || handX > width || handY < 0 || handY > height) {
stopRobot();
fill(255); // White color for no movement
}
// Check for obstacle detection
if (obstacleDetected && !carMovingBack) {
fill(255, 0, 0); // Red color for the obstacle warning
textSize(32);
textAlign(CENTER, CENTER);
text('There is an obstacle ahead!', width / 2, height - 50);
} else {
// Clear the obstacle detection
obstacleDetected = false;
}
}
function gotHands(results = []) {
if (!programStarted && results.length > 0) {
const hand = results[0].annotations.indexFinger[3];
handX = hand[0];
handY = hand[1];
// Start the program when hand is detected and 's' is pressed
if (handX && handY && keyIsPressed && (key === 's' || key === 'S')) {
programStarted = true;
startRobot();
}
} else if (results.length > 0) {
const hand = results[0].annotations.indexFinger[3];
handX = hand[0];
handY = hand[1];
} else {
handX = null;
handY = null;
}
}
function moveLeft() {
if (isConnected) {
serial.write('L');
}
}
function moveRight() {
if (isConnected) {
serial.write('R');
}
}
function moveForward() {
if (isConnected) {
serial.write('U');
}
}
function moveBackward() {
if (isConnected) {
serial.write('D');
}
}
function stopRobot() {
if (isConnected) {
serial.write('S');
}
}
function startRobot() {
// Start the robot movement when the hand tracking model is ready
console.log('Hand tracking model loaded');
}
// Function to detect obstacle
function detectObstacle() {
obstacleDetected = true;
carMovingBack = true;
}
// Function to stop obstacle detection
function stopObstacleDetection() {
obstacleDetected = false;
carMovingBack = false;
}