Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Is there a simple way to do this push button routine

Status
Not open for further replies.

mastero

Member
Hello all,


My code is

Code:
'Definition of Input ports
Symbol sw = PORTB.5  'input
Symbol led = PORTC.5
Symbol led1 = PORTC.2

led = 0
led = 0
k = False
y = 0
x = 0
'********** Main loop ******************

menu:

Gosub check_switch

Goto menu
End                                               
check_switch:

If sw = 0 Then
		k = True
	
Endif

If sw = 1 Then
	If k = True Then
		If x = 0 Then
			led = 1
				WaitMs 1
			led = 0
			k = False
			y = 0
		Endif
	Endif
Endif
		
If sw = 0 Then
	If k = True Then
		If x = 0 Then
			y = y + 1
			If y = 10 Then
				led1 = 1
				WaitMs 1
				led1 = 0
				x = 1
			Endif
		Endif
	Endif
Endif

If x = 1 Then
	If sw = 1 Then
		k = False
		y = 0
		x = 0
	Endif
Endif

	Return

Code is for a push button on input which is high always.

When i press the button for less than 5 sec it should light LED on PORTC.5
When i press the button for more than 5 sec it should light LED on PORTC.2

The code works perfectly but is there a simpler way to do it ? to save code space

cheers
Mastero
 
Last edited by a moderator:
This may not be correct BASIC syntax but hopefully you'll get the idea it will make sense. Basically, since you're looking for "short" and "long" presses, use a switch state latch bit and simple logic to act only on "new press" and "new release" states. Start a 5 second timer on a "new press" state and toggle your "short" or "long" LEDs according to the timer on a "new release" state.

Good luck. Cheerful regards, Mike

Code:
led0 = PORTC.5              '
led1 = PORTC.2              '

swnew = 0                   ' new switch sample
swold = 0                   ' switch state latch

main:
  WaitMs 50                 ' 50-msec sample/debounce interval
  swnew = ~PORTB            ' sample active lo switches
'
' if new press
'
  if swnew.5 = 1 and swold.5 = 0 then
    swold.5 = 1             ' update switch state latch
    swtmr = 5000/50         ' start 5 second timer
  endif
'
' if new release
'
  if swnew.5 = 0 and swold.5 = 1 then
    swold.5 = 0             ' update switch state latch
    if swtmr > 0 then       ' for "short" press
      led0 = led0 xor 1     '   toggle LED "0" (RC5)
    else                    ' for "long" press
      led1 = led1 xor 1     '   toggle LED "1" (RC2)
    endif                   '
    swtmr = 0               ' reset 5 second timer
  endif                     '
'
'  5 second timer
'
  if swtmr > 0 then         ' if timer running
    swtmr = swtmr - 1       ' decrement timer
  endif                     '
  goto main                 ' loop forever
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top