An updated and more polished version of the earlier TeaBasic. This is now finished and works like a treat.
main.cpp@0:36264f703f1c, 2011-06-29 (annotated)
- Committer:
- Eggotape
- Date:
- Wed Jun 29 13:20:33 2011 +0000
- Revision:
- 0:36264f703f1c
Haven\t tried it with water yet, so may want to consider slowing the Servo down to avoid splashing.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Eggotape | 0:36264f703f1c | 1 | #include "mbed.h" |
Eggotape | 0:36264f703f1c | 2 | #include "Servo.h" |
Eggotape | 0:36264f703f1c | 3 | #include "TextLCD.h" |
Eggotape | 0:36264f703f1c | 4 | #include "DebouncedIn.h" |
Eggotape | 0:36264f703f1c | 5 | |
Eggotape | 0:36264f703f1c | 6 | TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d4-d7 |
Eggotape | 0:36264f703f1c | 7 | DebouncedIn setbutton(p5); |
Eggotape | 0:36264f703f1c | 8 | DebouncedIn gobutton(p6); |
Eggotape | 0:36264f703f1c | 9 | Servo myservo(p21); |
Eggotape | 0:36264f703f1c | 10 | DigitalOut leda(LED1); |
Eggotape | 0:36264f703f1c | 11 | DigitalOut ledb(LED2); |
Eggotape | 0:36264f703f1c | 12 | DigitalOut ledc(LED3); |
Eggotape | 0:36264f703f1c | 13 | DigitalOut ledd(LED4); |
Eggotape | 0:36264f703f1c | 14 | Timer t; |
Eggotape | 0:36264f703f1c | 15 | Timer t2; |
Eggotape | 0:36264f703f1c | 16 | |
Eggotape | 0:36264f703f1c | 17 | int set(); |
Eggotape | 0:36264f703f1c | 18 | int dip(); |
Eggotape | 0:36264f703f1c | 19 | int set (int currentstate); |
Eggotape | 0:36264f703f1c | 20 | int displaystate(int currentstate); |
Eggotape | 0:36264f703f1c | 21 | int maketea(int currentstate); |
Eggotape | 0:36264f703f1c | 22 | int dipping(); |
Eggotape | 0:36264f703f1c | 23 | int alert(); |
Eggotape | 0:36264f703f1c | 24 | int alertoff(); |
Eggotape | 0:36264f703f1c | 25 | |
Eggotape | 0:36264f703f1c | 26 | int main() { |
Eggotape | 0:36264f703f1c | 27 | int state; |
Eggotape | 0:36264f703f1c | 28 | state = 1; |
Eggotape | 0:36264f703f1c | 29 | myservo = 1; |
Eggotape | 0:36264f703f1c | 30 | lcd.cls(); |
Eggotape | 0:36264f703f1c | 31 | lcd.printf("Start?\n"); |
Eggotape | 0:36264f703f1c | 32 | while(1) { |
Eggotape | 0:36264f703f1c | 33 | if (setbutton.rising()) { |
Eggotape | 0:36264f703f1c | 34 | state = set(state); |
Eggotape | 0:36264f703f1c | 35 | } |
Eggotape | 0:36264f703f1c | 36 | else if (gobutton.rising()) { |
Eggotape | 0:36264f703f1c | 37 | maketea(state); |
Eggotape | 0:36264f703f1c | 38 | } |
Eggotape | 0:36264f703f1c | 39 | } |
Eggotape | 0:36264f703f1c | 40 | } |
Eggotape | 0:36264f703f1c | 41 | |
Eggotape | 0:36264f703f1c | 42 | int set(int currentstate) { //For setting which mode to use |
Eggotape | 0:36264f703f1c | 43 | t.start(); //Inactivity timer |
Eggotape | 0:36264f703f1c | 44 | lcd.cls(); |
Eggotape | 0:36264f703f1c | 45 | displaystate(currentstate); |
Eggotape | 0:36264f703f1c | 46 | while (gobutton != 1 and t < 10) { |
Eggotape | 0:36264f703f1c | 47 | if (setbutton.rising()) { |
Eggotape | 0:36264f703f1c | 48 | currentstate = currentstate + 1; //Advances state |
Eggotape | 0:36264f703f1c | 49 | if (currentstate > 9) { //Loops back to start |
Eggotape | 0:36264f703f1c | 50 | currentstate = 1; |
Eggotape | 0:36264f703f1c | 51 | } |
Eggotape | 0:36264f703f1c | 52 | lcd.cls(); |
Eggotape | 0:36264f703f1c | 53 | displaystate(currentstate); //Update current state |
Eggotape | 0:36264f703f1c | 54 | wait(0.25); //Debounce |
Eggotape | 0:36264f703f1c | 55 | t.reset(); |
Eggotape | 0:36264f703f1c | 56 | t.start(); //Reset inactivity timer |
Eggotape | 0:36264f703f1c | 57 | } |
Eggotape | 0:36264f703f1c | 58 | } |
Eggotape | 0:36264f703f1c | 59 | lcd.cls(); |
Eggotape | 0:36264f703f1c | 60 | lcd.printf("Start?\n"); //Reset screen |
Eggotape | 0:36264f703f1c | 61 | return currentstate; |
Eggotape | 0:36264f703f1c | 62 | } |
Eggotape | 0:36264f703f1c | 63 | |
Eggotape | 0:36264f703f1c | 64 | int displaystate(int currentstate) { //Displays current setting |
Eggotape | 0:36264f703f1c | 65 | if (currentstate == 1) { |
Eggotape | 0:36264f703f1c | 66 | lcd.printf("1 min - dip\n"); |
Eggotape | 0:36264f703f1c | 67 | } |
Eggotape | 0:36264f703f1c | 68 | else if (currentstate == 2) { |
Eggotape | 0:36264f703f1c | 69 | lcd.printf("2 mins - dip\n"); |
Eggotape | 0:36264f703f1c | 70 | } |
Eggotape | 0:36264f703f1c | 71 | else if (currentstate == 3) { |
Eggotape | 0:36264f703f1c | 72 | lcd.printf("3 mins - dip\n"); |
Eggotape | 0:36264f703f1c | 73 | } |
Eggotape | 0:36264f703f1c | 74 | else if (currentstate == 4) { |
Eggotape | 0:36264f703f1c | 75 | lcd.printf("1 min - no dip\n"); |
Eggotape | 0:36264f703f1c | 76 | } |
Eggotape | 0:36264f703f1c | 77 | else if (currentstate == 5) { |
Eggotape | 0:36264f703f1c | 78 | lcd.printf("2 mins - no dip\n"); |
Eggotape | 0:36264f703f1c | 79 | } |
Eggotape | 0:36264f703f1c | 80 | else if (currentstate == 6) { |
Eggotape | 0:36264f703f1c | 81 | lcd.printf("3 mins - no dip\n"); |
Eggotape | 0:36264f703f1c | 82 | } |
Eggotape | 0:36264f703f1c | 83 | else if (currentstate == 7) { |
Eggotape | 0:36264f703f1c | 84 | lcd.printf("4 mins - no dip\n"); |
Eggotape | 0:36264f703f1c | 85 | } |
Eggotape | 0:36264f703f1c | 86 | else if (currentstate == 8) { |
Eggotape | 0:36264f703f1c | 87 | lcd.printf("5 mins - no dip\n"); |
Eggotape | 0:36264f703f1c | 88 | } |
Eggotape | 0:36264f703f1c | 89 | else if (currentstate == 9) { |
Eggotape | 0:36264f703f1c | 90 | lcd.printf("QTEST\n"); |
Eggotape | 0:36264f703f1c | 91 | } |
Eggotape | 0:36264f703f1c | 92 | return 0; |
Eggotape | 0:36264f703f1c | 93 | } |
Eggotape | 0:36264f703f1c | 94 | |
Eggotape | 0:36264f703f1c | 95 | int maketea(int currentstate) { //Main teamaking routine |
Eggotape | 0:36264f703f1c | 96 | lcd.cls(); |
Eggotape | 0:36264f703f1c | 97 | lcd.printf("Brewing...\n"); |
Eggotape | 0:36264f703f1c | 98 | displaystate(currentstate); //Display status |
Eggotape | 0:36264f703f1c | 99 | int time; |
Eggotape | 0:36264f703f1c | 100 | int dip; |
Eggotape | 0:36264f703f1c | 101 | if (currentstate == 1) { //Initialise time and dipping status |
Eggotape | 0:36264f703f1c | 102 | time = 60; |
Eggotape | 0:36264f703f1c | 103 | dip = 1; |
Eggotape | 0:36264f703f1c | 104 | } |
Eggotape | 0:36264f703f1c | 105 | else if (currentstate == 2) { |
Eggotape | 0:36264f703f1c | 106 | time = 120; |
Eggotape | 0:36264f703f1c | 107 | dip = 1; |
Eggotape | 0:36264f703f1c | 108 | } |
Eggotape | 0:36264f703f1c | 109 | else if (currentstate == 3) { |
Eggotape | 0:36264f703f1c | 110 | time = 180; |
Eggotape | 0:36264f703f1c | 111 | dip = 1; |
Eggotape | 0:36264f703f1c | 112 | } |
Eggotape | 0:36264f703f1c | 113 | else if (currentstate == 4) { |
Eggotape | 0:36264f703f1c | 114 | time = 60; |
Eggotape | 0:36264f703f1c | 115 | dip = 0; |
Eggotape | 0:36264f703f1c | 116 | } |
Eggotape | 0:36264f703f1c | 117 | else if (currentstate == 5) { |
Eggotape | 0:36264f703f1c | 118 | time = 120; |
Eggotape | 0:36264f703f1c | 119 | dip = 0; |
Eggotape | 0:36264f703f1c | 120 | } |
Eggotape | 0:36264f703f1c | 121 | else if (currentstate == 6) { |
Eggotape | 0:36264f703f1c | 122 | time = 180; |
Eggotape | 0:36264f703f1c | 123 | dip = 0; |
Eggotape | 0:36264f703f1c | 124 | } |
Eggotape | 0:36264f703f1c | 125 | else if (currentstate == 7) { |
Eggotape | 0:36264f703f1c | 126 | time = 240; |
Eggotape | 0:36264f703f1c | 127 | dip = 0; |
Eggotape | 0:36264f703f1c | 128 | } |
Eggotape | 0:36264f703f1c | 129 | else if (currentstate == 8) { |
Eggotape | 0:36264f703f1c | 130 | time = 300; |
Eggotape | 0:36264f703f1c | 131 | dip = 0; |
Eggotape | 0:36264f703f1c | 132 | } |
Eggotape | 0:36264f703f1c | 133 | else if (currentstate == 9) { |
Eggotape | 0:36264f703f1c | 134 | time = 10; |
Eggotape | 0:36264f703f1c | 135 | dip = 0; |
Eggotape | 0:36264f703f1c | 136 | } |
Eggotape | 0:36264f703f1c | 137 | myservo = 0; //Dunk teabag |
Eggotape | 0:36264f703f1c | 138 | t.start(); //Start tea timer |
Eggotape | 0:36264f703f1c | 139 | while (t < time) { //While there's still time left |
Eggotape | 0:36264f703f1c | 140 | if (dip == 1) { //If dipping, dip teabag |
Eggotape | 0:36264f703f1c | 141 | wait(1); |
Eggotape | 0:36264f703f1c | 142 | dipping(); |
Eggotape | 0:36264f703f1c | 143 | } |
Eggotape | 0:36264f703f1c | 144 | if (gobutton.rising()) { //If cancelling |
Eggotape | 0:36264f703f1c | 145 | lcd.cls(); |
Eggotape | 0:36264f703f1c | 146 | lcd.printf("Press again to\n"); |
Eggotape | 0:36264f703f1c | 147 | lcd.printf("stop brewing.\n"); |
Eggotape | 0:36264f703f1c | 148 | t2.start(); //Start (secondary) inactivity timer |
Eggotape | 0:36264f703f1c | 149 | while (t2 <= 5) { |
Eggotape | 0:36264f703f1c | 150 | if (gobutton.rising()) { |
Eggotape | 0:36264f703f1c | 151 | time = 0; //Resets time to run to 0, thus pulling out of main while loop |
Eggotape | 0:36264f703f1c | 152 | break; //Pulls out of this loop |
Eggotape | 0:36264f703f1c | 153 | } |
Eggotape | 0:36264f703f1c | 154 | } |
Eggotape | 0:36264f703f1c | 155 | t2.reset(); |
Eggotape | 0:36264f703f1c | 156 | lcd.cls(); |
Eggotape | 0:36264f703f1c | 157 | lcd.printf("Brewing...\n"); |
Eggotape | 0:36264f703f1c | 158 | displaystate(currentstate); //Reset screen to brewing |
Eggotape | 0:36264f703f1c | 159 | } |
Eggotape | 0:36264f703f1c | 160 | } |
Eggotape | 0:36264f703f1c | 161 | t.reset(); |
Eggotape | 0:36264f703f1c | 162 | myservo = 1; |
Eggotape | 0:36264f703f1c | 163 | lcd.cls(); |
Eggotape | 0:36264f703f1c | 164 | lcd.printf("Done!\n"); |
Eggotape | 0:36264f703f1c | 165 | lcd.printf("Reset?\n"); |
Eggotape | 0:36264f703f1c | 166 | t.start(); |
Eggotape | 0:36264f703f1c | 167 | while ((!setbutton.rising()) && (!gobutton.rising())) { //If not reset in 90 seconds, alert |
Eggotape | 0:36264f703f1c | 168 | if (t > 90) { |
Eggotape | 0:36264f703f1c | 169 | alert(); |
Eggotape | 0:36264f703f1c | 170 | } |
Eggotape | 0:36264f703f1c | 171 | } |
Eggotape | 0:36264f703f1c | 172 | alertoff(); |
Eggotape | 0:36264f703f1c | 173 | lcd.cls(); |
Eggotape | 0:36264f703f1c | 174 | lcd.printf("Start?\n"); //Reset |
Eggotape | 0:36264f703f1c | 175 | wait(1); |
Eggotape | 0:36264f703f1c | 176 | return 0; |
Eggotape | 0:36264f703f1c | 177 | } |
Eggotape | 0:36264f703f1c | 178 | |
Eggotape | 0:36264f703f1c | 179 | int dipping() { //Dipping routine |
Eggotape | 0:36264f703f1c | 180 | myservo = 0.5; |
Eggotape | 0:36264f703f1c | 181 | wait(1); |
Eggotape | 0:36264f703f1c | 182 | myservo = 0; |
Eggotape | 0:36264f703f1c | 183 | return 0; |
Eggotape | 0:36264f703f1c | 184 | } |
Eggotape | 0:36264f703f1c | 185 | |
Eggotape | 0:36264f703f1c | 186 | int alert() { //Alert using on-board LEDs |
Eggotape | 0:36264f703f1c | 187 | int y; |
Eggotape | 0:36264f703f1c | 188 | for ( y = 0; y != 1; y++ ) { |
Eggotape | 0:36264f703f1c | 189 | leda = 1; |
Eggotape | 0:36264f703f1c | 190 | ledb = 0; |
Eggotape | 0:36264f703f1c | 191 | ledc = 1; |
Eggotape | 0:36264f703f1c | 192 | ledd = 0; |
Eggotape | 0:36264f703f1c | 193 | wait(0.2); |
Eggotape | 0:36264f703f1c | 194 | leda = 0; |
Eggotape | 0:36264f703f1c | 195 | ledb = 1; |
Eggotape | 0:36264f703f1c | 196 | ledc = 0; |
Eggotape | 0:36264f703f1c | 197 | ledd = 1; |
Eggotape | 0:36264f703f1c | 198 | wait(0.2); |
Eggotape | 0:36264f703f1c | 199 | } |
Eggotape | 0:36264f703f1c | 200 | return 0; |
Eggotape | 0:36264f703f1c | 201 | } |
Eggotape | 0:36264f703f1c | 202 | |
Eggotape | 0:36264f703f1c | 203 | int alertoff() { //Resets on-board LEDs to 0 |
Eggotape | 0:36264f703f1c | 204 | leda = 0; |
Eggotape | 0:36264f703f1c | 205 | ledb = 0; |
Eggotape | 0:36264f703f1c | 206 | ledc = 0; |
Eggotape | 0:36264f703f1c | 207 | ledd = 0; |
Eggotape | 0:36264f703f1c | 208 | return 0; |
Eggotape | 0:36264f703f1c | 209 | } |