//lcd decleration
#include <LiquidCrystal.h>
const int rs = 9, en = 8, d4 = 7, d5 = 6, d6 = 5, d7 = 4;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
//Buzzer variable defenition
const int BuzzPin = 13;
int volume = 50;
int freq = map(volume, 0, 100, 500, 1000);
//Servo decleration
#include <Servo.h>
Servo myservo;
const int up_sw = 0;
const int down_sw = 1;
bool servo_state = true;
//Rotary Encoder decleration and variable defenitions
const int PinA = 11;
const int PinB = 12;
const int PinSW = 10;
const int BckSW = 2;
bool PinALast = LOW;
bool n = LOW;
//Stored patterns decleration
int patternCount= 1; //pattern count
int defPattern; //default pattern
int patterns; //stored patterns
void setup(){
lcdInit();
delay(2500);
lcd.clear();
lcd.print("Loading... ");
lcd.setCursor(0, 1);
lcd.print("Initiating Buzzer ");
BuzzInit();
delay(1000);
lcd.setCursor(0, 1);
lcd.print("Initiating Servo ");
servoInit();
delay(1000);
lcd.setCursor(0, 1);
lcd.print("Initiating Encoder ");
RotaryInit();
delay(1000);
lcd.setCursor(0, 1);
lcd.print("Retreving Storage ");
StorageRetrive();
lcd.setCursor(0, 3);
lcd.print("System is Initiated ");
delay(1000);
lcd.clear();
}
void loop(){
lcd.clear();
int selectedMenu = selectMenu();
delay(150);
if (selectedMenu == 1){
timerMenu();
}
else if (selectedMenu == 2){
int selectedAction = patternMenu();
delay(150);
if (selectedAction == 1){
int selectedPatern = selectPattern_Menu();
runPattern();
}
else if (selectedAction == 2){
createPattern_Menu();
}
else if (selectedAction == 3){
int selectedStep = editStorage_Menu();
delay(150);
}
}
else if (selectedMenu == 3){
int selectedOption = optionMenu();
delay(150);
if (selectedOption == 1){
setBuzz_Volume();
}
else if (selectedOption == 2){
callibrateServo();
}
else if (selectedOption == 3){
selfTest();
}
}
//End of the action taken and return to the main menu
beebTwice();
}
//lcd Initiation
void lcdInit(){
lcd.begin(20, 4);
lcd.print(" PIT System ");
lcd.setCursor(4, 1);
lcd.print("Version: 1.2");
}
//Buzzer Inititation
void BuzzInit(){
pinMode(BuzzPin, OUTPUT);
tone(BuzzPin, freq, 50);
delay(25);
tone(BuzzPin, freq, 50);
delay(25);
tone(BuzzPin, freq, 25);
}
//Buzzer beeb
void beeb(){
tone(BuzzPin, freq, 50);
delay(25);
tone(BuzzPin, freq, 50);
}
//Buzzer beeb extended
void beebTwice(){
tone(BuzzPin, freq, 50);
delay(25);
tone(BuzzPin, freq, 50);
delay(100);
tone(BuzzPin, freq, 50);
delay(25);
tone(BuzzPin, freq, 50);
}
//Servo motor Initiation
void servoInit(){
myservo.attach(3);
pinMode(up_sw, INPUT_PULLUP);
pinMode(down_sw, INPUT_PULLUP);
if (digitalRead(up_sw) == HIGH){
int check= 0;
myservo.write(45);
lcd.setCursor(0, 2);
lcd.print("verifying Up");
while(digitalRead(up_sw) == HIGH){
if(check <= 500){
delay(10);
check++;
}
else{
servo_state= false;
lcd.setCursor(0, 3);
lcd.print("verification faild");
break;
}
}
check= 0;
myservo.write(135);
lcd.setCursor(0, 2);
lcd.print("verifying Down");
while(digitalRead(down_sw) == HIGH){
if(check <= 500){
delay(10);
check++;
}
else{
servo_state= false;
lcd.setCursor(0, 3);
lcd.print("Faild");
break;
}
}
myservo.write(90);
lcd.setCursor(0, 2);
lcd.print(" ");
lcd.setCursor(0, 3);
lcd.print(" ");
}
else{
int check= 0;
myservo.write(135);
lcd.setCursor(0, 2);
lcd.print("verifying Down");
while(digitalRead(down_sw) == HIGH){
if(check <= 500){
delay(10);
check++;
}
else{
servo_state= false;
lcd.setCursor(0, 3);
lcd.print("Faild");
break;
}
}
myservo.write(90);
lcd.setCursor(0, 2);
lcd.print(" ");
lcd.setCursor(0, 3);
lcd.print(" ");
}
beebTwice();
}
//Servo motor inverting position
void servoInvert(){
if(servo_state == true){
if(digitalRead(up_sw) == LOW){
int check= 0;
myservo.write(135);
while(digitalRead(down_sw) == HIGH){
if(check <= 500){
delay(10);
check++;
}
else{
servo_state= false;
break;
}
}
myservo.write(90);
}
else if(digitalRead(down_sw) == LOW){
int check= 0;
myservo.write(45);
while(digitalRead(up_sw) == HIGH){
if(check <= 500){
delay(10);
check++;
}
else{
servo_state= false;
break;
}
}
myservo.write(90);
}
else{
servo_state= false;
}
}
}
//Rotary Encode Initiation
void RotaryInit(){
pinMode (PinA, INPUT);
pinMode (PinB, INPUT);
pinMode (PinSW, INPUT_PULLUP);
pinMode (BckSW, INPUT_PULLUP);
}
//Select using rotary encoder
int rotarySelect(int pos, int S, int E){
n = digitalRead(PinA);
if ((PinALast == LOW) && (n == HIGH)) {
if (digitalRead(PinB) == LOW) {
pos--;
if (pos == (S-1)) {
pos = E;
}
} else {
pos++;
if (pos == (E+1)) {
pos = S;
}
}
}
PinALast = n;
return pos;
}
//Reset rotary values after selection
void rotaryReset(){
bool PinALast = LOW;
bool n = LOW;
}
//Stored Patterns retreving
void StorageRetrive(){
patternCount= 1;
}
//run pattern function for this version its only running
//default pattern but will be expanded in functions
//with the next versions
void runPattern(){
int pat[] = {5, 2, 5, 2, 10, 2, 15, 2, 30, 1, 45};
lcd.clear();
lcd.print("Default pattern");
lcd.setCursor(7, 3);
lcd.print(0);
lcd.setCursor(10, 3);
lcd.print(" %");
int p= 1;
for(int c= 0; c < pat[0]; c++){
for (int r= 1; r <= pat[((c*2)+1)]; r++){
startTimer(pat[((c+1)*2)]);
if(digitalRead(BckSW) == LOW){
return 0;
}
beeb();
lcd.setCursor(7, 3);
lcd.print((p*100)/9);
lcd.setCursor(10, 3);
lcd.print(" %");
p++;
}
}
}
//Start timer and invert servo after
void startTimer(int t){
lcd.setCursor(0, 1);
lcd.print("Elapsed time: ");
lcd.setCursor(17, 1);
lcd.print("m");
lcd.setCursor(0, 2);
lcd.print("Remaining time: ");
lcd.setCursor(19, 2);
lcd.print("m");
for(int i= 0; i< t; i++){
lcd.setCursor(15, 1);
lcd.print(i);
if ((t-i) == 9){
lcd.setCursor(17, 2);
lcd.print(t-i);
lcd.print(" ");
}
else{
lcd.setCursor(17, 2);
lcd.print(t-i);
}
for(int j= 0; j< 60; j++){
if(digitalRead(BckSW) == LOW){
return 0;
}
delay(1000);
}
}
servoInvert();
}
//Main menu
int selectMenu(){
lcd.setCursor(0, 0);
lcd.print("Select");
lcd.setCursor(0, 1);
lcd.print("1- Timer");
lcd.setCursor(0, 2);
lcd.print("2- Pattern");
lcd.setCursor(0, 3);
lcd.print("3- Options");
int selection= 0;
while(digitalRead(PinSW) == HIGH){
lcd.setCursor(19, selection);
lcd.print(" ");
selection = rotarySelect(selection, 1, 3);
if (selection == 0){
selection+= 1;
}
lcd.setCursor(19, selection);
lcd.print("<");
}
beeb();
rotaryReset();
return selection;
}
//Timer menu
void timerMenu(){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Timer ");
lcd.setCursor(0, 1);
lcd.print("Set timer");
lcd.setCursor(0, 2);
lcd.print("time: ");
lcd.setCursor(9, 2);
lcd.print(" mins");
int t= 1;
while(digitalRead(PinSW) == HIGH){
lcd.setCursor(7, 2);
lcd.print(" ");
t= rotarySelect(t, 1, 30);
lcd.setCursor(7, 2);
lcd.print(t);
}
beeb();
rotaryReset();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Timer ");
startTimer(t);
}
//Pattern Menu
int patternMenu(){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Patterns ");
lcd.setCursor(0, 1);
lcd.print("1- Select pattern");
lcd.setCursor(0, 2);
lcd.print("2- Create pattern");
lcd.setCursor(0, 3);
lcd.print("3- Edit Storage");
int selection= 0;
while(digitalRead(PinSW) == HIGH){
lcd.setCursor(19, selection);
lcd.print(" ");
selection = rotarySelect(selection, 1, 3);
if (selection == 0){
selection+= 1;
}
lcd.setCursor(19, selection);
lcd.print("<");
}
beeb();
rotaryReset();
return selection;
}
//Select Pattern Menu
int selectPattern_Menu(){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Select Patters");
lcd.setCursor(0, 1);
lcd.print("Default Pattern <");
while(digitalRead(PinSW) == HIGH){
}
return 0;
}
//Create pattern Menu
void createPattern_Menu(){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Create Pattern");
lcd.setCursor(0, 2);
lcd.print("This function is");
lcd.setCursor(0, 3);
lcd.print("not yet developed");
delay(1000);
//lcd.print("Repeat: 0 time: 00 m");
//while(digitalRead(PinSW) == HIGH){
//}
//while(digitalRead(PinSW) == HIGH){
//}
return 0;
}
//Edit Storage Menu
int editStorage_Menu(){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Edit patterns");
lcd.setCursor(0, 2);
lcd.print("This function is");
lcd.setCursor(0, 3);
lcd.print("not yet developed");
delay(1000);
return 0;
}
//Options Menu
int optionMenu(){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" Options ");
lcd.setCursor(0, 1);
lcd.print("1- Buzzer Volume");
lcd.setCursor(0, 2);
lcd.print("2- Callibrate servo");
lcd.setCursor(0, 3);
lcd.print("3- Run self Test");
int selection= 0;
while(digitalRead(PinSW) == HIGH){
lcd.setCursor(19, selection);
lcd.print(" ");
selection = rotarySelect(selection, 1, 3);
if (selection == 0){
selection+= 1;
}
lcd.setCursor(19, selection);
lcd.print("<");
}
beeb();
rotaryReset();
return selection;
}
//Buzzer Volume Menu
void setBuzz_Volume(){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Set Buzzer Volume");
lcd.setCursor(0, 2);
lcd.print("volume: ");
lcd.setCursor(11, 2);
lcd.print("%");
while(digitalRead(PinSW) == HIGH){
lcd.setCursor(8, 2);
lcd.print(volume);
volume = rotarySelect(volume, 1, 100);
lcd.setCursor(8, 2);
lcd.print(" ");
}
beeb();
int freq = map(volume, 0, 100, 500, 1000);
}
//Callirate Servo Menu
void callibrateServo(){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Calibrate Servo");
lcd.setCursor(0, 2);
lcd.print("This function is");
lcd.setCursor(0, 3);
lcd.print("not yet developed");
delay(1000);
}
//Self test Menu
void selfTest(){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Self Test");
lcd.setCursor(0, 2);
lcd.print("This function is");
lcd.setCursor(0, 3);
lcd.print("not yet developed");
delay(1000);
}