Go Down

Topic: A layman finds purpose, and needs some help with this timer. (Read 647 times) previous topic - next topic

bebo

Oct 21, 2011, 09:53 pm Last Edit: Oct 21, 2011, 10:24 pm by bebo Reason: 1
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: [Select]
   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 ("  ");                 
     }                                 
    }



bird denigration is a crime

Grumpy_Mike

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.

PaulS

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

a.d

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.

bebo

I figured it out.  Baby's crying...gotta go....thanks for looking...thanks for caring...
bird denigration is a crime

Grumpy_Mike

Quote
I figured it out.

Well done.  :)

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.

bebo

#6
Oct 22, 2011, 12:59 am Last Edit: Oct 22, 2011, 02:14 am by bebo Reason: 1
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. 
bird denigration is a crime

Graynomad

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 :)

______
Rob

Rob Gray aka the GRAYnomad www.robgray.com

bebo

Well played sir... :)
  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.
bird denigration is a crime

Jack Christensen

Pretty decent first project, I'd say! Lots of ground covered. Congrats, and hope you had fun!  :)
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Go Up