Using 2 buttons in combinations

Chris Kelly

Well-known member
Hi guys, hope you're all well

If you have two buttons A and B connected to separate input pins, is there a logical way of achieving:

While A is pressed and held (i.e pin A is high) then subsequent presses of B can act as one-shot inputs.
While B is pressed and held then subsequent presses of A can act as one-shot inputs.

I can't think of a way of enabling both alternatives without it conflicting for the split second when each are both high

Cheers
Chris
 

oracacle

Senior Member
I would think about using do loops with if statements inside, and the inputs have a variable referenced to them

Code:
main:
 do while pinc.1 = 1
if pinc.2 = 1 then
b0 = 1
else
b0 = 0
end if
loop

do while pinc.2 = 1
if pinc.1 = 1 then
b1 = 1
else
b1 = 0
end if
loop
'now each input is refrences to via a variable
 'check each variable to fugure out what was pressed
 

AllyCat

Senior Member
Hi,

It depends what you mean by "one-shot" input, but I think the above code will only "do" something after the "master" (first) button has been released (with b1 probably zero if the second button was released first). So you might want to INCrement b1 each time the second button is pressed (and released).

However, the basic principle is correct; you need two separate "polling" loops and enter one when the corresponding master button is pressed. What you do within that loop (or exit with a flag set) when the second button is pressed, will depend on the overall requirements of the program.

Cheers, Alan.
 

oracacle

Senior Member
its an old piece of code, from memory as I couldn't find the original.
That's is true, had a deeper dive back into this, I don't I don't remember it being like this, thought it was small. Alas this work in simulation of a 08M2+, so should work on anything providing you update the pins and variable to reflect you own project.

Code:
main:
 if pinc.1 = 1 then
  pause 100   'debounce
  do while pinc.1 = 1
   if pinc.2 = 1 then
    pause 100
    b0 = b0 + 1
    'do stuff here
   end if
  loop
 'now once you relase the first button check for variable increase
 if b0 = 0 then
  'just the first button was pressed
  'do stuff for first button
  end if
 'now user input has been handled reset the varible
 let b0 = 0
 end if
 
 if pinc.2 = 1 then
  pause 100   'debounce
  do while pinc.2 = 1
   if pinc.1 = 1 then
    pause 100
    b1 = b1 + 1
    'do stuff here
   end if
  loop
 'now once you relase the first button check for variable increase
 if b1 = 0 then
  'just the first button was pressed
  'do stuff for first button
  end if
 'now user input has been handled reset the varible
 let b1 = 0
 end if
 
 goto main
 

Buzby

Senior Member
Hi Chris,

My take on this would be to make the one-shots every time, but only use each one-shot if the 'other' button is pressed.

Code:
make1shots

if ButtonApressed then
      use1shotB
endif

if ButtonBpressed then
      use1shotA
endif
Cheers,

Buzby
 
Top