Cancelling a routine & debouncing

24 Jun 2011

Hi all,

I'm a bit of a beginner at C++ and mbed, and so I had a couple of questions about when things happen.

I'm trying to improve this so that it's easier to cancel when you're dipping your teabag (if you're wondering what I mean by that, look at http://mbed.org/users/Eggotape/notebook/the-wonderful-tea-o-matic/);

myservo = 0; //Dunk teabag
    t.start(); //Start tea timer
    while (t < time) { //While there's still time left
        wait(0.1); //Debounce
        if (dip == 1) { //If dipping, dip teabag
            wait(0.9); //0.1 + 0.9 = 1 for regular dip intervals
            dipping();
        }
        if (gobutton == 1) { //If cancelling
            wait(0.1); //Debounce
            lcd.cls();
            lcd.printf("Press again to\n");
            lcd.printf("stop brewing.\n"); //You sure you want to cancel?
            t2.start(); //Start (secondary) inactivity timer
            while (t2 <= 5) {
                if (gobutton == 1) {
                    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 != 1) && (gobutton != 1)) { //If not reset in 90 seconds, alert
        if (t > 90) {
            alert();
        }
    }
    alertoff();
    wait(0.25); //Debounce

It's really hard to get into the cancelling loop when you are dipping your teabag, because you can't access it when the teabag dipping routine is occurring. (It's fine when you're not dipping, by the way). You could hold the button, but that ends up skipping through several stages and could even end up with you going into another teamaking cycle.

Any suggestions on how to improve accessibility to the cancel routine when dipping would be much appreciated. Is there any way to run the dipping parallel to the cancel routine? As is, when you do get into the cancel routine, you stop dipping. That's sort of acceptable but I'd prefer if the dipping could continue.

I'd also appreciate suggestions of better methods of debouncing, because the wait statements help but aren't foolproof.

24 Jun 2011

For debouncing, have a look at DebouncedIn.

24 Jun 2011

Ah, cheers. I'd forgotten about that. (Also: pictures are now up on the Tea-o-Matic page :D)

24 Jun 2011

Also see PinDetect which makes a normal DigitalIn appear like an InterruptIn but with deboucing. It also has key press, release and period hold callbacks.

24 Jun 2011

Oooh, brilliant. That sounds like just the ticket. Cheers!

27 Jun 2011

Just to let you know, I went with DebouncedIn in the end. The buttons are now working reliably, and it's easier to cancel dipping mode! Hurrah!

If you want to view the finished (basic) version of the project, please check out http://mbed.org/users/Eggotape/notebook/the-wonderful-tea-o-matic/. I'd really appreciate any comments you may have.