Control LED Light bars

Hello Everyone!

Long time lurker, tinkerer and maker. This forum has been a great help for years ( Search function is amazing :) ). Finally after years I find the need to post a question due to not being able to locate the correct answer that helps. I may be asking the question wrong, or have the completely wrong approach.

Here is what I am trying to do: I have 4 LED light bars. (Like the kind you see on off road pick up trucks.) I have them mounted to a Side by side UTV, 2 in the front (one up high (large bar) and one down low on the bumper (smaller size) then two small work lights mounted to the rear roll cage (left and right side). I would like to control the on off of each bar with an Arduino uno and a 4 relay setup ( I have all the hardware working as it "should" I believe. I can turn them all on and off currently (see code)

Here is the sticking point for me; I have a push button that I want to cycle through different lighting options ( Front upper bar alone, front lower bar alone, both front bars, rear lights alone and all lights. I have this working ) and I also have two switches that I would like to control just the front bars and just the rear work lights (cant get this to work) (make it easy for the wife to turn them on and off as she uses it a lot as well). Then comes the fun part, that makes my head explode :o . I would some how like to make a function for strobe (just flash them on and off in a pattern that I can whip up ( I can handle that sort of stuff), as we use the vehicle to plow in the winters and also move live stock across roads, so the warning lights would be awesome! It could be as simple as "If both rocker switches are on, enter strobe mode when the button is pressed" I dont know/understand how to change the function if the switches are flipped or how to get them to override the buttons.

Here is the code I have that cycles the bars currently. Any help would be greatly appreciated!

int ledPin1 = 12;
int ledPin2 = 11;
int ledPin3 = 10;
int ledPin4 = 9;
int switchPin = 7;
int FrontSwitchPin = 6;
int RearSwitchPin = 5;
int count = 0;
int old_something;
int old_frtsomething;
boolean lastButton;
boolean currentButton = false;
boolean ledOn = false;

void setup() {
    Serial.begin(9600);
    pinMode(switchPin, INPUT);
    pinMode(FrontSwitchPin, INPUT);
    pinMode(RearSwitchPin, INPUT_PULLUP);
    pinMode(ledPin1, OUTPUT);
    pinMode(ledPin2, OUTPUT);
    pinMode(ledPin3, OUTPUT);
    pinMode(ledPin4, OUTPUT);
    count = 0;

    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin4, HIGH);
    digitalWrite(FrontSwitchPin, HIGH);
}
//debounce function to stabilise the button
boolean debounce(boolean last)
{
    boolean current = digitalRead(switchPin);
    if (last != current)
    {
        delay(5);
        current = digitalRead(switchPin);
    }
    return current;
}

void loop() {
    lastButton = currentButton;
    currentButton = debounce(lastButton);
    FrontSwitchPin = digitalRead(FrontSwitchPin);
    if (lastButton == false && currentButton == true)
    {
        if (count == 0)
        {
            count++;
            digitalWrite(ledPin1, LOW);
            digitalWrite(ledPin2, HIGH);
            digitalWrite(ledPin3, HIGH);
            digitalWrite(ledPin4, HIGH);
        }

        else if (count == 1)
        {
            count++;
            digitalWrite(ledPin1, HIGH);
            digitalWrite(ledPin2, LOW);
            digitalWrite(ledPin3, HIGH);
            digitalWrite(ledPin4, HIGH);
        }

        else if (count == 2)
        {
            count++;
            digitalWrite(ledPin1, LOW);
            digitalWrite(ledPin2, LOW);
            digitalWrite(ledPin3, HIGH);
            digitalWrite(ledPin4, HIGH);
        }
        else if (count == 3)
        {
            count++;
            digitalWrite(ledPin1, HIGH);
            digitalWrite(ledPin2, HIGH);
            digitalWrite(ledPin3, LOW);
            digitalWrite(ledPin4, LOW);
        }
        else if (count == 4)
        {
            count++;
            digitalWrite(ledPin1, LOW);
            digitalWrite(ledPin2, LOW);
            digitalWrite(ledPin3, LOW);
            digitalWrite(ledPin4, LOW);
        }
        else if (count == 5)
        {
            count++;
            digitalWrite(ledPin1, HIGH);
            digitalWrite(ledPin2, HIGH);
            digitalWrite(ledPin3, HIGH);
            digitalWrite(ledPin4, HIGH);
        }
        else if ((count == 6) && (FrontSwitchPin == HIGH))
        {
            count = 0;
            digitalWrite(ledPin1, LOW);
            digitalWrite(ledPin2, HIGH);
            digitalWrite(ledPin3, LOW);
            digitalWrite(ledPin4, HIGH);
        }
    }
    int something = count;
    if (something != old_something)
        Serial.println(count);  // display if it changed

    old_something = something;
int old_something;

Are you serious? Been out in the cold so long your brain froze?

    pinMode(switchPin, INPUT);
    pinMode(FrontSwitchPin, INPUT);
    pinMode(RearSwitchPin, INPUT_PULLUP);

Be a lot simpler if all the switches were wired the same, eh?

    digitalWrite(FrontSwitchPin, HIGH);

So you don't have to use INPUT_PULLUP as the mode?

    count = 0;

Pretty hard to guess what you are counting.

Why are you debouncing only one the three switches?

    int something = count;
    if (something != old_something)
        Serial.println(count);  // display if it changed

    old_something = something;

Why do you need to make a copy of count?

Where is the rest of the code?

PaulS: int old_something;

Are you serious? Been out in the cold so long your brain froze?

It has been cold... I should have clarified, I have pieced this stuff together from other sources... cant take all the credit for the names. Just trying to do my best with it.

 pinMode(switchPin, INPUT);
 pinMode(FrontSwitchPin, INPUT);
 pinMode(RearSwitchPin, INPUT_PULLUP);

Be a lot simpler if all the switches were wired the same, eh?..

Meh, Good catch. This was a hardware test that I left in. Fixed it now. It has an external resistor.

 digitalWrite(FrontSwitchPin, HIGH);

So you don't have to use INPUT_PULLUP as the mode?

Same as above.

 count = 0;

Pretty hard to guess what you are counting.

Counting the presses of the button from what I understand.

Why are you debouncing only one the three switches?

There is only one button, two switches.

 int something = count;
 if (something != old_something)
 Serial.println(count);  // display if it changed

old_something = something;



Why do you need to make a copy of count?

**Lost me here. This was also a serial debug to see where the code was running/stuck for me.** 

Where is the rest of the code?

This is as far as I made it before asking myself if this is the proper approach.

Counting the presses of the button from what I understand.

Is switchPressCount that hard to type or copy/paste?

There is only one button, two switches.

Buttons are for keeping shirts from flapping in the breeze. Push button switches are STILL switches.

You are better off changing all switches to momentary buttons instead of the rockers. That'd be the simplest way about it to give you the functionality that you want.

It gets a little more complex if you want to have those manual rocker switches override for function in case the Arduino bits fail. You'd have to treat each light as a 3-way circuit with a traveller to the relay, assuming they have NO and NC contacts.

On the note of relays, though, and since you mentioned wanting to strobe the lights- you should look into ditching relays and using MOSFETs. They are just LED lights on DC voltage, MOSFETs will allow you to fade and strobe while a relay will just die an early death. Much more reliable, too, being solid state devices. I'm assuming the relays you have now are electromechanical.

If you give the rocker switches priority over the button you can just ignore the button when a rocker is flipped. At the top of loop() check the switches. If one or both of them is flipped turn the appropriate lights on, if neither is flipped look at the button as usual.

void loop() {

  bool frontSwitchFlipped = digitalRead(FrontSwitchPin) == HIGH;
  bool rearSwitchFlipped = digitalRead(RearSwitchPin) == HIGH;

  // if a switch is flipped
  if ( frontSwitchFlipped || rearSwitchFlipped )
  {
    if ( frontSwitchFlipped )
    {
      // turn on front

    }

    if ( rearSwitchFlipped )
    {
      // turn on rear
    }

  } else {

    // code to check for button press

  }

}

How about entering strobe mode (disabling the button and switches) if the button is held down on start up? If you did that it should not be too difficult to integrate the change with your current code. When in strobe mode you would update the lights based on a timer through a given number of strobe steps to form a pattern.

The general idea would be:

int switchPin = 7;
int FrontSwitchPin = 6;
int RearSwitchPin = 5;

bool strobeMode;                            // true => strobe only, ignore button and switch
const unsigned long strobeInterval = 1000;  // how fast to strobe
const int strobeStates = 4;                 // # of states when strobbing
int strobeStep = 1;                         // current strobe state
unsigned long strobeStart = 0UL;


void setup() {

  // set strobe mode
  strobeMode = (digitalRead(switchPin) == HIGH);
  strobeStart = millis();

}

void loop() {

  bool frontSwitchFlipped = digitalRead(FrontSwitchPin) == HIGH;
  bool rearSwitchFlipped = digitalRead(RearSwitchPin) == HIGH;


  // if in strobe mode
  if ( strobeMode )
  {

    // update pattern
    updateStrobe();

  } else if ( frontSwitchFlipped || rearSwitchFlipped ) {

    // handle switches

  } else {

    // check button

  }


}

void updateStrobe()
{

  // if time to change lights
  if ( millis() - strobeStart > strobeInterval )
  {
    // turn on desired lights
    switch ( strobeStep )
    {
      case 1: // step 1 lights on
        break;

      case 2: // step 2 lights on
        break;

      case 3: // step 3 lights on
        break;

      case 4: // step 4 lights on
        break;

      default:
        break;
    }

    // advance strobe pattern
    strobeStep = (strobeStep == strobeStates) ? 1 : strobeStep + 1;

    // mark time
    strobeStart = millis();

  } // if
  
}

Hello,

I know old topic but I am trying to do something similar and wanted to know what hardware you chose or was given to use? I'm a total newb when it comes to arduino but am in an IT background so don't think it'll be that hard just unsure of what hardware to use. Any help would be appropriated.

Thanks, Steve