Hi mackrackit, thanks for answering;
I just won't something like this;
ThanksCode: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
Hi mackrackit, thanks for answering;
I just won't something like this;
ThanksCode: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
************************************************** ***********************
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 " !!!
*****************************************
or even my nooby 'take' might work?.
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!)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
Disclaimer: I know bo-diddley squat really!!![]()
Last edited by HankMcSpank; - 17th August 2010 at 15:34.
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;
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; // 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 ; { // ; } // ; } // ;
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 ; } // ; } // ; } ; } ;
Last edited by Mike, K8LH; - 17th August 2010 at 19:30.
Thanks you for all the reaplys, I will analyse all of them...in the mean time, if anyone have more options, please post.!!
Thanks
Hi Again;
I made this code, however is not working like i wont, only sometimes, can somebody tell me why;
ThanksCode:'********************************************************************* ' INCLUDE's e FUSES ' ==================================================================== DEFINE OSC 4 ' CONFIGURAÇÃO DAS INTERRUPÇÕES ' ==================================================================== ' VARIÁVEIS ' ==================================================================== VAR1 var byte VAR2 var byte I var byte var1=0 var2=0 ' REGISTOS PINOUT 1 = IN; 0 = OUT ' ==================================================================== '76543210 TRISA = %00000001 TRISB = %00000000 TRISC = %10000000 TRISD = %00000000 TRISE = %00000000 ADCON1 = 7 ' NOMES PINOUT ' ==================================================================== LED1 VAR PORTC.0 LED2 var PORTD.0 Botao var PORTA.0 ' DEFINIÇÕES ' ==================================================================== DEFINE LCD_DREG PORTB ' LCD Data bits on PORTB DEFINE LCD_DBIT 0 ' PORTB starting address DEFINE LCD_RSREG PORTB ' LCD RS bit on PORTB DEFINE LCD_RSBIT 4 ' LCD RS bit address DEFINE LCD_EREG PORTB ' LCD E bit on PORTB DEFINE LCD_EBIT 5 ' LCD E bit address DEFINE LCD_BITS 4 ' LCD in 4-bit mode DEFINE LCD_LINES 2 ' LCD has 2 rows DEFINE LCD_COMMANDUS 2000 ' Set command delay time in us DEFINE LCD_DATAUS 50 ' Set data delay time in us ' INICIO PROGRAMA ' ==================================================================== low led1 low lED2 LCDOUT $fe, 1 lcdout " Teste Botoes " lcdout $FE,$C0," Por Hugo Oliveira " Main: if botao = 1 then 'Button presse pause 80 'debounce for 80ms If Botao = 0 then BotaoClick 'Button Unpressed -> Only Click else if Botao = 1 then 'Button Still pressed for i = 1 to 6 pause 486 If Botao = 0 then Main 'Test if was unpressed in the mean while next If Botao = 1 then BotaoClickHold 'IF Still pressed after 3s -> Click&Hold endif endif GOTO MAIN ' SUB-ROTINAS ' ==================================================================== BotaoClick: if var1 = 0 then high led1 var1 = 1 pause 500 else low led1 var1 = 0 pause 500 endif Goto Main BotaoClickHold: if var2 = 0 then high led2 var2 = 1 pause 500 else low led2 var2 = 0 pause 500 endif Goto Main END
Hi, Hugo
I do not see those two actions in your program ...Code:While Button pressed Wait a bit Increment a counter WEND ... Is Counter limit reached ?
Alain
PS for Administrators ... There is a bug with high size characters: suppress the blank lines inserted after " wait a bit " and it will appear ...
Last edited by Acetronics2; - 18th August 2010 at 10:17.
************************************************** ***********************
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 " !!!
*****************************************
Bookmarks