FAST_LED question - multiple lights turning on instead of one

Hi All,
I hope you'll tolerate a question from someone who is new to Arduino programming.
The application will be instrument cluster lights in my old Fiat restoration project.

The LEDs are WS2811 controlled and the board is a Leonardo. I've confirmed that they are individually addressable and not the chips that control 3 LEDs with one chip.

In the final application, a 12V signal from the car will go through an optocoupler 12V-->5V, but right now on the bench I'm just using 5V as a trigger.

Instead of powering an old 12V incandescent light, the 12V signal from the car will go through the optocoupler to the Arduino and then power one the specific addressable LEDs.

My code is below. After an initial start-up routine that cycles all of the lights on and off (and works fine), I'd like it to turn on individual lights when a 5V signal goes to one of the analog pins and turn it off when the power is disconnected. The problem I am having is that once I put 5V to any of the four assigned pins, all 4 lights power on. When I disconnect the 5V, all 4 lights turn off. I can't figure out how to get just one light to turn on by providing signal to one of the analog pins.

Can some kind soul take a look at my code and guide? Thank you!!

#include <FastLED.h>

#define NUM_LEDS  16
#define LED_PIN   2

CRGB leds[NUM_LEDS];

void setup() {
  delay(2000);
  FastLED.addLeds<WS2811, LED_PIN, RGB>(leds, NUM_LEDS);
  FastLED.setBrightness(125);

for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(255,255,255);
      FastLED.show();
      delay(100);
    }

for(int i=0;i<NUM_LEDS;i++){
      leds[i].setRGB(0,0,0);
      FastLED.show();
      delay(100);
    }    
}

void loop() {
       //LIGHTS ON
       if (analogRead(A0)>900) {
          leds[0] = CRGB::White;
          FastLED.show();
          }
       //LIGHTS OFF
       if (analogRead(A0)<200) {
         leds[0] = CRGB::Black;
         FastLED.show();
         }  
        //BRIGHTS ON             
          if (analogRead(A1)>900) {
          leds[1] = CRGB::White;
          FastLED.show();
          }
       //BRIGHTS OFF
       if (analogRead(A1)<200) {
         leds[1] = CRGB::Black;
         FastLED.show();
         }
        
       //HAZARD ON
       if (analogRead(A2)>900) {
          leds[2] = CRGB::White;
          FastLED.show();
          }
       //HAZARD OFF
       if (analogRead(A2)<200) {
         leds[2] = CRGB::Black;
         FastLED.show();
         }  
       //SEATBELT ON
       if (analogRead(A3)>900) {
          leds[3] = CRGB::White;
          FastLED.show();
          }
       //SEATBELT OFF
       if (analogRead(A3)<200) {
         leds[3] = CRGB::Black;
         FastLED.show();
         }
    }

It sounds interesting, however it would help if you posted a schematic, not a frizzy thing.

"karma" point for reading the instructions and properly formatting code.

Connect the analog pins you are not connecting to 5 V or ground at any particular time, to ground. You cannot leave any pin you are using as an input, unconnected.

If that is not the problem, show your circuit schematic and pictures of your assembly. How are you providing 5 V power to the circuit?

"Connect the analog pins you are not connecting to 5 V or ground at any particular time, to ground"
Or set them as digital inputs with pullup resistors enabled.
pinMode (pinX, INPUT_PULLUP);

Use digitalRead(), not analogRead(), and set mode to INPUT_PULLUP for each input. Then connect your buttons between the input pins and ground (no resistors required). When a button is pressed, it will read LOW.

CrossRoads beat me to it. But worth explaining what is going on:

Your inputs are "floating" when your buttons are not pressed. "Floating" means not connected to 5V, ground, or any particular voltage inbetween. So they could read just about anything, including the same as each other, given that you are trying to read a digital signal as though it were an analog signal. The Arduino only has one ADC (Analog-to-Digital converter) which is shared between all its analog pins. An internal multiplexer feeds the ADC from your desired pin. But if the pins are floating, then when you switch between them, they will probably read about the same.

Perfect. Thank you all so much for your help and explanation. It was, indeed, the "floating input" issue. When I grounded the unused pins it worked as expected. Much appreciated!

The problem is the pins you are using, not the unused ones.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.