Shop OBEX P1 Docs P2 Docs Learn Events
Need Help with Countdown Timer — Parallax Forums

Need Help with Countdown Timer

shfehershfeher Posts: 2
edited 2006-07-21 19:37 in BASIC Stamp
I am new to this forum and new to programming.· I have a basic stamp interfaced to a Reynolds Electronics 4 digit LED display module (looks like a digital clock).· My goal is to have have a three position toggle switch (up off down) toggle the seconds up or down, one digit per toggle, to set the countdown start point, and then press another button to begin the countdown.· No...I'm not building a timer for a bomb!· It is for a simulated launch sequence for a spaceship toy that I am building for my son.
·
I have written the code to count up and the code to count down using a "finite state machine" loop.· And it works (will wonders ever cease?) But once I get into one of the loops, I can't exit.· I want to be able to press the switch up or down and have the digits respond accordingly.· Here is my code so far:

I have tried numerous "IF·THEN" exit statements to no avail.· Any help would be appreciated.

Thanks!

'{$STAMP BS2}
'{$PBASIC 2.5}


'Pin assignments
'

EN····· PIN 0······ ' Connect to SLED4C enable pin #5
CLK···· PIN 1······ ' Connect to SLED4C clock pin #4
DOUT··· PIN 2······ ' Connect to SLED4C data pin #3
'Variables
'
X······ VAR Word··· ' GP var
Y······ VAR Byte··· ' GP var
··················· ' Bank #1 controls the colon ":"
D2····· VAR Byte··· ' SLED4C bank #2 digit (right)
D3····· VAR Byte··· ' SLED4C bank #3
D4····· VAR Byte··· ' SLED4C bank #4
D5····· VAR Byte··· ' SLED4C bank #5 digit (left)
CFG···· VAR Byte··· ' Holds display configuration value
'Initialization
'
DIRL = 255
CFGN··· CON %11000001
'Main program
'
Main:
··· GOSUB Clock······ ' Set Countdown Timer





Clock:· 'Countdown Timer
··· CFG = %11000011·· ' 5,4,3,2 HEX decode, 1 special decode
··· GOSUB Config····· ' Configure display
··· Y = 8············ ' Turn Colon ON (8 = ON, 0 = OFF)
··· ' Now write data to the display
··· D5=0 : D4=0 : D3=0 : D2=0 ' Set clock time to 01:59
··· EN=0
··· SHIFTOUT DOUT, CLK, MSBFIRST, [noparse][[/noparse]%0000\4, D5\4, D4\4, D3\4, D2\4, Y\4]
··· EN = 1

'STATE MACHINE CODE

x0 VAR Nib ' prior state of 1 input pin
x1 VAR Nib ' present state of 1 input pin
xx VAR Nib ' detect change in state of 4 input pins

SetTimer:
IF IN5 = 0 THEN
GOTO SecondsUp
ENDIF
IF IN4 = 0 THEN
GOTO SecondsDown
ENDIF
'scan:

SecondsUp:
Btn· PIN 5
··· btnWrk· VAR· Byte
··· BUTTON Btn, 0, 0, 0, btnWrk, 0, No_Press
DO
FOR X = 0 TO 10·· ' Display clock time from 16:50 to 17:00
····· EN = 0········· ' Enable data input
····· IF D2 > 9 THEN D2=0 : D3=D3+1 ' Increment each higher digit D3 whtn D2 is greater than 9
····· IF D3 > 5 THEN D3=0 : ' do not display digits greater than 5 in D3
····· IF (D3 = 6) AND (D4 = 0) THEN ' Roll-over from :59 to :00
······ D5 = 0 : D4 = 0
····· ENDIF
····· SHIFTOUT DOUT, CLK, MSBFIRST, [noparse][[/noparse]%0000\4, D5\4, D4\4, D3\4, D2\4, Y\4]
····· EN = 1······················· ' Transfer data into display registers
NEXT
······· x1 = IN5 ' get state of all 4 input pushbuttons or sensors
··· xx = x1^x0 & x1 ' detect changes, bit =1 when button is released
··· x0 = x1 ' update old state of all 3 buttons
··· D2 = D2 + xx.BIT0 - xx.BIT1 ' update counts,
··· EN = 0
··· SHIFTOUT DOUT, CLK, MSBFIRST, [noparse][[/noparse]%0000\4, D5\4, D4\4, D3\4, D2\4, Y\4]
··· EN = 1····· ' Transfer data into display registers

··· LOOP

SecondsDown:
Btn2· PIN 4
··· btnWrk2· VAR· Byte
··· BUTTON Btn2, 0, 0, 0, btnWrk2, 0, No_Press
DO
FOR X = 0 TO 10·· ' Display clock time from 16:50 to 17:00
····· EN = 0········· ' Enable data input
····· IF D2 > 9 THEN D2 = 9 : D3=D3-1 ' Decrease D2 by one digit if button is pressed
····· IF D3 > 9 THEN D3 = 5· 'do not display higher than 5 on D3
····· IF D3 = 6 THEN D3=0 : 'D4=D4+1 ' Display 0 in D3
····· IF (D2 > 9) AND (D3 = 0) THEN ' Roll back from :00 to :59
······· D2 = 9 : D3 = 5
····· ENDIF
····· SHIFTOUT DOUT, CLK, MSBFIRST, [noparse][[/noparse]%0000\4, D5\4, D4\4, D3\4, D2\4, Y\4]
····· EN = 1······················· ' Transfer data into display registers
NEXT
··· x1 = IN4 ' get state of pin 4
··· xx = x1^x0 & x1 ' detect changes, bit =1 when button is released
··· x0 = x1 ' update old state of all 3 buttons
··· D2 = D2 - xx.BIT0 - xx.BIT1 ' update counts,
··· EN = 0
··· SHIFTOUT DOUT, CLK, MSBFIRST, [noparse][[/noparse]%0000\4, D5\4, D4\4, D3\4, D2\4, Y\4]
··· EN = 1······················· ' Transfer data into display registers
··· LOOP··················· ' Update frequency or clock ticks = once per second



··· '
Config:
··· EN = 0····························· ' Enable data input
··· SHIFTOUT DOUT, CLK, MSBFIRST, [noparse][[/noparse]CFG] ' Write to display config register
··· EN = 1····························· ' Transfer data into display registers
··· No_Press:
··· RETURN

Comments

  • shfehershfeher Posts: 2
    edited 2006-07-21 19:37
    Well....I figured it out and it's working fine now!· If anyone wants the code, I'll forward it to them.
Sign In or Register to comment.