Ellie Griffiths / Mbed 2 deprecated TeabagControlDevice
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "Servo.h"
00003 #include "TextLCD.h"
00004 #include "DebouncedIn.h"
00005 
00006 TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d4-d7
00007 DebouncedIn setbutton(p5);
00008 DebouncedIn gobutton(p6);
00009 Servo myservo(p21);
00010 DigitalOut leda(LED1);
00011 DigitalOut ledb(LED2);
00012 DigitalOut ledc(LED3);
00013 DigitalOut ledd(LED4);
00014 Timer t;
00015 Timer t2;
00016 
00017 int set();
00018 int dip();
00019 int set (int currentstate);
00020 int displaystate(int currentstate);
00021 int maketea(int currentstate);
00022 int dipping();
00023 int alert();
00024 int alertoff();
00025 
00026 int main() {
00027     int state;
00028     state = 1;
00029     myservo = 1;
00030     lcd.cls();
00031     lcd.printf("Start?\n");
00032     while(1) {
00033        if (setbutton.rising()) {
00034             state = set(state);
00035         }
00036         else if (gobutton.rising()) {
00037             maketea(state);
00038         }       
00039     }
00040 }
00041 
00042 int set(int currentstate) { //For setting which mode to use
00043     t.start(); //Inactivity timer
00044     lcd.cls();
00045     displaystate(currentstate);
00046     while (gobutton != 1 and t < 10) {
00047         if (setbutton.rising()) {
00048             currentstate = currentstate + 1; //Advances state
00049             if (currentstate > 9) { //Loops back to start
00050                 currentstate = 1;
00051             }
00052             lcd.cls();
00053             displaystate(currentstate); //Update current state
00054             wait(0.25); //Debounce
00055             t.reset();
00056             t.start(); //Reset inactivity timer
00057         }
00058     }
00059     lcd.cls();
00060     lcd.printf("Start?\n"); //Reset screen
00061     return currentstate;
00062 }
00063 
00064 int displaystate(int currentstate) { //Displays current setting
00065     if (currentstate == 1) {
00066         lcd.printf("1 min - dip\n");
00067     }
00068     else if (currentstate == 2) {
00069         lcd.printf("2 mins - dip\n");
00070     }
00071     else if (currentstate == 3) {
00072         lcd.printf("3 mins - dip\n");
00073     }
00074     else if (currentstate == 4) {
00075         lcd.printf("1 min - no dip\n");
00076     }
00077     else if (currentstate == 5) {
00078         lcd.printf("2 mins - no dip\n");
00079     }
00080     else if (currentstate == 6) {
00081         lcd.printf("3 mins - no dip\n");
00082     }
00083     else if (currentstate == 7) {
00084         lcd.printf("4 mins - no dip\n");
00085     }
00086     else if (currentstate == 8) {
00087         lcd.printf("5 mins - no dip\n");
00088     }
00089     else if (currentstate == 9) {
00090         lcd.printf("QTEST\n");
00091     }
00092     return 0;
00093 }
00094 
00095 int maketea(int currentstate) { //Main teamaking routine
00096     lcd.cls();
00097     lcd.printf("Brewing...\n");
00098     displaystate(currentstate); //Display status
00099     int time;
00100     int dip;
00101     if (currentstate == 1) { //Initialise time and dipping status
00102         time = 60;
00103         dip = 1;
00104     }
00105     else if (currentstate == 2) {
00106         time = 120;
00107         dip = 1;
00108     }
00109     else if (currentstate == 3) {
00110         time = 180;
00111         dip = 1;
00112     }
00113     else if (currentstate == 4) {
00114         time = 60;
00115         dip = 0;
00116     }
00117     else if (currentstate == 5) {
00118         time = 120;
00119         dip = 0;
00120     }
00121     else if (currentstate == 6) {
00122         time = 180;
00123         dip = 0;
00124     }
00125     else if (currentstate == 7) {
00126         time = 240;
00127         dip = 0;
00128     }
00129     else if (currentstate == 8) {
00130         time = 300;
00131         dip = 0;
00132     }
00133     else if (currentstate == 9) {
00134         time = 10;
00135         dip = 0;
00136     }
00137     myservo = 0; //Dunk teabag
00138     t.start(); //Start tea timer
00139     while (t < time) { //While there's still time left
00140         if (dip == 1) { //If dipping, dip teabag
00141             wait(1);
00142             dipping();
00143         }
00144         if (gobutton.rising()) { //If cancelling
00145             lcd.cls();
00146             lcd.printf("Press again to\n");
00147             lcd.printf("stop brewing.\n");
00148             t2.start(); //Start (secondary) inactivity timer
00149             while (t2 <= 5) {
00150                 if (gobutton.rising()) {
00151                     time = 0; //Resets time to run to 0, thus pulling out of main while loop
00152                     break; //Pulls out of this loop
00153                 }
00154             }
00155             t2.reset();
00156             lcd.cls();
00157             lcd.printf("Brewing...\n");
00158             displaystate(currentstate); //Reset screen to brewing
00159         }            
00160     }
00161     t.reset();
00162     myservo = 1;
00163     lcd.cls();
00164     lcd.printf("Done!\n");
00165     lcd.printf("Reset?\n");
00166     t.start();
00167     while ((!setbutton.rising()) && (!gobutton.rising())) { //If not reset in 90 seconds, alert
00168         if (t > 90) {
00169             alert();
00170         }
00171     }
00172     alertoff();
00173     lcd.cls();
00174     lcd.printf("Start?\n"); //Reset
00175     wait(1);
00176     return 0;
00177 }
00178 
00179 int dipping() { //Dipping routine
00180     myservo = 0.5;
00181     wait(1);
00182     myservo = 0;
00183     return 0;
00184 }    
00185 
00186 int alert() { //Alert using on-board LEDs
00187     int y;
00188     for ( y = 0; y != 1; y++ ) {
00189         leda = 1;
00190         ledb = 0;
00191         ledc = 1;
00192         ledd = 0;
00193         wait(0.2);
00194         leda = 0;
00195         ledb = 1;
00196         ledc = 0;
00197         ledd = 1;
00198         wait(0.2);
00199     }
00200     return 0;
00201 }
00202 
00203 int alertoff() { //Resets on-board LEDs to 0
00204     leda = 0;
00205     ledb = 0;
00206     ledc = 0;
00207     ledd = 0;
00208     return 0;
00209 }