Newb push-button question
willdabeast
Posts: 6
Hi all. I'm trying to get a push button to cycle thru 4 choices. I will make the sub-routines later, but for now I'm just trying to get the button to work. I've tried using the button command, but it was making my head hurt.
Here's the code I have:
i VAR Byte
btn PIN 2
Main:
DO
INPUT btn
IF btn = 1 THEN i=i+1
SELECT i
CASE i=1
DEBUG "one"
CASE i=2
DEBUG "two"
CASE i=3
DEBUG "three"
CASE i=4
DEBUG "four"
ENDSELECT
IF btn = 0 THEN i=i
IF i=5 THEN i=1
LOOP
I have it inside a DO/LOOP because I need it to run for a long time.
I know my physical assembly of the cct is working, so something ain't right with the code.
Thanks,
-Will
Here's the code I have:
i VAR Byte
btn PIN 2
Main:
DO
INPUT btn
IF btn = 1 THEN i=i+1
SELECT i
CASE i=1
DEBUG "one"
CASE i=2
DEBUG "two"
CASE i=3
DEBUG "three"
CASE i=4
DEBUG "four"
ENDSELECT
IF btn = 0 THEN i=i
IF i=5 THEN i=1
LOOP
I have it inside a DO/LOOP because I need it to run for a long time.
I know my physical assembly of the cct is working, so something ain't right with the code.
Thanks,
-Will
Comments
-Will
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
the same as "if i=5 then i=1"
They both check to see if i = 5.
If it does, it sets i to 1
If it doesn't it lets i stay the same.
-Will
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
Your code is assigning values to variable i instead of evaluating it.
It should reed
SELECT i
· CASE 1
···· DEBUG "One"
.....
IF btn = 1 THEN i=i+1
SELECT i
CASE i=1
DEBUG "one"
CASE i=2
DEBUG "two"
CASE i=3
DEBUG "three"
CASE i=4
DEBUG "four"
ENDSELECT
IF btn = 0 THEN i=i
IF i=5 THEN i=1
LOOP
That does make since. I overlooked the little fact it may skip over 5.
I think I'll add a latch to debounce the button.
Thanks.
-Will
Any ideas?
-Will
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
Anyway. Thank you both for the input. The button works although it repeats endlessly. I think I know where the problem is.
Thanks again.
-Will
[noparse][[/noparse]single shot controlled by flg variable]
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Smile ... It increases your face value!