Improvement version of UI team 2

From UBC Wiki

#include <Adafruit_LiquidCrystal.h>

#include "Wire.h"

#include "Adafruit_LiquidCrystal.h"

#include <DHT.h>

#define LIMIT_SWITCH 2

#define MOTOR 3

#define HEATER 4

#define FAN 6

#define SENSOR 7

#define Buzzer 9

#define LEFT_button 13

#define RIGHT_button 12

#define OK_button 11

#define BACK_button 10

#define C 261

#define CS 277

#define D 294

#define DS 311

#define E 330

#define FA 349

#define FS 370

#define G 392

#define GS 415

#define A 440

#define GREEN A2

#define RED A1

Adafruit_LiquidCrystal lcd(0);

#define DHTType DHT22

DHT dht = DHT(SENSOR, DHTType);

int pg_num = 0;

int tim, temp, humid, preset, complete, choice, control_cursor;

double speedy;

int tim_mins = 0;

int tim_sec = 0;

int count = 0;

void setup() {

  pinMode(LEFT_button, INPUT_PULLUP);

  pinMode(RIGHT_button, INPUT_PULLUP);

  pinMode(OK_button, INPUT_PULLUP);

  pinMode(BACK_button, INPUT_PULLUP);

  pinMode(MOTOR, OUTPUT);

  pinMode(FAN, OUTPUT);

  pinMode(LIMIT_SWITCH, INPUT);

  pinMode(HEATER, OUTPUT);

  pinMode(SENSOR, INPUT);

  pinMode(Buzzer, OUTPUT);

  pinMode(RED, OUTPUT);

  pinMode(GREEN, OUTPUT);

  lcd.begin (20,4);

  lcd.setCursor(0,0);

  Serial.begin(9600);

  dht.begin();

}

int button_checks() {

  while (digitalRead(13) == 1 && digitalRead(12) == 1 && digitalRead(11) == 1 && digitalRead(10) == 1 && preset == 0){

    if (pg_num == 2 && complete == 0){

      time_func();

      if (Check_Door() == 0){

        End_cycle();

        pg_error();

        pg_num = 0;

        tim = 0;

      }

    }

  }

  if(digitalRead (13) == 0) {

    return 1;

  }

  else if (digitalRead (12) == 0) {

    return 2;

  }

  else if (digitalRead (11) == 0) {

    return 3;

  }

  else if (digitalRead (10) == 0) {

    return 4;

  }

  delay(200);

}

void button_respone () {

  if (choice == 1) {

      if (pg_num == 1){

        control_cursor = 2;

        lcd.setCursor(control_cursor,2);

        delay(200);

      }

      else if (pg_num == 5){

        if (count == 5){

          pos_cursor_confirm();

          count--;

          delay(200);

        }

        else if (count == 4) {

          pos_cursor_humid();

          count--;

          delay(200);

        }

        else if (count == 3) {

          pos_cursor_speed();

          count--;

          delay(200);

        }

        else if (count == 2) {

          pos_cursor_temp();

          count--;

          delay(200);

        }

        else if (count == 1){

          pos_cursor_time();

          count--;

          delay(200);

        }

      }

    }

    else if (choice == 2) {

      if (pg_num == 1) {

        control_cursor = 10;

        lcd.setCursor(control_cursor,2);

        delay(200);

      }

      if (pg_num == 5){

        if (count == 0){

          pos_cursor_temp();

          count++;

          delay(200);

        }

        else if (count == 1) {

          pos_cursor_speed();

          count++;

          delay(200);

        }

        else if (count == 2) {

          pos_cursor_humid();

          count++;

          delay(200);

        }

        else if (count == 3) {

          pos_cursor_confirm();

          count++;

          delay(200);

        }

        else if (count == 4) {

          pos_cursor_back();

          count++;

          delay(200);

        }

      }

    }

    else if (choice == 3) {

      if (control_cursor == 2){

        /*

         * AUTO CONFIGURE

         */

        tim_mins = 60;

        temp = 100;

        humid = 100;

        speedy = 100;

        pg_num = 2;

        delay(200);

      }

      else if (control_cursor == 10){

        pg_num = 4;

        delay(200);

      }

    if (pg_num == 5) {

      if (count == 0){

        if (tim_mins > 58){

          tim_mins = 0;

        }

        tim_mins = tim_mins + 5;

        delay(200);

      }

      else if (count == 1){

        if (temp > 75){

          temp = -25;

        }

        temp = temp + 25;

        delay(200);

      }

      else if (count == 2){

        if (speedy > 75){

          speedy = -25;

        }

        speedy = speedy + 25;

        delay(200);

      }

      else if (count == 3){

        if (humid > 75){

          humid = -25;

        }

        humid = humid + 25;

        delay(200);

      }

      else if (count == 4){

        pg_num = 2;

        delay(200);

      }

      else if (count == 5){

        pg_num = 0;

        delay(200);

        }

      }

      if (pg_num == 7){

        pg_num = 0;

        preset = 1;

        complete = 0;

      }

    }

    else if (choice == 4) {

      if (pg_num == 2){

        End_cycle();

        tim = 0;

        pg_num = 0;

        control_cursor = 2;

        delay(200);

      }

      else if (pg_num == 5){

        if (count == 0){

          if (tim_mins < 4) {

            tim_mins = 22;

          }

          tim_mins = tim_mins - 2;

        delay(200);

        }

        else if (count == 1){

          if (temp == 0){

          temp = 125;

          }

          temp = temp - 25;

          delay(200);

        }

        else if (count == 2){

          if (speedy == 0){

          speedy = 125;

          }

          speedy = speedy - 25;

          delay(200);

        }

        else if (count == 3){

          if (humid == 0){

            humid = 125;

          }

          humid = humid - 25;

          delay(200);

        }

        else if (count == 4){

          pg_num = 0;

          complete = 0;

        }

        delay(200);

      }

  }

}

void time_func () {

  int time_print = tim_mins - 1;

 

    tim++;

    delay(1000);

    if (time_print >= 10){

    lcd.setCursor(5,1);

    lcd.print(time_print);

    lcd.print(":");

    }

    else if (time_print < 10){

      lcd.setCursor(6,1);

      lcd.print(time_print);

      lcd.print(":");

    }

   

    if (time_print >= 0){

    tim_sec = 60 - tim;

    lcd.setCursor(8,1);

    }

   

    if(tim_sec > 9){

    lcd.print(tim_sec);

    }

    else if(tim_sec < 10 && tim_sec > 0){

      lcd.setCursor(8,1);

      lcd.print("0");

      lcd.setCursor(9,1);

      lcd.print(tim_sec);

    }

    else if (tim_sec == 0){

      lcd.print(tim_sec);

      tim_mins--;

      tim = 0;

    }

    if (time_print == 0 && tim_sec == 0){

        End_cycle();

        pg_complete();

        complete = 1;

        pg_num = 7;

        choice = button_checks();

        button_respone();

        delay(200);

    }

    double currentT = dht.readTemperature();

    double currentH = dht.readHumidity();

   

    lcd.setCursor(16,1);

    lcd.print(currentT,0);

    lcd.setCursor(17,2);

    lcd.print(currentH,0);

}

void LCD_screen () {

  if (pg_num == 0) {  

    pg_intro();

    pg_num = 1;

    temp = 0;

    speedy = 0;

    humid = 0;

    tim_mins = 2;

    preset = 0;

    complete = 0;

  }

  else if (pg_num == 4) {

      pg_manual();

      count = 0;

      control_cursor = 0;

      pg_num = 5;

      preset = 0;

  }

    while (pg_num == 5){

     

      choice = button_checks();

      button_respone();

      delay(200);

      if (choice == 3 || choice == 4) {

        pg_manual();

       

        if (count == 0){

          pos_cursor_time();

        }

        else if (count == 1) {

          pos_cursor_temp();

        }

        else if (count == 2) {

          pos_cursor_speed();

        }

        else if (count == 3) {

          pos_cursor_humid();

        }

      }

    }

   

    if (pg_num == 2) {

      pg_summary();

     

      /*

       * The output

       */

      Serial.println(tim_mins);

      Serial.println(temp);

      Serial.println(speedy,0);

      Serial.println(humid);

     

      Start_buzzer();

      FAN_on();

      HEATER_FUNC();

      MOTOR_on();

      Serial.println("done motor run");

    }

  if (pg_num != 0) {

    choice = button_checks();

    button_respone();

    }

}

void pos_cursor_time (){

  lcd.setCursor(0,1);

}

void pos_cursor_temp (){

  lcd.setCursor(9,1);

}

void pos_cursor_speed(){

  lcd.setCursor(0,2);

}

void pos_cursor_humid(){

  lcd.setCursor(9,2);

}

void pos_cursor_confirm(){

  lcd.setCursor(5,3);

}

void pos_cursor_back(){

  lcd.setCursor(15,3);

}

void pg_intro () {

  lcd.clear();

  lcd.setCursor(0,0);

  lcd.print("Pick drying options");

  lcd.setCursor(3,2);

  lcd.blink();

  lcd.print("Auto");

  lcd.setCursor(11,2);

  lcd.print("Manual");

  lcd.setCursor(2,2);

  control_cursor = 2;

}

void pg_summary () {

  lcd.clear();

  lcd.setCursor(0,0);

  lcd.print("Summary");

  lcd.setCursor(0,1);

  lcd.print("Time:");

  lcd.setCursor(11,1);

  lcd.print("Temp:");

  lcd.setCursor(0,2);

  lcd.print("Speed:");

  lcd.setCursor(6,2);

  lcd.print(speedy, 0);

  lcd.print("%");

  lcd.setCursor(11,2);

  lcd.print("Humid:");

  lcd.setCursor(1,3);

  lcd.print("Hold BACK to STOP");

}

void pg_manual () {

  lcd.clear();

  lcd.setCursor(0,0);

  lcd.print("Manual");

  lcd.setCursor(1,1);

  lcd.print("Time:");

  lcd.setCursor(6,1);

  lcd.print(tim_mins);

  lcd.print("m");

  lcd.setCursor(10,1);

  lcd.print("Temp:");

  lcd.setCursor(15,1);

  lcd.print(temp);

  lcd.print("%");

  lcd.setCursor(1,2);

  lcd.print("Spd:");

  lcd.setCursor(5,2);

  lcd.print(speedy,0);

  lcd.print("%");

  lcd.setCursor(10,2);

  lcd.print("Humid:");

  lcd.setCursor(16,2);

  lcd.print(humid);

  lcd.print("%");

  lcd.setCursor(6,3);

  lcd.print("Confirm");

  lcd.setCursor(16,3);

  lcd.print("Back");

  lcd.setCursor(0,1);

  lcd.blink();

}

void pg_complete(){

  lcd.clear();

  lcd.setCursor(2,1);

  lcd.print("Drying Complete");

  lcd.setCursor(0,2);

  lcd.print("Press OK to confirm");

}

void pg_error(){

  lcd.clear();

  lcd.setCursor(6,0);

  lcd.print("ERROR!!");

  lcd.setCursor(2,1);

  lcd.print("The door is open");

  lcd.setCursor(5,3);

  lcd.print("Press BACK");

}

void loop () {

  LCD_screen();

}

void MOTOR_on(){

double speedi = speedy/100*255;

double spd = 0;

Serial.println("motor run");

while(speedi > spd){

 

  spd = spd + 5;

  analogWrite(MOTOR, spd);

  delay(40);

  Serial.println(spd);

  }

}

int Check_Door(){

 

  if(digitalRead(LIMIT_SWITCH) == HIGH){

    return 1;

  }

  else

    return 0;

}

void HEATER_FUNC(){


double currentT = dht.readTemperature();

double currentH = dht.readHumidity();

  if(temp > currentT){

    if(humid > currentH){

    digitalWrite(HEATER,HIGH);

    }

  }

  else {

  digitalWrite(HEATER,LOW);

  }

}

void FAN_off(){

  int num = 255;

  while (num > 0){

    analogWrite(FAN ,num);

    num = num - 5;

    delay(20);

  }

}

void FAN_on(){

      int num = 0;

  while (num < 254){

    analogWrite(FAN,num);

    num = num + 5;

    delay(20);

  }

}

void MOTOR_off(double speedy){


  double speedi = speedy/100*255;

  while(speedi > -1){

    analogWrite(MOTOR, speedi);

    speedi = speedi - 5;

    delay(40);

  }

}

void Start_buzzer(){

  tone(Buzzer,C);

  digitalWrite(GREEN, HIGH);

  delay(700);

  digitalWrite(GREEN, LOW);

   

  tone(Buzzer,D);

  digitalWrite(RED, HIGH);

  delay(700);

  digitalWrite(RED, LOW);

  tone(Buzzer,E);

  digitalWrite(GREEN, HIGH);

  digitalWrite(RED, HIGH);

  delay(700);

  digitalWrite(GREEN, LOW);

  digitalWrite(RED, LOW);

   

  noTone(Buzzer);

  digitalWrite(RED, HIGH);

  delay(1000);

}

void End_buzzer(){

  tone(Buzzer,DS);

  digitalWrite(RED, HIGH);

  delay(400);

  digitalWrite(RED, LOW);

  noTone(Buzzer);

  delay(500);

   

  tone(Buzzer,DS);

  digitalWrite(RED, HIGH);

  delay(400);

  digitalWrite(RED, LOW);

  noTone(Buzzer);

 

  delay(500);

  tone(Buzzer,DS);

  digitalWrite(RED, HIGH);

  delay(400);

  digitalWrite(RED, LOW);

  noTone(Buzzer);

  delay(500);

 

  tone(Buzzer, G);

  digitalWrite(RED, HIGH);

  digitalWrite(GREEN, HIGH);

  delay(700);

  noTone(Buzzer);

  digitalWrite(RED, LOW);

  digitalWrite(GREEN, LOW);

}

void End_cycle(){

  FAN_off();

  digitalWrite(HEATER,LOW);

  MOTOR_off(speedy);

  End_buzzer();

}