#include <Servo.h>
#include <LiquidCrystal_I2C.h>
int POSservo = 0;
Servo servo_5;
Servo servo_1;
Servo servo_2;
const int ledpin1 = 8;
const int ledpin2 = 9;
const int buttonpin = 12;
int buttonState = 0;
const int buzzerPin = 7;
LiquidCrystal_I2C lcd(0x27, 16, 2);
int food = 1000;
void setup()
{
servo_5.attach(3);
servo_1.attach(5);
servo_2.attach(6);
Serial.begin(9600);
pinMode(buttonpin, INPUT);
pinMode(ledpin1, OUTPUT);
pinMode(ledpin2, OUTPUT);
pinMode(buzzerPin, OUTPUT);
lcd.begin(16,2);
lcd.setBacklight((uint8_t)1);
lcd.print("Food: " + String(food));
}
int count = 1;
// add something for time as well here
void loop()
{
// It will sweep the servo from 0 to 180 degrees
// if button pressed then only execute the code ....
buttonState = digitalRead(buttonpin);
digitalWrite (8, LOW);
servo_5.write(0);
servo_1.write(0);
servo_2.write(0);
if (buttonState == HIGH) {
for (POSservo = 0; POSservo <= 180; POSservo=POSservo+1)
{
// tell servo to go to position in variable 'POSservo'
if(count==1){
servo_5.write(POSservo);
// It will wait for the specified duration (20 milliseconds) to reach the position
delay(2); }
else if(count==2){
servo_1.write(POSservo);
delay(2);
}
else{
servo_2.write(POSservo);
delay(2);
}
}
for (POSservo = 180; POSservo >= 0;POSservo= POSservo-1)
{
// It will tell servo to go to position in the declared variable 'POSservo'
if(count==1){
servo_5.write(POSservo);
// It will wait for the specified duration (20 milliseconds) to reach the position
delay(2); }
else if(count==2){
servo_1.write(POSservo);
delay(2);
}
else{
servo_2.write(POSservo);
delay(2);
}
}
digitalWrite (9, HIGH);
delay(1000); // 1 second = 1 x 1000 milliseconds
digitalWrite (9, LOW);
food = (food - 333);
if(count==1){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Food: " + String(food));
lcd.setCursor(0,1);
lcd.print("2 Serves left");
}
else if(count==2){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Food: " + String(food));
lcd.setCursor(0,1);
lcd.print("1 Serves left");
} else{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Food: " + String(food));
lcd.setCursor(0,1);
lcd.print("0 Serves left");
}
count=count+1;
}
else{
servo_5.write(0);
}
// if count > 3 buzzer sounds...
if(food<300){
tone(buzzerPin, 500, 2000);
noTone(buzzerPin);
delay(200);
tone(buzzerPin, 500, 2000);
digitalWrite (8, HIGH);
delay(5000);
noTone(buzzerPin);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Add more Food");
delay(5000);
lcd.clear();
lcd.setCursor(0, 0);
food=1000;
count=1;
lcd.print("Food: " + String(food));
}
}