Single momentary switch controlling two relays

I suspect this posting will reveal in painful detail that I am not a programmer. Generally I can find sample code that I have been able to use but I've not been lucky this time. Basically I'm trying to use a normally open (NO) push button to sort of sequentially control two relays. Initially the relays will be controlling two sets of LED lamps (3W to 10W variety).

From both outputs Low/Off, press button, if A=low/off and B=low/off, turn A=high/on (leave B=off)
press button, if A=high/on and B=low/off, turn A=low/off and B=high/on
press button, if A=low/off and B=high/on, turn A=high/on and B=high/on
press button, if A=high/on and B=high/on, turn both A & B = low/off

Basically the single button will command the relay/LEDs to light in sequence of A only, then B only, then Both A & B and finally both off.

I've reviewed some of the example code here, and initially thought the "Button State change" would apply but now don't think so. I'm now thinking I need some sort of "if/then" type of statement but can't figure it out. I firmly suspect I'll need the debounce logic for the momentary input switch but have seen a few examples of that and feel I can add that when the rest is figured out.

I'm intending on creating a project to allow one button to control two sets of auxiliary lights on a motorcycle. yes, I know it would be simple to add two SPST switches and control the lights but where is the challenge in that.

So, any guidance/advice for my simple brain??

Thanks in advance,

Rick

Sounds like you just need a push button counter.

You can look at the Button State change code and make it so when the button is pressed (Not HELD), you increment a counter by 1 (0 - 3); If the counter goes above 3, reset it back to 0.

Then from that counter, you can turn on the corresponding relays. Also look into Case statements for this part.

Example using an LED:

const byte LED = 6; // PWM pin
const byte Button = 2;
byte ButtonState;
byte last = HIGH;
byte count = 0;

void setup() 
{
  Serial.begin(115200);
  pinMode(LED, OUTPUT);
  pinMode(Button, INPUT_PULLUP);
}

void loop() 
{
  ButtonState = digitalRead(Button);
  
  if(ButtonState != last) 
  {
    if(ButtonState == LOW)
    {
      count += 5;
      analogWrite(LED, count);
      Serial.println(count);
    }
  }
  
  if(count > 255)
  {
    digitalWrite(LED, LOW);
    count = 0;
  }
  
  last = ButtonState;
}

Actually instead of case statments, you can just use the bits from the counter variable.
digitalWrite(Relay1, count & 0x01);
digitalWrite(Relay2, count & 0x02);

Thank you.

I wish I had a handy flow-chart program....

I suspect this posting will reveal in painful detail that I am not a programmer.

For something like this, the important thing to remember is that the processor never really stops or pauses.* In this case, most of the time your program will be sitting there looping, doing nothing except checking to see if a button is pushed.

There's always more than one way to do it. I see this as 3 loops nested inside your main loop. It could be "simplified", but one loop for each of the 3 conditions seems like the most straightforward way to do it.

BTW - Loops (doing stuff over-and-over) and conditional branching/conditional execution (if this do that, else do "nothing" or do something else) are the most important concepts in programming. Once you understand these concepts, you should be able to figure-out the basic logic of how to structure your program.

There are 3 kinds of loops (in addition to the main Arduino loop) and they all combine looping with conditional logic that allows you to break-out of the loop when some condition is met.

Of top of my head, I'm thinking [u]do-while loops[/u].

The basic program flow would look like this:


START MAIN loop()

Turn-off B, Turn on A
Start do-while loop

While button is not pressed, loop-back to start of this do-while loop, else exit loop and continue below.

Turn-off A, Turn on B
Start do-while loop

While button is not pressed, loop-back to start of this do-while loop, else exit loop and continue below.

Turn on A
Start do-while loop

While button is not pressed, loop-back to start of this do-while loop, else exit loop and continue to end of main loop.

END OF MAIN loop(), go back to start of main loop.

Of course, that's not C-code, it's just the basic logic, or partial psudo-code.

You'll need to add some short delays (a few milliseconds) or some other switch debouncing method so that one button-push doesn't "look like" several button-pushes to the program.

*The delay() function puts the processor in a "do nothing" loop 'till the time is up.

Hi, your light pattern is;

B A
0 0
0 1
1 0
1 1

Which is binary counting, on each button press.

Tom...... :slight_smile: