Shop OBEX P1 Docs P2 Docs Learn Events
Help me please, Need to find out how to count down time and show it on the debu — Parallax Forums

Help me please, Need to find out how to count down time and show it on the debu

lm0824lm0824 Posts: 3
edited 2007-04-18 19:11 in BASIC Stamp
Hi, first off, thanks in advance

I'm making simple game, first i need to know how i can make a timecounter that counts down from 10 to 0 and shows up on the debug screen.

Any help would be greatly appreciated,

Thanks!

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-03-30 03:05
    A lot depends on how you have your game organized in that the Stamp doesn't have a built-in timer. If you have a simple loop waiting for something to happen, you can use a PAUSE statement to fill out most of the wait, perhaps PAUSEing 100 milliseconds on each loop. If the statements that make up the loop are few, each loop will take slightly more than 100 milliseconds and you can use that fact to count down. Perhaps every 10 times through the loop, you can use DEBUG to output the current time (in seconds or tenths of a second). Without some kind of external clock, that would be about the best you can do.
  • curious1curious1 Posts: 104
    edited 2007-03-30 03:19
    Good Idea Mike,
    I have a similar situation. This isn't perfect but close enough for what I need.
    Thanks
  • lm0824lm0824 Posts: 3
    edited 2007-03-30 03:26
    Thanks for the quick reply Mike, I'm pretty new at this and i'm a bit confused by what you wrote. I just can't seem to get it, like i want the time to countdown for the game to start, once it starts, i won't need the timer until the game is either won or over. Can you provide an example of what you're saying.

    Thanks again
  • Mike GreenMike Green Posts: 23,101
    edited 2007-03-30 03:37
    Here's a simple loop that counts down roughly every second, checks pin 0 for a low every 1/10 of a second and starts the game if 10 seconds elapses or the button is pushed.
    counter var nib
    temp     var nib
    
    countDown:
      counter = 10
      DO
        DEBUG DEC counter," seconds",CR      ' Count down the seconds
        counter = counter - 1
        temp = 10
        DO
          IF IN0 = 0 THEN startGame ' Check for a button push.  Start game if pushed
          temp = temp - 1
          PAUSE 100               ' Wait 100 milliseconds
        LOOP UNTIL temp = 0  ' Continue after 10 * 100 milliseconds
      LOOP UNTIL counter = 0 ' Continue after 10 * 10 * 100 milliseconds
    startGame:
      DEBUG "Start the game",CR
    
    
  • lm0824lm0824 Posts: 3
    edited 2007-03-30 04:36
    Thank you so much Mike, it worked like a charm!
  • CogburnCogburn Posts: 62
    edited 2007-04-17 18:42
    Mike,

    I am using· your elegant countdown code to make a remote rocket launcher.· I have added some bells and whistles like a LCD for the control box, audible speaker beeps, led flashing, etc.· The idea is to countdown from 10 to 0 and then using a MOSFET, drive a solenoid that removes the holddowns.· I have the solenoid connected and it is working properly.· The code works fine most of the time but every now and then, when I reset the stamp with the reset button,(I plan to use a power key switch to do this when I get the box built) the countdown progresses a couple of loops then activates the solenoid as if it has not gotten through the loops the first time and had to "finish up".·· Or maybe it is not resetting at the proper place in the program.·· Any ideas?·

    It is just now and then that the LCD will display the 10, 9, 8, 7 and then activate the soleniod prematurely when the stamp is reset.· I need this to be dependable and reliable for safety's sake as students will be present during launches and premature release of a pressurized rocket is not desirable.· I suppose that do not understand the nested loop as well as I should.

    I will try to attach my code to this post.

    I had a power point drawing of the circuit but the attachment manager would not allow that to be attached.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Showing up to school doesn't·mean you are a student any more than crawling up in an oven means that·you are a biscuit.

    Post Edited (Cogburn) : 4/17/2007 6:55:03 PM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2007-04-17 23:47
    Cogburn,
    The code looks good although I would explicitly set pins 0 and 9 to LOW during initialization even though they're set to that by the reset itself.

    I suspect you have a problem in the firing switch and, for some reason, it's prematurely activating the whole thing. I'm sure if you comment out the "IF IN1 = 1" statement, this problem will go away.

    Do you have a pull-down on the line to the button/switch? If not, that's your problem. You need a resistor on that Stamp lead (I/O pin 1) to ground, perhaps 1K to 4.7K. That would require a current of 1-5ma and would tend to dampen out noise pulses. You could also change the inner loop to require that the button be held for about a second like this (with temp2 a nibble variable initialized to zero after countDown[noparse]:)[/noparse]:
          temp = 10
          DO
             IF IN1 = 1 THEN
                temp2 = temp2 + 1
                ' If temp2 wraps around, we have a count of 16
                if temp2 = 0 THEN LaunchRocket
             ELSE
                temp2 = 0
             ENDIF
             temp = temp - 1
             PAUSE 60
          LOOP UNTIL temp = 0
    
    
  • CogburnCogburn Posts: 62
    edited 2007-04-18 19:11
    Thanks Mike- I appreciate the help. I will make the changes.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Showing up to school doesn't·mean you are a student any more than crawling up in an oven means that·you are a biscuit.
Sign In or Register to comment.