An updated and more polished version of the earlier TeaBasic. This is now finished and works like a treat.
Revision 0:36264f703f1c, committed 2011-06-29
- Comitter:
- Eggotape
- Date:
- Wed Jun 29 13:20:33 2011 +0000
- Commit message:
- Haven\t tried it with water yet, so may want to consider slowing the Servo down to avoid splashing.
Changed in this revision
diff -r 000000000000 -r 36264f703f1c DebouncedIn.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DebouncedIn.cpp Wed Jun 29 13:20:33 2011 +0000 @@ -0,0 +1,93 @@ +#include "DebouncedIn.h" +#include "mbed.h" + +/* + * Constructor + */ +DebouncedIn::DebouncedIn(PinName in) + : _in(in) { + + // reset all the flags and counters + _samples = 0; + _output = 0; + _output_last = 0; + _rising_flag = 0; + _falling_flag = 0; + _state_counter = 0; + + // Attach ticker + _ticker.attach(this, &DebouncedIn::_sample, 0.005); +} + +void DebouncedIn::_sample() { + + // take a sample + _samples = _samples >> 1; // shift left + + if (_in) { + _samples |= 0x80; + } + + // examine the sample window, look for steady state + if (_samples == 0x00) { + _output = 0; + } + else if (_samples == 0xFF) { + _output = 1; + } + + + // Rising edge detection + if ((_output == 1) && (_output_last == 0)) { + _rising_flag++; + _state_counter = 0; + } + + // Falling edge detection + else if ((_output == 0) && (_output_last == 1)) { + _falling_flag++; + _state_counter = 0; + } + + // steady state + else { + _state_counter++; + } + + // update the output + _output_last = _output; + +} + + + +// return number of rising edges +int DebouncedIn::rising(void) { + int return_value = _rising_flag; + _rising_flag = 0; + return(return_value); +} + +// return number of falling edges +int DebouncedIn::falling(void) { + int return_value = _falling_flag; + _falling_flag = 0; + return(return_value); +} + +// return number of ticsk we've bene steady for +int DebouncedIn::steady(void) { +return(_state_counter); +} + +// return the debounced status +int DebouncedIn::read(void) { + return(_output); +} + +// shorthand for read() +DebouncedIn::operator int() { + return read(); +} + +
diff -r 000000000000 -r 36264f703f1c DebouncedIn.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DebouncedIn.h Wed Jun 29 13:20:33 2011 +0000 @@ -0,0 +1,31 @@ +#include "mbed.h" + + class DebouncedIn { + public: + DebouncedIn(PinName in); + + int read (void); + operator int(); + + int rising(void); + int falling(void); + int steady(void); + + private : + // objects + DigitalIn _in; + Ticker _ticker; + + // function to take a sample, and update flags + void _sample(void); + + // counters and flags + int _samples; + int _output; + int _output_last; + int _rising_flag; + int _falling_flag; + int _state_counter; + + }; + \ No newline at end of file
diff -r 000000000000 -r 36264f703f1c Servo.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Servo.lib Wed Jun 29 13:20:33 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/simon/code/Servo/#36b69a7ced07
diff -r 000000000000 -r 36264f703f1c TextLCD.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TextLCD.lib Wed Jun 29 13:20:33 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/simon/code/TextLCD/#44f34c09bd37
diff -r 000000000000 -r 36264f703f1c main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Jun 29 13:20:33 2011 +0000 @@ -0,0 +1,209 @@ +#include "mbed.h" +#include "Servo.h" +#include "TextLCD.h" +#include "DebouncedIn.h" + +TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d4-d7 +DebouncedIn setbutton(p5); +DebouncedIn gobutton(p6); +Servo myservo(p21); +DigitalOut leda(LED1); +DigitalOut ledb(LED2); +DigitalOut ledc(LED3); +DigitalOut ledd(LED4); +Timer t; +Timer t2; + +int set(); +int dip(); +int set (int currentstate); +int displaystate(int currentstate); +int maketea(int currentstate); +int dipping(); +int alert(); +int alertoff(); + +int main() { + int state; + state = 1; + myservo = 1; + lcd.cls(); + lcd.printf("Start?\n"); + while(1) { + if (setbutton.rising()) { + state = set(state); + } + else if (gobutton.rising()) { + maketea(state); + } + } +} + +int set(int currentstate) { //For setting which mode to use + t.start(); //Inactivity timer + lcd.cls(); + displaystate(currentstate); + while (gobutton != 1 and t < 10) { + if (setbutton.rising()) { + currentstate = currentstate + 1; //Advances state + if (currentstate > 9) { //Loops back to start + currentstate = 1; + } + lcd.cls(); + displaystate(currentstate); //Update current state + wait(0.25); //Debounce + t.reset(); + t.start(); //Reset inactivity timer + } + } + lcd.cls(); + lcd.printf("Start?\n"); //Reset screen + return currentstate; +} + +int displaystate(int currentstate) { //Displays current setting + if (currentstate == 1) { + lcd.printf("1 min - dip\n"); + } + else if (currentstate == 2) { + lcd.printf("2 mins - dip\n"); + } + else if (currentstate == 3) { + lcd.printf("3 mins - dip\n"); + } + else if (currentstate == 4) { + lcd.printf("1 min - no dip\n"); + } + else if (currentstate == 5) { + lcd.printf("2 mins - no dip\n"); + } + else if (currentstate == 6) { + lcd.printf("3 mins - no dip\n"); + } + else if (currentstate == 7) { + lcd.printf("4 mins - no dip\n"); + } + else if (currentstate == 8) { + lcd.printf("5 mins - no dip\n"); + } + else if (currentstate == 9) { + lcd.printf("QTEST\n"); + } + return 0; +} + +int maketea(int currentstate) { //Main teamaking routine + lcd.cls(); + lcd.printf("Brewing...\n"); + displaystate(currentstate); //Display status + int time; + int dip; + if (currentstate == 1) { //Initialise time and dipping status + time = 60; + dip = 1; + } + else if (currentstate == 2) { + time = 120; + dip = 1; + } + else if (currentstate == 3) { + time = 180; + dip = 1; + } + else if (currentstate == 4) { + time = 60; + dip = 0; + } + else if (currentstate == 5) { + time = 120; + dip = 0; + } + else if (currentstate == 6) { + time = 180; + dip = 0; + } + else if (currentstate == 7) { + time = 240; + dip = 0; + } + else if (currentstate == 8) { + time = 300; + dip = 0; + } + else if (currentstate == 9) { + time = 10; + dip = 0; + } + myservo = 0; //Dunk teabag + t.start(); //Start tea timer + while (t < time) { //While there's still time left + if (dip == 1) { //If dipping, dip teabag + wait(1); + dipping(); + } + if (gobutton.rising()) { //If cancelling + lcd.cls(); + lcd.printf("Press again to\n"); + lcd.printf("stop brewing.\n"); + t2.start(); //Start (secondary) inactivity timer + while (t2 <= 5) { + if (gobutton.rising()) { + time = 0; //Resets time to run to 0, thus pulling out of main while loop + break; //Pulls out of this loop + } + } + t2.reset(); + lcd.cls(); + lcd.printf("Brewing...\n"); + displaystate(currentstate); //Reset screen to brewing + } + } + t.reset(); + myservo = 1; + lcd.cls(); + lcd.printf("Done!\n"); + lcd.printf("Reset?\n"); + t.start(); + while ((!setbutton.rising()) && (!gobutton.rising())) { //If not reset in 90 seconds, alert + if (t > 90) { + alert(); + } + } + alertoff(); + lcd.cls(); + lcd.printf("Start?\n"); //Reset + wait(1); + return 0; +} + +int dipping() { //Dipping routine + myservo = 0.5; + wait(1); + myservo = 0; + return 0; +} + +int alert() { //Alert using on-board LEDs + int y; + for ( y = 0; y != 1; y++ ) { + leda = 1; + ledb = 0; + ledc = 1; + ledd = 0; + wait(0.2); + leda = 0; + ledb = 1; + ledc = 0; + ledd = 1; + wait(0.2); + } + return 0; +} + +int alertoff() { //Resets on-board LEDs to 0 + leda = 0; + ledb = 0; + ledc = 0; + ledd = 0; + return 0; +} \ No newline at end of file
diff -r 000000000000 -r 36264f703f1c mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Jun 29 13:20:33 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912