PDA

View Full Version : Help on routine-Work with a button



gadelhas
- 16th August 2010, 21:53
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

mackrackit
- 16th August 2010, 22:10
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 ??

gadelhas
- 17th August 2010, 13:36
Hi mackrackit, thanks for answering;

I just won't something like this;


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

Acetronics2
- 17th August 2010, 14:49
I just won't something like this;



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

HankMcSpank
- 17th August 2010, 15:17
or even my nooby 'take' might work?.



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!! :D

Mike, K8LH
- 17th August 2010, 18:56
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.
http://www.picbasic.co.uk/forum/attachment.php?attachmentid=4714&d=1282066905

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;


;
; 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);
http://www.picbasic.co.uk/forum/attachment.php?attachmentid=4715&d=1282067483


; 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
; } //
; } //
; }
; }
;

gadelhas
- 17th August 2010, 19:21
Thanks you for all the reaplys, I will analyse all of them...in the mean time, if anyone have more options, please post.!!

Thanks

gadelhas
- 18th August 2010, 00:56
Hi Again;

I made this code, however is not working like i wont, only sometimes, can somebody tell me why;


'************************************************* ********************
' 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

Thanks

Acetronics2
- 18th August 2010, 10:14
Hi Again;

I made this code, however is not working like i wont, only sometimes, can somebody tell me why;

Thanks


Hi, Hugo



While Button pressed
Wait a bit


Increment a counter
WEND

...


Is Counter limit reached ?


I do not see those two actions in your program ...

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

gadelhas
- 18th August 2010, 14:02
Hi, thanks Acetronics;

Hi made thsi way, and its working, however later, i will make more tests.


' INCLUDE's e FUSES
' ================================================== ==================
DEFINE OSC 4

' CONFIGURAÇÃO DAS INTERRUPÇÕES
' ================================================== ==================

' VARIÁVEIS
' ================================================== ==================
VAR1 var byte
VAR2 var byte
I var byte
counter var byte


' 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:
while botao = 1
pause 100
counter = counter + 1
Wend

if counter >= 30 then BotaoClickHold
if counter>0 and counter<=5 then BotaoClick
counter=0
GOTO MAIN
' SUB-ROTINAS
' ================================================== ==================
BotaoClick:
counter = 0
if var1 = 0 then
high led1
var1 = 1
pause 500
else
low led1
var1 = 0
pause 500
endif
Goto Main

BotaoClickHold:
counter = 0
if var2 = 0 then
high led2
var2 = 1
pause 500
else
low led2
var2 = 0
pause 500
endif
Goto Main
END

Thanks Again.

gadelhas
- 18th August 2010, 17:26
Hi;

It's Working just like the way i want, just made this change in the Main loop, from the code above;



' INCLUDE's e FUSES
' ================================================== ==================
DEFINE OSC 4

' CONFIGURAÇÃO DAS INTERRUPÇÕES
' ================================================== ==================

' VARIÁVEIS
' ================================================== ==================
VAR1 var byte
VAR2 var byte
I var byte
counter var byte


' 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:
while botao = 1
pause 100
counter = counter + 1
if counter = 30 then BotaoClickHold
Wend
if counter>0 and counter<=5 then BotaoClick
counter=0
GOTO MAIN
' SUB-ROTINAS
' ================================================== ==================
BotaoClick:
counter = 0
if var1 = 0 then
high led1
var1 = 1
pause 500
else
low led1
var1 = 0
pause 500
endif
Goto Main

BotaoClickHold:
counter = 0
if var2 = 0 then
high led2
var2 = 1
pause 500
else
low led2
var2 = 0
pause 500
endif
Goto Main
END

Thanks Again.

Acetronics2
- 18th August 2010, 18:08
Hi;

It's Working just like the way i want, just made this change in the Main loop, from the code above;


Thanks Again.

:):):)

Glad you're happy !!!

Alain