#include <Servo.h>
int pirPin = 12; // Arduino pin the PIR sensor is connected to
int servoPin = 9; //Arduino pin the servo is connected to
int motionStatus = 0; // variable to store the PIR sensor's motion status (high or low)
int pirState = 0; // variable to track the state change
Servo scaryServo; // create servo object to control our servo
void setup() {
Serial.begin(9600); // initialize the serial monitor
pinMode(pirPin, INPUT); // set the Arduino pin that PIR sensor is connected to as an INPUT
scaryServo.attach(servoPin); // attaches the servo on pin 9 to the servo object
scaryServo.write(0); // start the servo at 0 degrees
delay(5000); // give time for the PIR sensor to calibrate (30-60secs is best)
}
void loop() {
if(Serial.available() > 0) { // Check if data is available to read from serial monitor
int command = Serial.parseInt(); // Read the command from the serial monitor
if(command == 1) { // If command is 1 (Espresso)
Serial.println("Payment complete. Please wait for your Espresso.");
while(true) { // Wait until motion is detected
motionStatus = digitalRead(pirPin);
if(motionStatus == HIGH) {
scaryServo.write(90); // rotate the servo to 90 degrees
Serial.println("Your Espresso is ready.");
delay(5000); // Delay to give time to take the coffee
scaryServo.write(0); // rotate the servo back to 0 degrees
break; // Exit the loop once motion is detected
}
}
}
else if(command == 2) { // If command is 2 (Latte)
Serial.println("Payment complete. Please wait for your Latte.");
while(true) { // Wait until motion is detected
motionStatus = digitalRead(pirPin);
if(motionStatus == HIGH) {
scaryServo.write(90); // rotate the servo to 90 degrees
Serial.println("Your Latte is ready.");
delay(5000); // Delay to give time to take the coffee
scaryServo.write(0); // rotate the servo back to 0 degrees
break; // Exit the loop once motion is detected
}
}
}
else if(command == 3) { // If command is 3 (Cappuccino)
Serial.println("Payment complete. Please wait for your Cappuccino.");
while(true) { // Wait until motion is detected
motionStatus = digitalRead(pirPin);
if(motionStatus == HIGH) {
scaryServo.write(90); // rotate the servo to 90 degrees
Serial.println("Your Cappuccino is ready.");
delay(5000); // Delay to give time to take the coffee
scaryServo.write(0); // rotate the servo back to 0 degrees
break; // Exit the loop once motion is detected
}
}
}
else { // If command is not recognized
Serial.println("Invalid selection.");
}
}
}