LED Lighting Sequential *Help*

Trying to do a project with Sequential LED Lights for a car tail lights, i have a bit of code but isn’t working as intended, how it is coded it shouldn’t do anything until it has a input through an analog input (A8 on a Mega 2560) but what it is doing is running through the loop without an input, and when an input is given it stops until there is no more input.

The File will be available for download.

int LED1 = 13;
int LED2 = 12;
int LED3 = 11;
int LED4 = 10;
int LED5 = 9;
int LED6 = 8;
int LED7 = 7;
int LED8 = 6;
int LED9 = 5;
int LED10 = 4;
int LED11 = 3;
int LED12 = 2;
int BrakeOn = A8 ;


void setup() {
  pinMode(BrakeOn, INPUT); //brake input
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
  pinMode(LED6, OUTPUT);
  pinMode(LED7, OUTPUT);
  pinMode(LED8, OUTPUT);
  pinMode(LED9, OUTPUT);
  pinMode(LED10, OUTPUT);
  pinMode(LED11, OUTPUT);
  pinMode(LED12, OUTPUT);
}


void loop() {
  int sensorValue = analogRead(BrakeOn);

  if (analogRead(BrakeOn) == HIGH)
  {

    while (analogRead(BrakeOn) == HIGH) {

      digitalWrite(LED1, HIGH);    // turn on LED1
      delay(100);                  // wait for 200ms
      digitalWrite(LED2, HIGH);    // turn on LED2
      delay(100);                  // wait for 200ms
      digitalWrite(LED3, HIGH);    // turn on LED3
      delay(100);                  // wait for 200ms
      digitalWrite(LED4, HIGH);    // turn on LED4
      delay(100);                  // wait for 200ms
      digitalWrite(LED5, HIGH);    // turn on LED5
      delay(100);                  // wait for 200ms
      digitalWrite(LED6, HIGH);    // turn on LED6
      delay(100);                  // wait for 200ms
      digitalWrite(LED7, HIGH);    // turn on LED7
      delay(100);                  // wait for 200ms
      digitalWrite(LED8, HIGH);    // turn on LED8
      delay(100);                  // wait for 200ms
      digitalWrite(LED9, HIGH);    // turn on LED9
      delay(100);                  // wait for 200ms
      digitalWrite(LED10, HIGH);    // turn on LED10
      delay(100);                  // wait for 200ms
      digitalWrite(LED11, HIGH);    // turn on LED11
      delay(100);                  // wait for 200ms
      digitalWrite(LED12, HIGH);    // turn on LED12
      delay(100);                  // wait for 200ms
      digitalWrite(LED1, LOW);     // turn off LED1
      delay(100);                  // wait for 300ms
      digitalWrite(LED2, LOW);     // turn off LED2
      delay(100);                  // wait for 300ms
      digitalWrite(LED3, LOW);     // turn off LED3
      delay(100);                  // wait for 300ms
      digitalWrite(LED4, LOW);     // turn off LED4
      delay(100);                  // wait for 300ms
      digitalWrite(LED5, LOW);     // turn off LED5
      delay(100);                  // wait for 300ms
      digitalWrite(LED6, LOW);     // turn off LED6
      delay(100);                  // wait for 300ms
      digitalWrite(LED7, LOW);     // turn off LED7
      delay(100);                  // wait for 300ms
      digitalWrite(LED8, LOW);     // turn off LED8
      delay(100);                  // wait for 300ms
      digitalWrite(LED9, LOW);     // turn off LED9
      delay(100);                  // wait for 300ms
      digitalWrite(LED10, LOW);     // turn off LED10
      delay(100);                  // wait for 300ms
      digitalWrite(LED11, LOW);     // turn off LED11
      delay(100);                  // wait for 300ms
      digitalWrite(LED12, LOW);     // turn off LED8
      delay(100);                  // wait for 300ms before running program all over again
    }
  } else {

    digitalWrite(LED1, LOW);     // turn off LED1
    digitalWrite(LED2, LOW);     // turn off LED2
    digitalWrite(LED3, LOW);     // turn off LED3
    digitalWrite(LED4, LOW);     // turn off LED4
    digitalWrite(LED5, LOW);     // turn off LED5
    digitalWrite(LED6, LOW);     // turn off LED6
    digitalWrite(LED7, LOW);     // turn off LED7
    digitalWrite(LED8, LOW);     // turn off LED8
    digitalWrite(LED9, LOW);     // turn off LED9
    digitalWrite(LED10, LOW);     // turn off LED10
    digitalWrite(LED11, LOW);     // turn off LED11
    digitalWrite(LED12, LOW);     // turn off LED8
  }
}

taillightv2.ino (3.86 KB)

This is problematic:

  int sensorValue = analogRead(BrakeOn);

  if (analogRead(BrakeOn) == HIGH)

Is BrakeOn truly an analog signal or, is it digital - like a switch?

Whatever pin you plan to use , as this is automotive you will need to convert the input to a 5v signal and you need a pull down resistor on that input .

Thanks for the replays yes i understand that i will need to bring the 12v down to 5v. what i am trying to do is when it gets a 5v input it will start the loop. i will be using a Arduino nano for the project. dougp yes i am guessing it would be like a switch . hopping someone can help

When You use an analog read compare the reading with a triggering value in the range of 0....1023, not with HIGH.

Also when comparing an analogue reading never use == but >= or <=

R0N:
Thanks for the replays yes i understand that i will need to bring the 12v down to 5v. what i am trying to do is when it gets a 5v input it will start the loop. i will be using a Arduino nano for the project. dougp yes i am guessing it would be like a switch . hopping someone can help

You can use an opto-isolator to bridge car 12V to Duino 5V safely. 12V on one side closed a switch on the other with kilovolt protection in between. There's a led and a detector in the chip, you can make your own if you have parts. On the Duino side, make a pin INPUT_PULLUP and have the opto ground it when the 12V side is ON.

You will need at least a resistor on the 12V side, possibly a zener diode to ground way-overvoltage.

The IDE Built-In Examples in section 5 show the use of arrays and loops and logic-structures.
See how many lines of copy&edit you didn’t need to do. See how easy it can be to change the led array now.

// don't use an int where a byte or char will do, it wastes RAM -- a bad habit
const byte ledCount = 12;  
const byte LED[ ledCount ] = { 13,12,11,10,9,8,7,6,5,4,3,2 };
byte i; // use for indexing the LED array

const byte BrakeOn = A8;

byte lastBrakeState;


void setup() 
{
  pinMode(BrakeOn, INPUT); //brake input

  for ( i = 0; i < ledCount; i++ )
  {
    pinMode(LED[ i ], OUTPUT);
  }
}


void loop() 
{
  byte sensorValue = digitalRead(BrakeOn); // because LOW is 0 and HIGH is not-0, analogRead isn't much good for HIGH/LOW

  if ( sensorValue == HIGH )
  {
    for ( i = 0; i < ledCount; i++ ) 
    {
      digitalWrite(LED[ i ], HIGH);
      delay(100);                  // wait for 100ms
    }
    for ( i = 0; i < ledCount; i++ ) 
    {
      digitalWrite(LED[ i ], LOW);
      delay(100);                  // wait for 100ms
    }
  } 
  else // as is this else just turns OFF what's already turned OFF
  {
    if ( lastBrakeState != sensorValue )
    {
      for ( i = 0; i < ledCount; i++ ) 
      {
        digitalWrite(LED[ i ], LOW);
      }
    }
  }

  lastBrakeState = sensorValue;
}

What this will do is run the whole sequence for the shortest brake tap and continue to sequence while brake is down.
What it will not do is stop in the middle of a sequence if the brake is let up.

Thank you GoForSmoke for that its my first time trying to do this i just put it on a mega and what i am finding is i dont need to use 5v to activate the loop when i put my hand near the A8 pin it activates the loop so what would i put in to make the A8 pin only work when 5v is applied . Should i use a triggering value ?? thanks again for the help

when i put my hand near the A8 pin it activates the loop so what would i put in to make the A8 pin only work when 5v is applied

You should put in a 10K resistor from the A8 input pin to ground. You can’t do anything in software if you want to trigger on a 5V input.

R0N:
Thank you GoForSmoke for that its my first time trying to do this i just put it on a mega and what i am finding is i dont need to use 5v to activate the loop when i put my hand near the A8 pin it activates the loop so what would i put in to make the A8 pin only work when 5v is applied . Should i use a triggering value ?? thanks again for the help

I don't know how you have it wired but be aware that you and just about everything has an electrical field that can affect wires, usually in a small way. So when you say "when i put my hand near the A8 pin" I wonder what is affecting what. It shouldn't do that in your case while other projects look for just that kind of effect.

You should be reading the pin digital not analog since the brake is either ON or OFF.

Thanking you all its working the 10K resistor did the job again thank your all

R0N:
i will be using a Arduino Nano for the project.

Good choice.

I would actually recommend you use this for your development as well, rather than confusing yourself with the oddities of a Mega 2560 which is only really appropriate or even useful in a small number of cases.

Now for automotive use, there are a number of considerations. You need to provide a very reliable ground to the car chassis. You need a 5 V power supply for the Arduino; the on-board regulator is essentially useless; a switchmode "buck" converter rated for surges up to say, 30 V would be a good start.

To sense 12 V inputs, you need a voltage divider such as a 47k resistor from that input to your pin and a 22k resistor from pin to ground. A diode with anode to the pin and cathode to 5 V is also a good idea. Testing without anything connected to the pin makes no sense at all. And it also makes no sense to use analogRead to read ON/ OFF values.