PDA

View Full Version : Strange problem with button command and loop



CuriousOne
- 12th April 2013, 19:06
Hello.

I'm continiuing to evaluate PBP pro, so I built a simple circuit with 16F682A, two leds and a button (tied to Vdd via 10k resistor and when pushed, connects the pin to gnd) leds are tied to rb0 and rb1 via 1k resistors.

The code is below:



LED VAR PORTB.0
LED2 VAR PORTB.1
brep var byte

mainloop:
BUTTON PORTB.3,0,255,10,brep,1,secondled
High LED
Pause 250
Low LED
Pause 250

Goto mainloop

secondled:
high LED2
return
End


The code works almost as it should do, but with a nice twist. When launched, if I press the button for the short period, the 2nd lend comes on and 1st one continues to blink, as it should do. However, if I press the button for a bit longer time, or press it 2nd time, the 1st led stops flashing!

What is the reason?

Also, I don't like that button should be handled in the loop with all my delays. Say I have somewhere in the code statement: pause 2000. This means, that even if I press button, I have to wait 2 seconds for software to react for it? is there a workaround for that? For example, on ZX Spectrum, if any key was pressed, the pause command, regardless of it's parameters, was terminated and next step executed immediatelly. Is same feature available here?

CuriousOne
- 12th April 2013, 19:14
What is interesting, in about a half minute, the led starts to flash again !

Darrel Taylor
- 12th April 2013, 22:01
The jump from a BUTTON command is essentially a GOTO, not a GOSUB.
So from the "secondled" routine you should GOTO mainloop, not return.


And, assembly language interrupts can be used to immediately respond to events, even if a PAUSE 2000 is executing.

andywpg
- 14th April 2013, 02:01
Also, I don't like that button should be handled in the loop with all my delays. Say I have somewhere in the code statement: pause 2000. This means, that even if I press button, I have to wait 2 seconds for software to react for it? is there a workaround for that? For example, on ZX Spectrum, if any key was pressed, the pause command, regardless of it's parameters, was terminated and next step executed immediatelly. Is same feature available here?

I don't particularly like the BUTTON command, I prefer to check it and debounce it myself.

As to the pause, instead of PAUSE 2000, do this instead:



FOR COUNTER = 1 TO 2000
PAUSE 1
***check for button press here - if pressed EXIT***
NEXT COUNTER


That way, you are checking every millisecond for a button press. Of course you can adjust this to check as often or seldom as you like.

Hope this helps,

Andy

CuriousOne
- 14th April 2013, 07:06
Thanks a lot, I've fixed it now. But I think there's a flaw in compiler. Any normal compiler, when see "RETURN" command without GOSUB, should pop up an error "RETURN without GOSUB"

I have another question. From PBP examples, I've learned how to interact with multiple buttons. But how to set "action" running only when button is being pressed and hold?

This is an example code:



chk1:
PAUSE 25 ' Pause once for each loop
' Check Button 1 (Skip to 2 if Not Pressed)
BUTTON FOCLBN, 0, 40, 5, B1, 0, chk2
TOGGLE LED1 ' Toggle LED if pressed
chk2:
' Check Button 2 (Skip to 1 if Not Pressed)
BUTTON FOCRBN, 0, 40, 5, B2, 0, chk1
TOGGLE LED2 ' Toggle LED if pressed
GOTO CHK1

END


here the TOGGLE command toggles the status of the led. So instead of toggle, I can use HIGH LED1, HIGH LED2? but what will make LEDs low, when buttons aren't pressed?

CuriousOne
- 14th April 2013, 07:10
Sure, I can insert LED1 low, LED2 low in the code, but this will cause blinking led when pressed a button, not the steady on.