Pages: [1]   Go Down
Author Topic: Hold button to stop countdown. HW or SW? [Solved]  (Read 621 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: March 13, 2012, 01:25:29 am by thetaco » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8975
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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().
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 639
Posts: 34725
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

London
Offline Offline
Full Member
***
Karma: 0
Posts: 146
Ugi was not declared in this scope
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hint:

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

http://arduino.cc/en/Tutorial/BlinkWithoutDelay
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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){
    delay(1000);
    allOff();
    while ( true ){
      digitalWrite(G,!digitalRead(G));
      delay(200);
    }

  }
}
Logged

Pages: [1]   Go Up
Jump to: