#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo s1;
const int buzzer1 = 9; //buzzer to arduino pin 9
int upButton = 7;
int downButton = 8;
int selectButton = 12;
int menu = 1, submenu = 1, num = 1;
int LedOut1 = 2;
int LedOut2 = 4;
int LedOut3 = 5;
int LedOut4 = 6;
int Timer = 0;
int Timer_1 = 0;
int Timer2 = 0;
int Timer3 = 0;
int Timer4 = 0;
int pos = 0; // variable to store the servo position
int angle = 0;
void setup() {
lcd.init();
lcd.backlight();
lcd.setCursor(3,0);
lcd.print("Welcome to");
lcd.setCursor(0,1);
lcd.print("JRA VENDING SHOP");
delay(6000);
lcd.clear();
pinMode(upButton, INPUT_PULLUP);
pinMode(downButton, INPUT_PULLUP);
pinMode(selectButton, INPUT_PULLUP);
pinMode(LedOut1, OUTPUT);
pinMode(LedOut2, OUTPUT);
pinMode(LedOut3, OUTPUT);
pinMode(LedOut4, OUTPUT);
pinMode(buzzer1, OUTPUT); // Set buzzer - pin 9 as an output
s1.attach(11);
s1.write(pos);
updateMenu();
}
void loop() {
keyboard ();
}
void keyboard() {
if (!digitalRead(downButton) && num == 1){
menu++;
updateMenu();
delay(100);
while (!digitalRead(downButton) && num == 1);
}
if (!digitalRead(upButton) && num == 1){
menu--;
updateMenu();
delay(100);
while(!digitalRead(upButton) && num == 1);
}
if (!digitalRead(selectButton) && num == 1){
executeAction();
delay(100);
while (!digitalRead(selectButton) && num == 1);
}
if (!digitalRead(downButton) && num == 2){
submenu++;
Menu1();
delay(100);
while (!digitalRead(downButton) && num == 2);
}
if (!digitalRead(upButton) && num == 2){
submenu--;
Menu1();
delay(100);
while (!digitalRead(upButton) && num == 2);
}
}
void updateMenu() {
switch (menu) {
case 0:
menu = 1;
break;
case 1:
lcd.clear();
lcd.print(">YELLOW");
lcd.setCursor(0, 1);
lcd.print(" BLUE");
tone(buzzer1, 2000); // Send 2KHz sound signal...
delay(300); // ...for 1 sec
noTone(buzzer1); // Stop sound...
delay(300); // ...for 1sec
break;
case 2:
lcd.clear();
lcd.print(" YELLOW");
lcd.setCursor(0, 1);
lcd.print(">BLUE");
tone(buzzer1, 2000); // Send 2KHz sound signal...
delay(300); // ...for 1 sec
noTone(buzzer1); // Stop sound...
delay(300); // ...for 1sec
break;
case 3:
lcd.clear();
lcd.print(">RED");
lcd.setCursor(0, 1);
lcd.print(" GREEN");
tone(buzzer1, 2000); // Send 2KHz sound signal...
delay(300); // ...for 1 sec
noTone(buzzer1); // Stop sound...
delay(300); // ...for 1sec
break;
case 4:
lcd.clear();
lcd.print(" RED");
lcd.setCursor(0, 1);
lcd.print(">GREEN");
tone(buzzer1, 2000); // Send 2KHz sound signal...
delay(300); // ...for 1 sec
noTone(buzzer1); // Stop sound...
delay(300); // ...for 1sec
break;
case 5:
lcd.clear();
lcd.print(">SERVO MOTOR");
lcd.setCursor(0, 1);
lcd.print(" MOTOR CONTROL");
tone(buzzer1, 2000); // Send 2KHz sound signal...
delay(300); // ...for 1 sec
noTone(buzzer1); // Stop sound...
delay(300); // ...for 1sec
break;
case 6:
lcd.clear();
lcd.print(" SERVO MOTOR");
lcd.setCursor(0, 1);
lcd.print(">MOTOR CONTROL");
tone(buzzer1, 2000); // Send 2KHz sound signal...
delay(300); // ...for 1 sec
noTone(buzzer1); // Stop sound...
delay(300); // ...for 1sec
break;
case 7:
menu = 6;
break;
}
}
void executeAction() {
switch (menu) {
case 1:
action1();
break;
case 2:
action2();
break;
case 3:
action3();
break;
case 4:
action4();
break;
case 5:
action5();
break;
case 6:
Menu1();
num ++;
break;
}
}
void action1() {
lcd.clear();
lcd.print(">TOGGLE YELLOW!");
digitalWrite(LedOut1,HIGH);
Timer++;
if(Timer ==2){
digitalWrite(LedOut1,LOW);
Timer=0;
}
else{
digitalWrite(LedOut1,HIGH);
}
delay(1500);
updateMenu();
}
void action2() {
lcd.clear();
lcd.print(">TOGGLE BLUE!");
digitalWrite(LedOut2, HIGH);
Timer_1++;
if(Timer_1 ==2){
digitalWrite(LedOut2,LOW);
Timer_1=0;
}
else{
digitalWrite(LedOut2,HIGH);
}
delay(1500);
updateMenu();
}
void action3() {
lcd.clear();
lcd.print(">TOGGLE RED!");
digitalWrite(LedOut3, HIGH);
Timer2++;
if(Timer2 ==2){
digitalWrite(LedOut3,LOW);
Timer2=0;
}
else{
digitalWrite(LedOut3,HIGH);
}
delay(1500);
updateMenu();
}
void action4() {
lcd.clear();
lcd.print(">TOGGLE GREEN!");
digitalWrite(LedOut4,HIGH);
Timer3++;
if(Timer3 ==2){
digitalWrite(LedOut4,LOW);
Timer3=0;
}
else{
digitalWrite(LedOut4,HIGH);
}
delay(1500);
updateMenu();
}
void action5() {
Timer4 += 1;
if(Timer4 ==1){
lcd.clear();
lcd.print("MOTOR ANGLE:");
for (pos = 0; pos <= 90; pos += 1) { // goes from 0 degrees to 90 degrees
// in steps of 1 degree
s1.write(pos); // tell servo to go to position in variable 'pos'
delay(50); // waits 15ms for the servo to reach the position
lcd.setCursor(2,1);
lcd.print(pos);
}
delay(1000);
updateMenu();
}
if(Timer4 ==2){
lcd.clear();
lcd.print("MOTOR ANGLE:");
for (pos = 90; pos <= 180; pos += 1) { // goes from 90 degrees to 180 degrees
s1.write(pos); // tell servo to go to position in variable 'pos'
delay(50); // waits 15ms for the servo to reach the position
lcd.setCursor(2,1);
lcd.print(pos);
}
delay(1000);
updateMenu();
}
if(Timer4 ==3){
lcd.clear();
lcd.print("MOTOR ANGLE:");
for (pos = 180; pos >= 90; pos -= 1) { // goes from 180 degrees to 90 degrees
s1.write(pos); // tell servo to go to position in variable 'pos'
delay(50); // waits 15ms for the servo to reach the position
lcd.setCursor(2,1);
lcd.print(pos);
}
delay(1000);
updateMenu();
}
if(Timer4 ==4){
lcd.clear();
lcd.print("MOTOR ANGLE:");
for (pos = 90; pos >= 0; pos -= 1) { // goes from 90 degrees to 0 degrees
s1.write(pos); // tell servo to go to position in variable 'pos'
delay(50); // waits 15ms for the servo to reach the position
Timer4=0;
lcd.setCursor(2,1);
lcd.print(pos);
}
delay(1000);
updateMenu();
}
}
void Menu1() {
switch (submenu) {
case 0:
submenu = 1;
break;
case 1:
lcd.clear();
lcd.print(">RED");
lcd.setCursor(0, 1);
lcd.print(" GREEN");
break;
case 2:
lcd.clear();
lcd.print(" RED");
lcd.setCursor(0, 1);
lcd.print(">GREEN");
break;
case 3:
lcd.clear();
lcd.print(">MAX");
lcd.setCursor(0, 1);
lcd.print(" MIN");
break;
}
}