Strange problem with button command and loop


Closed Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2013
    Posts
    1,078

    Default Strange problem with button command and loop

    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:

    Code:
    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?

  2. #2
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: Strange problem with button command and loop

    What is interesting, in about a half minute, the led starts to flash again !

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default Re: Strange problem with button command and loop

    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.
    DT

  4. #4
    Join Date
    Jan 2012
    Location
    Grid EN19MV
    Posts
    159


    Did you find this post helpful? Yes | No

    Default Re: Strange problem with button command and loop

    Quote Originally Posted by CuriousOne View Post
    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:

    Code:
    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
    "I have noticed that even those who assert that everything is predestined and that
    we can change nothing about it still look both ways before they cross the street"


    -Stephen Hawking

  5. #5
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: Strange problem with button command and loop

    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:

    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?

  6. #6
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: Strange problem with button command and loop

    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.

Similar Threads

  1. Button command question
    By aherrera in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 31st August 2009, 08:44
  2. Button command
    By MrRoboto in forum mel PIC BASIC
    Replies: 1
    Last Post: - 29th January 2009, 02:23
  3. Button command explanations
    By F1CHF in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 26th October 2006, 15:34
  4. How to use BUTTON command right?
    By Sharky in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 14th December 2005, 06:54
  5. Serin2 and Button command
    By ero in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 1st March 2005, 08:46

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts