/* this code is a user interface for our project the curing box 
 *  it is simple and easy to use 
 *  FABLAB IRBID
 *  V 1.0.0
 *  a very appreciated graphics library is used in this project, the u8g2 lib 
 *  feel free to upgrade and fix this code 
 */




//LCD libraries
#include <U8g2lib.h>

#include <SPI.h>


//Push buttons green=A red=B
int pushA=7;
int pushB=8;
  int Aval=0;
  int Bval=0;
 
//Potentiometers left=1 middle=2 right=3
int pot1=A1;
int pot2=A2;
int pot3=A3;
  int p1val=0;
  int p2val=0;
  int p3val=0;
  
  long hours=0; 
  long minutes=0;
  long seconds=0;
  int speeds=0; // rotational speed of the rotating plate
  int brightness=0;// brughtness of the uv leds


int Thermo=A4;// the temperature sensor lm35
  int thermoval=0;// the measured milli volts of the lm35
  int temp=0;// the temperature in celcuis

int leds=5;// the output of the leds intensity control PWM
int motor=6;//the output of the motor speed control PWM

int menu=1;// the menu reference number

long alltime=0;// the running time in milliseconds
int pause=0; // state of the device if paused or not

U8G2_ST7920_128X64_F_SW_SPI u8g2(U8G2_R2, /* clock=*/ 13, /* data=*/ 11, /* CS=*/ 10, /* reset=*/ 9);// setting the spi pins on the lcd arduino

void setup() {


  //setting pins to input:
  pinMode(pushA,INPUT);
  pinMode(pushB,INPUT);
  //setting pins to output:
  pinMode(leds,OUTPUT);
  pinMode(motor,OUTPUT);




u8g2.begin();// intializing the lcd screen
u8g2.enableUTF8Print();  // enabling print command 
u8g2.setFont(u8g2_font_t0_11_tr); // setting the font displayed on the LCD
//The welcome screen:
do{
                   
      u8g2.drawStr(23, 20, "CURING   BOX");
      u8g2.drawStr(23, 50, "Fablab Irbid");
      
 } while ( u8g2.nextPage() );
 delay(2000);
}

void loop() {
   u8g2.firstPage();
  do {
         //push buttons read: 
          Aval=digitalRead(pushA);
            Bval=digitalRead(pushB);
            
            //Potentiometer analog read:
                p1val=analogRead(pot1);
                  p2val=analogRead(pot2);
                     p3val=analogRead(pot3);
                     
                        //thermocouple analogue read:
                        thermoval=analogRead(Thermo);
                          temp = ( thermoval/1024.0)*500;


 
if(Aval==1&&menu<3){ // when pressing button a in the third menu
                     menu=menu+1; // go to the next menu
                      delay(500);
                   }
  else if(Bval==1&&menu==3){// when pressing button b in the third menu
                      menu=1;// go to menu 1
                        delay(500);
                           }
       
      else if(alltime==0&&menu==3){// when time is finished in the 3rd menu
                     menu=4;// go to menu 4
                      delay(500);
                        }
              else if(Aval==1&&menu==4){// when pressing button a in the third menu
                       menu=1;// go to menu 1
                         delay(500);
                                       }
                                       else if(Bval==1&&menu==2){// when pressing button b in the second menu
                                        menu=1;
                                       delay(500);
                                       }


if(menu==1){// the first menu screen
                        // turn off leds and motor :
                        analogWrite(motor,0);
                        analogWrite(leds,0);  
               //the title of the menu:         
            u8g2.drawStr(20, 8, "Set Time");
            // button next :
            u8g2.drawCircle(112, 15, 15, U8G2_DRAW_ALL); //draw circle
              u8g2.drawLine(93, 0, 93, 63);
               u8g2.drawStr(101, 18, "Next");// print next
  
             u8g2.drawFrame(4,32,85,15);// draw frame
               u8g2.drawStr(5, 30, "hr : min : sec");//print hr organization
  
   
            hours=map(p1val,1,1020,12,0);// convert potentiomerter 1 values to hours
               minutes=map(p2val,1,1020,60,0);// convert potentiomerter 2 values to minutess
                   seconds=map(p3val,1,1020,60,0);// convert potentiomerter 3 values to seconds
                   
   alltime=(hours*3600000)+(minutes*60000)+(seconds*1000); // save the hours minutes and seconds in alltime in milliseconds

             u8g2.setCursor(8, 43);
                u8g2.print(hours);// print the hours
                  u8g2.setCursor(23, 43);
                    u8g2.print(":");
           
            u8g2.setCursor(40, 43);
                u8g2.print(minutes);//print the minutes
                  u8g2.setCursor(59, 43);
                    u8g2.print(":");
           
            u8g2.setCursor(75, 43);
              u8g2.print(seconds);  //print the seconds    
            }

  else if(menu==2){// the second menu
    
                  //print the title of this menu:
                   u8g2.drawStr(5, 8,   "Set Speed &");
                     u8g2.drawStr(5, 19, " Brightness");

                  
                   
                      
                   u8g2.drawCircle(112, 15, 15, U8G2_DRAW_ALL); //draw upper circle
                      u8g2.drawStr(101, 19, "Done"); // print done
                        u8g2.drawLine(93, 0, 93, 63);

                  u8g2.drawCircle(112, 48, 15, U8G2_DRAW_ALL); //draw lower circle
                      u8g2.drawStr(101, 52, "Back");// print back
                      
                  speeds=map(p2val,1,1020,100,0);// convert potentiomerter 2 values to motor speed percent
                      brightness=map(p3val,1,1020,100,0);// convert potentiometer 3 values to brightness percent
    //set time:
                  u8g2.setCursor(0, 40);
                    u8g2.print("Speed:");
                      u8g2.setCursor(45, 40);
                        u8g2.print(speeds);
                          u8g2.setCursor(65, 40);
                            u8g2.print("%");
   //set brightness:
                u8g2.setCursor(0, 53);
                  u8g2.print("Bright:");
                    u8g2.setCursor(45, 53);
                      u8g2.print(brightness);
                        u8g2.setCursor(65, 53);
                          u8g2.print("%");

                          
                analogWrite(motor,255*speeds/100);           //send motor speed out of 255
                  analogWrite(leds,255*brightness/100);      //send leds brightness out of 255 
                }
  
  else if (menu==3){// the third menu 

                      speeds=map(p2val,1,1020,100,0);// convert potentiomerter 2 values to motor speed percent
                      brightness=map(p3val,1,1020,100,0);// convert potentiometer 3 values to brightness percent
    
                    u8g2.drawCircle(112, 15, 15, U8G2_DRAW_ALL); //upper circle
                       u8g2.drawCircle(112, 48, 15, U8G2_DRAW_ALL); //lower circle
                          u8g2.drawStr(101, 52, "Stop");
                            u8g2.drawLine(93, 0, 93, 63);
                                    


                    if(Aval==1&&pause==0){
                      pause=1;
                        delay(200);
                                          }
                      else if(Aval==1&&pause==1){
                        pause=0;
                          delay(200);
                                         }

                    if(pause==0){ // when not pausing check if there is time remaining
                       u8g2.drawStr(106, 19, "||");
                           u8g2.drawStr(20, 8, "Curing...");  
                                    analogWrite(motor,255*speeds/100);
                                   analogWrite(leds,255*brightness/100);
 
                                     

                  if(alltime>=0){ // when there still time keep going
                                  hours=alltime/3600000;
                                   u8g2.setCursor(8, 59);
                                     u8g2.print(hours);
                                      u8g2.setCursor(23, 59);
                                       u8g2.print(":");
                           
                                 minutes=(alltime/60000)%60;
                                   u8g2.setCursor(40, 59);
                                     u8g2.print(minutes);
                                      u8g2.setCursor(59, 59);
                                        u8g2.print(":");
                          
                                 seconds=(alltime%60000)/1000;     
                                   u8g2.setCursor(75, 59); 
                                      u8g2.print(seconds);
          
          
                                 delay(50); 
                                   alltime=alltime-160;


                                   
                                   
                                }
                              else if(alltime<0){
                                menu=4;  
                              }

                    }
                  else if(pause==1){
                     u8g2.drawStr(101, 19, "Play");
                        u8g2.drawStr(20, 8, "Paused");
                        analogWrite(motor,0);
                        analogWrite(leds,0);  
                                   }
    
                                    
//display temp
                    u8g2.setCursor(0, 46);
                      u8g2.print("Temp:");
                        u8g2.setCursor(45, 46);
                          u8g2.print(temp);
                            u8g2.setCursor(63, 46);
                              u8g2.print("'C");

//remap speeds and brightnes
                      speeds=map(p2val,1,1020,100,0);
                        brightness=map(p3val,1,1020,100,0);
    //display speed
                  u8g2.setCursor(0, 20);
                    u8g2.print("Speed:");
                      u8g2.setCursor(45, 20);
                        u8g2.print(speeds);
                          u8g2.setCursor(65, 20);
                            u8g2.print("%");
                //display brightness            
                u8g2.setCursor(0, 33);
                  u8g2.print("Bright:");
                    u8g2.setCursor(45, 33);
                      u8g2.print(brightness);
                        u8g2.setCursor(65, 33);
                          u8g2.print("%");


                    delay(50);
   
                    }
else if(menu=4){// the fourth menu
  
                        analogWrite(motor,0);
                        analogWrite(leds,0);  
      //display finish when the job is done:                  
      u8g2.drawStr(23, 30, "Finish!");
            
            u8g2.drawCircle(112, 15, 15, U8G2_DRAW_ALL); //draw upper cirle
              u8g2.drawLine(93, 0, 93, 63);
               u8g2.drawStr(104, 18, "New");
               
  
}
  
      } while ( u8g2.nextPage() );


}
