Help on routine-Work with a button


Closed Thread
Results 1 to 12 of 12

Hybrid View

  1. #1
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240

    Default Help on routine-Work with a button

    Hi again everyone;

    I'm trying to do a code, that work with a button, to do 2 things;

    1º - A simple pulse on the button, go to label one
    2º - If a press and hold the some button for 3 seconds, go to label two.

    I do i search, but i'm a little lost with all the post that the search give back.

    Can somebody help me.

    Thanks

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    and when the code jumps to label 1 if the button is still pushed you could pause for 3 seconds and check again. If not pushed in label 1 then... but if still pushed after 3 second pause then label 2 ??
    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240


    Did you find this post helpful? Yes | No

    Default

    Hi mackrackit, thanks for answering;

    I just won't something like this;
    Code:
    Main:
     ´teste button click--->jump label1
     ´teste button click and hold 3 seconds--->jump label2
    Goto Main
    
    Label1:
     ´some code
    goto Main
    
    
    label2:
     ´some code
    goto Main
    Thanks

  4. #4
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Lightbulb

    Quote Originally Posted by gadelhas View Post
    I just won't something like this;
    Code:
     
    While Button pressed
      Wait a bit
      Increment a counter 
    WEND
     
    Is Counter limit reached ???
      GOSUB label 2
    else
      GOSUB label 1
    ENDIF
     
    goto Main
     
    label1:
     ´some code
     
    label2:
     ´some code
    Alain

    Thanks
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  5. #5
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    or even my nooby 'take' might work?.

    Code:
    Main:
    if sw1=0 then 
    pause 3200             ' wait 3.2 seconds (or whatever length of time you expect the user to press/hold the switch for)
    if sw1 = 0 then       ' recheck to see if sw1 is still held down
    goto label2            ' if it is then go & do some stuff in label2
    else
    goto label 1           ' if it wasn't held down then go & do other stuff -  goto label 1
    endif
    endif
    pause 10
    goto main
    
    label1:
    do stuff!
    pause 20           ' bit of a pause for switch debouncing
    goto main
    
    label2:
    do other stuff!
    pause 20 '           ' bit of a pause for switch debouncing
    goto main
    Of course with the method I've posted above, you have to hang around 3.2 seconds to work out whether the user input wanted label1 or label2 - it would really works betterif there was just say a half second pause to decide/glean if label1 or label 2 was required. (but if you're new to picbasic it means grappling with less commands/concepts!)

    Disclaimer: I know bo-diddley squat really!!
    Last edited by HankMcSpank; - 17th August 2010 at 15:34.

  6. #6
    Join Date
    Aug 2005
    Location
    Michigan, USA
    Posts
    224


    Did you find this post helpful? Yes | No

    Default

    This might be a good candidate for switch state logic. Remember that you can detect the following states when using a switch state latch;

    (1) new = 1, old = 0 --> new press
    (2) new = 0, old = 1 --> new release
    (3) new = 1, old = 1 --> still pressed
    (4) new = 0, old = 0 --> still released

    Start a 3-second timer when you detect the "new press" state. If the timer times out while the switch is held pressed then set a "long" flag. If you detect a "new release" state before the timer times out then set a "short" flag.


    Another C example (sorry Gentlemen) based on a 50-msec loop time (sample/debounce/timer interval) that generates a single 'beep' on a "new press" and a double beep at the 3-second time-out;

    Code:
    ;
    ;  unsigned char swnew = 0;     // fresh switch sample
    ;  unsigned char swold = 0;     // switch state latch
    ;  unsigned char swtmr = 0;     // 3-second switch timer
    ;
    ;  #define newpress     swnew.0 == 1 && swold.0 == 0
    ;  #define newrelease   swnew.0 == 0 && swold.0 == 1
    ;
    ;  while(1)                     //
    ;  { delay_ms(50);              // 50-msec sample/debounce interval
    ;    swnew = ~gpio;             // sample active lo switches
    ;    if(newpress)               // if "new press"
    ;    { swold.0 = 1;             // update switch state latch
    ;      swtmr = 3000/50;         // start 3 second timer
    ;      beep1();                 // send "new press" beep
    ;    }
    ;    if(newrelease)             // if "new release"
    ;    { swold.0 = 0;             // update switch state latch
    ;      if(swtmr)                // if 'short' press
    ;        swtmr = 0;             // turn off switch timer
    ;        sflag = 1;             // set "short" flag
    ;    }
    ;    if(swtmr)                  // if switch timer running
    ;    { swtmr--;                 // decrement it and
    ;      if(swtmr == 0)           // if 3 second timeout
    ;      { beep2();               // send double beep and
    ;        lflag = 1;             // set "long" flag
    ;      }
    ;    }
    ;    if(sflag)                  // if "short" press
    ;    {                          //
    ;    }                          //
    ;    if(lflag)                  // if "long" press
    ;    {                          //
    ;    }                          //
    ;  }                            //
    ;
    Add a little more logic for safety (and outputs) and you've got one of those novelty lighted single switch ignition systems (I don't endorse or recommend anyone trying this as it's just too dangerous, IMO);


    Code:
    ;  unsigned char swnew = 0;     // fresh switch sample
    ;  unsigned char swold = 0;     // switch state latch
    ;  unsigned char swtmr = 0;     // 1-second switch timer
    ;
    ;  #define ignition    gpio.0   // GP0, active hi relay output
    ;  #define starter     gpio.1   // GP1, active hi relay output
    ;  #define running     swold.0  // flag
    ;
    ;  #define newpress    swnew.3 == 1 && swold.3 == 0
    ;  #define newrelease  swnew.3 == 0 && swold.3 == 1
    ;  #define allowstart  ignition == 1 && running == 0
    ;
    ;  while(1)
    ;  { delay_ms(32);              // 32-msec debounce intervals
    ;    swnew = ~gpio;             // sample active lo switch GP3
    ;    if(newpress)               // if "new press"
    ;    { swold.3 = 1;             //   update switch state latch
    ;      swtmr = 1000/32;         //   start 1 second timer
    ;      beep1();                 //   send a single beep
    ;    }
    ;    if(newrelease)             // if "new release"
    ;    { swold.3 = 0;             //   update switch state latch
    ;      starter = 0;             //   turn GP1 "starter" off
    ;      if(swtmr)                //   if "short" press
    ;      { ignition ^= 1;         //     toggle GP0 "ignition"
    ;        swtmr = 0;             //     turn 1 second timer off
    ;        running = 0;           //     clear "running" flag
    ;      }                        //
    ;    }
    ;    if(swtmr)                  // if 1 second timer running
    ;    { swtmr--;                 //   decrement it
    ;      if(swtmr == 0)           //   if timed out
    ;      { beep2();               //     send a double beep
    ;        if(allowstart)         //     if start allowed
    ;        { running = 1;         //       set "running" flag
    ;          starter = 1;         //       turn GP1 "starter" on
    ;        }                      //
    ;      }                        //
    ;    }
    ;  }
    ;
    Attached Images Attached Images   
    Last edited by Mike, K8LH; - 17th August 2010 at 19:30.

Members who have read this thread : 0

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