BUTTON Command
PRPROG01
Posts: 28
Hi.
I have a button on pin 0 that I need to have the following behavior: When press it must generate a "ON" status (set a variable = 1) and when release an "OFF" status (set the same variable to 0). No repeats when press. I am exploring the following code. So far when I press the button it show the ON status with no repeats. Good. But when release It display the "OFF" message infinitely. Do I have to used the BUTTON command? It was very difficult to get to this stage , the BUTTON command is not that easy to understand (...at least for me)
btnWrk VAR Byte
Main:
BUTTON 0, 1, 255, 0, btnWrk, 0, Off
DEBUG "On", CR
Off:
DEBUG "Off" , CR
GOTO Main
Thanks,
PRPROG
I have a button on pin 0 that I need to have the following behavior: When press it must generate a "ON" status (set a variable = 1) and when release an "OFF" status (set the same variable to 0). No repeats when press. I am exploring the following code. So far when I press the button it show the ON status with no repeats. Good. But when release It display the "OFF" message infinitely. Do I have to used the BUTTON command? It was very difficult to get to this stage , the BUTTON command is not that easy to understand (...at least for me)
btnWrk VAR Byte
Main:
BUTTON 0, 1, 255, 0, btnWrk, 0, Off
DEBUG "On", CR
Off:
DEBUG "Off" , CR
GOTO Main
Thanks,
PRPROG
Comments
Initial:
If <button is pressed> THEN
DEBUG "On",CR
GOTO 2ndState
ENDIF
GOTO Initial
2ndState:
If <button is released> THEN
DEBUG "Off",CR
GOTO Initial
ENDIF
GOTO 2ndState
The IF THEN ENDIF is used for illustration. You can do this with the BUTTON statement as well.
There are several ways to do this, one of which Mike pointed out. It all depends on what else your program needs to do. Does it have to do other tasks at the same time as checking the button?