Pages: [1]   Go Down
Author Topic: A layman finds purpose, and needs some help with this timer.  (Read 580 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So just a day or two after I posted in the Bar Sport forum about what to do with C++, my science teacher wife comes to me with a need for some game buzzers for the National Science Bowl.  A few days and a couple of orders to Adafruit and Sparkfun later, I'm in the home stretch...except for this one thing.  The contest needs an eight minute timer.  No problem (see code below).  But I need to be able to hold down a button and stop the clock, then let the clock resume upon release of the button.  This is printing out to an LCD.  I've gotten as far as read the button, if it's HIGH, then .......    As it is now, the delay ends, the clock resumes even though I am still holding the button down.  I've tried some other means to stop the clock while the button is pressed, but when the clock resumes, it is as if no pause ever took place i.e.  30 seconds left on the clock, I hold the button for ten seconds, release, and there is twenty seconds on the clock.  Any thoughts?? 

[Edit]  Yup, I'm green as they come, a FNG if there ever was one, so let 'er rip folks. 
Code:
   void gameClock(){
     int pauseVal = 0;                       // this stores the state of the pause button
   int seconds = (millis()/1000)-480;  // 480 seconds is eight minutes.  Using negative values here and absolute values to print so clock counts down.
   pauseVal = digitalRead(pauseButton);
    if (pauseVal == HIGH){
      delay(10);
      /*  Some nifty code needs to go here*/
     
    }
     
    lcd.setCursor(12,1);               
    lcd.print(abs(seconds/60));       
                                       
    if (abs(seconds%60) < 10){           
      lcd.setCursor(14,1);           
      lcd.print("0");                 
      lcd.print (abs(seconds%60));       
    }else if (abs(seconds%60) >=10) { 
      lcd.setCursor(14,1);               
      lcd.print (abs(seconds%60));     
      lcd.print ("  ");                 
     }                                 
    }


« Last Edit: October 21, 2011, 03:24:44 pm by bebo » Logged

bird denigration is a crime

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 620
Posts: 33999
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Any thoughts??
Yes, when posting code use the # icon not the quote one next to it.
Post all the code not just a fraction.
Say what the codes actually does and what you want it to do.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 611
Posts: 49097
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Blank comments make you look like a rank amateur. Why don't you remove them to remove that appearance.
Logged

Leeds, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 134
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to keep a track of the seconds, which you can do with the millis() function, as you've already got there are 1000 milliseconds per second. Then perhaps take a note of the millis() value when the button is pressed and again when it is released, then apply corrections to account for the paused time.

Timer not paused, 8 minutes elapses unhindered.
  • Start timer, timeStart = millis()
  • timeEnd = timeStart + 480000 // 480000 = 480 secs = 8 mins in milliseconds
  • When millis() == timeEnd, then trigger the buzzer

Timer is paused, then later unpaused
  • Start timer, timeStart = millis()
  • timeEnd = timeStart + 480000
  • Button pressed, don't let the alarm go off if the button is held down.
  • timeButtonPressed = millis()
  • //button is pressed while question is answered
  • Button released, allow the alarm to go off
  • timeEnd = millis() + (480000 - (timeButtonPressed - timeStart))
  • When millis() == timeEnd, then trigger the buzzer

I haven't tried this, but in my head it works. Be aware there are probably better methods of doing this, but hey whatever give it a go and see if it works.
Logged

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

I figured it out.  Baby's crying...gotta go....thanks for looking...thanks for caring...
Logged

bird denigration is a crime

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 620
Posts: 33999
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I figured it out.
Well done.  smiley

I think you have shed your laymans status.

Quote
Baby's crying...gotta go.
OK, enjoy the baby, they don't stay like that for long.
Logged

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

Thanks Mike...Yeah they grow up quick.  This has been a hell of a first project.  It's just shy of 10K when compiled. Two teams of five players, with buttons and leds for each.  All the ancillary timing and score keeping is handled by the code.  There is the eight minute clock (with pause function), a five second timer, a twenty second timer, scoring in increments of four and ten.  Also the twenty second timer needs a five second warning.  It all gets displayed on a 16x2 LCD.  If I had more time, I'd like to handle the player button reads with direct port manipulation, but for some high school kids practicing for the regionals, I think it will be okay.
Special Thanks to Roy Rabey and his Quiz-o-Tron tutorial.
   And a.d you were on the right track.  I was getting the time with     int seconds = (millis()/1000)-480;
  but I changed it to ((millis()/1000)-480)-pauseDuration;  and got pauseDuration by timing the interval between the press and the release of the button. 
« Last Edit: October 21, 2011, 07:14:34 pm by bebo » Logged

bird denigration is a crime

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8501
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Yeah they grow up quick.  This has been a hell of a first project.
So you probably won't want to have any more then smiley

______
Rob

Logged

Rob Gray aka the GRAYnomad www.robgray.com

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

Well played sir... smiley
  Actually the baby raising is quite a bit easier than learning to write code.  If the baby cries, she's wet, dirty, hungry, sleepy, or needs to poop.  When the code won't behave, it's a whole lot tougher to find out why.
Logged

bird denigration is a crime

Grand Blanc, MI, USA
Online Online
Faraday Member
**
Karma: 95
Posts: 4062
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Pretty decent first project, I'd say! Lots of ground covered. Congrats, and hope you had fun!  smiley
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Pages: [1]   Go Up
Jump to: