Wait for 1 of 3 inputs to go high

smeagol

Member
Hi all,

I'm sure it's due to old age but I'm having serious brian fade.

I have 3 inputs and want the program to wait until one of them goes high.

Code:
[color=Black]WaitForButton:[/color]
[color=Blue]do
loop while [/color][color=Purple]Buttonset [/color][color=DarkCyan]<> [/color][color=Navy]1 [/color][color=DarkCyan]or [/color][color=Purple]buttonup [/color][color=DarkCyan]<> [/color][color=Navy]1 [/color][color=DarkCyan]or [/color][color=Purple]buttondown [/color][color=DarkCyan]<> [/color][color=Navy]1[/color]
I would expect this code to sit in the do loop until one input goes high as it is ORing the inputs.

What it actually does is wait for all inputs to go high at the same time.

Where am I going wrong?

Cheers
 

hippy

Technical Support
Staff member
I think you have a logical condition error ...

do
loop while Buttonset <> 1 or buttonup <> 1 or buttondown <> 1

Is the same as ...

do
loop while Buttonset = 0 or buttonup = 0 or buttondown = 0

Which will loop while any of the buttons is low, will only continue when all buttons are high.

You probably want one of the following ...

loop until Buttonset = 1 or buttonup = 1 or buttondown = 1

or

loop while Buttonset = 0 and buttonup = 0 and buttondown = 0
 

smeagol

Member
Cheers Hippy
I think you have a logical condition error ...

loop until Buttonset = 1 or buttonup = 1 or buttondown = 1
That one does exactly what I want :eek:

I'll never be be able to pretend to be a Vulcan, I'm obviously not logical enough :confused:
 

westaust55

Moderator
I know this has been sorted but how about a cheapo 'Micky Mouse Logic (M2L) gate
Yes great, simple and cheap.

However, if the OP wanted to later be able to determine the cause of the change then it would still be necessary to bring each signal directly into the PICAXE on separate pins.

Depending upon the complexity of the OPs project code, using SETINT is another possibility which can monitor for any one (or more) of the three signals going high.
 

Circuit

Senior Member
If you can wire the three buttons onto one port then the easiest solution is to monitor for ANY changes in the port.
e.g.
On an 18M2, this would work for eight buttons;
Code:
    [color=Blue]let [/color][color=Purple]dirsB[/color][color=DarkCyan]=[/color][color=Navy]%00000000 [/color][color=Green]; this sets portB pins as inputs.
'Then set the state of this port into a variable;
    [/color][color=Blue]let [/color][color=Purple]b1[/color][color=DarkCyan]=[/color][color=Purple]PinsB[/color]
[color=Green]'Record this state in another variable:
    [/color][color=Blue]let [/color][color=Purple]b0[/color][color=DarkCyan]=[/color][color=Purple]b1[/color]
[color=Green]'Then test b0 for any change in a loop
    [/color][color=Blue]do
        let [/color][color=Purple]b1[/color][color=DarkCyan]=[/color][color=Purple]PinsB
        [/color][color=Blue]if [/color][color=Purple]b0[/color][color=DarkCyan]<>[/color][color=Purple]b1 [/color][color=Blue]then exit
    loop[/color]
Upon exiting the loop you can isolate which switch has changed.
 

westaust55

Moderator
Another options where the signals are from three pushbuttons is to use a single ADC (Analog) input with differing values of resistor associated with each switch and a pull down resistor.
Then a low value (from pull down) when no button is pressed and differing higher voltages towards Vcc when a switch is pressed. The value received from the ADC can be used to identify which switch(es) are pressed.
 
Top