Push switch counter
hmlittle59
Posts: 404
Hello everyone,
I'm trying to count UP/DOWN with momentary push switches on two pins 3 & 4. Pin7 switch is used for moving to the next location. The program will count up and down as I want, but it seems to hang/pause some times. It may or may not keep up with the count during these delays. Timing and efficiency of code is what I suspect. The switches are wired over 12ins. away. Can anyone help with code...I will be moving the switches closer.
Thanks for any help
hmlittle59
' {$STAMP BS2}
' {$PBASIC 2.5}
switchUP PIN 4
switchDown PIN 3
switchNext PIN 7
year VAR Byte
month VAR Byte
Day VAR Byte
Date VAR Byte
Hour VAR Byte
Minute VAR Byte
work VAR Byte
year = 01:month =10:day = 1:date = 10:Hour = 18:Minute = 22
back:
'==================================================
work = year
DO
DEBUG CLS,"Year->", DEC2 work
IF switchUp = 1 THEN ' if high next check
work = work + 1 //100 ' add 1 to work
IF work = 0 THEN work = year ' if rolls to zero make = year
PAUSE 175 ' time to release switch
ENDIF
IF switchDown = 1 THEN
work = work + 99 //100
IF work = 00 THEN work = 99
PAUSE 175
ENDIF
IF switchNext = 0 THEN ' Normally Closed Switch..being used here
PAUSE 700 ' Chance to release switch
GOTO exit_year
ENDIF
LOOP
'==================================================
I'm trying to count UP/DOWN with momentary push switches on two pins 3 & 4. Pin7 switch is used for moving to the next location. The program will count up and down as I want, but it seems to hang/pause some times. It may or may not keep up with the count during these delays. Timing and efficiency of code is what I suspect. The switches are wired over 12ins. away. Can anyone help with code...I will be moving the switches closer.
Thanks for any help
hmlittle59
' {$STAMP BS2}
' {$PBASIC 2.5}
switchUP PIN 4
switchDown PIN 3
switchNext PIN 7
year VAR Byte
month VAR Byte
Day VAR Byte
Date VAR Byte
Hour VAR Byte
Minute VAR Byte
work VAR Byte
year = 01:month =10:day = 1:date = 10:Hour = 18:Minute = 22
back:
'==================================================
work = year
DO
DEBUG CLS,"Year->", DEC2 work
IF switchUp = 1 THEN ' if high next check
work = work + 1 //100 ' add 1 to work
IF work = 0 THEN work = year ' if rolls to zero make = year
PAUSE 175 ' time to release switch
ENDIF
IF switchDown = 1 THEN
work = work + 99 //100
IF work = 00 THEN work = 99
PAUSE 175
ENDIF
IF switchNext = 0 THEN ' Normally Closed Switch..being used here
PAUSE 700 ' Chance to release switch
GOTO exit_year
ENDIF
LOOP
'==================================================
Comments
GOTO exit_year
near the end of your DO:LOOP and I don't see that label anywhere in your program. I would think this would give you a syntax error in the Stamp Editor.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
This is just one of the Sub-routines for changing the values of (Year,Date,Day,Hour,Sec...). All the routines are the same until the return to the Main-Loop. I think, the switch activation is not being detected in real-time(or buffered), so it causes me to miss a press or hold to long and then the value goes up/down by two(2). Some how I've got to make this code more efficient to detect every press as soon it is pressed.
Any help that you can give...thanks
hmlittle59
In your program, the whole thing comes to halt after a button press because:
IF work = 0 THEN work = year ' if rolls to zero make = year
PAUSE 175 ' time to release switch
ENDIF
Would PAUSE the Stamp and nothing else is happening during the pause -- if you release the button the stamp won't know, if you keep it down it won't know. The way you blast through buttons is usually more like this:
Start:
buttonval0 = 0
buttonval1 = 0
IF button0 = PRESSED THEN
buttonval0 = 1
ENDIF
IF button1 = PRESSED THEN
buttonval1 = 1
ENDIF
'now the above is really fast, so decide what to do:
IF buttonval0 = 1 AND lastbuttonval0 = 0 THEN 'button0 is now 1, last time was 0, must have been released
'do something
ELSEIF buttonval1 = 0 AND lastbuttonval1 = 0 THEN 'button1 is now 0, last time 1, must have been pressed
'etc
ELSEIF buttonval1 = lastbuttonval1 AND buttonval1 = 0 'still pressed from lasttime
'do something
ENDIF
'etc
GOTO Start
But I would definitely check out the button command -- and post your whole program.
lastbuttonval0 = buttonval0
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When the going gets weird, the weird turn pro. -- HST
I will go back and study the button command
Thanks alot for your help
hmlittle59
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Rick