Go Down

Topic: Hold button to stop countdown. HW or SW? [Solved] (Read 1 time) previous topic - next topic


Mar 11, 2012, 09:43 pm Last Edit: Mar 13, 2012, 07:25 am by thetaco Reason: 1
The project that I have in mind is yet another 7-segment count down timer. The only programming experience that I have is from writing mildly complicated bash scripts, so software my weak point.

I want a single button to set/start the timer (long press to enter setup, short presses to increase time, long press to start countdown). I've been tinkering with each part individually, and I'm confident that I can figure that stuff out. Right now, my long/short press detection relies on delay(), so my next step is to adapt BlinkWithoutDelay to my project.

The part that I'm concerned about is that while the timer is counting down, I want to stop the count by holding down a button for 30 seconds. My first thought was to use my long/short press detection on an interrupt pin, but I have since learned that neither delay() nor millis() work inside an interrupt routine.

So my tentative solution is to tune an RC circuit to the 30 seconds and use it on the base of a transistor. Then, I can just initiate the interrupt on either RISING or FALLING. Going the RC route will work, but increases my part count. Can I do what I want in software, or is the RC circuit best?


Once you have your code changed to use millis() instead of delay() to detect long and short presses it should be trivial to have it detect very-long(30 second)  presses.  No need for interrupts once you get rid of delay().
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp


is to tune an RC circuit to the 30 seconds and use it on the base of a transistor.

No that simply is not on. The leakage current from the capacitors will ensure that is a very inaccurate way of going about things.

There is no need for all this if you just write the software correctly.



The "blink without delay" tutorial might be a good place to look for measuring time without the "delay" function.



Ok, I got it working using timer1 interrupts to update the 7-segment and this bit of logic running in the main loop:

Code: [Select]
void loop (){
  if (digitalRead(buttonPin) == 0){ // keep previousMillis
    previousMillis = millis();      // and currentMillis in
    currentMillis = previousMillis; // sync when button is open
  if (digitalRead(buttonPin) == 1){
    currentMillis = millis(); // measure length of button press
  if(currentMillis - previousMillis > interval) { // long press?
    previousMillis = currentMillis; // Don't need this yet.
    allOff();     // clear LEDS
    TIMSK1=0x00;  // stop timer interrupts

  if (alarm == 1){
    while ( true ){


Go Up