Led Strip ws2812b with button

Dear

I need your help, I’m trying to make a strip of leds WS2812b turns red (all) when a digital input is at “0” and changes to white (all) when it is at “1”

This is my code:

// LED + Pulsador
#include <Adafruit_NeoPixel.h>

// Pines totales tira
#define NUMPIXELS 72

// Puerto de datos tira
#define PIN 5

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); //define el objeto pixels que es la tira de leds

const int led=5;
const int pulsador=4;
int val;

void setup() 
{
pixels.begin();
pinMode(pulsador,INPUT);
pinMode(led,OUTPUT);
}

void loop() 
{
 val=digitalRead(pulsador);
 if (val==HIGH)
   { 
     for(int i=71;i<NUMPIXELS;i--)
     {
       pixels.setPixelColor(i, pixels.Color(255,255,255)); // BLANCO
       pixels.show(); // Actualiza los leds.
       delay(2); // espera para el encendido de cada led.
      }
    }
 else 
    {
     for(int i=71;i<NUMPIXELS;i--)
     { 
       pixels.setPixelColor(i, pixels.Color(255,000,000)); // ROJO
       pixels.show(); // Actualiza los leds.
       delay(1);
      }
     }
}

It works correctly every time arduino starts but then the loop does not continue, it does not change state.

Does anyone know what’s wrong?

Thank you so much for everything

Do you have a pull up or pull down resistor on that input pin? You need one if you are not using the internal pull ups.

Change both “for” to:for(int i=0;i<NUMPIXELS;i++)

Grumpy_Mike:
Do you have a pull up or pull down resistor on that input pin? You need one if you are not using the internal pull ups.

I a using pull down but not works.

danardo:
Change both “for” to:

for(int i=0;i<NUMPIXELS;i++)

This onlu change the direction of the leds

Do you understand how work your "for"? Change as I said.

Hello

You’re right I was wrong.

Then how can change the direcction of the leds?

Thanks in advance

Then how can change the direcction of the leds?

for(int i=NUMPIXELS; i>0; i--)

The first version of the project works correctly, I have decided to modify some small details, depending on the input will change color to another as follows:

LED off
A = + red color (simultaneous ignition)
A = + and B = + white color (progressive ignition)
A = + (after white) change from white to progressive red
LED off (simultaneous shutdown)

The case is that I want that if B never happens to + the LEDs turn on and off simultaneously but the B ignition is progressive (this is OK) and the step from B to A is also progressive (here I am stuck).

I have thought to introduce a variable “BANDERA” that if it is “0” A always turn on and off simultaneously but if “BANDERA” at some point becomes “+” the shutdown is progressive.

#include <Adafruit_NeoPixel.h>

// Pines totales tira
#define NUMPIXELS 90

// Puerto de datos tira
#define PIN 4

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); //Define el objeto pixels que es la tira de leds

const int LUZ_ENCENDIDA=2;
const int MARCHA_ATRAS=3;
const int SALIDA=4;
int ESTADO_LUZ;
int ESTADO_MARCHA;
int BANDERA = 0;

void setup() 
{
strip.begin();
pinMode(LUZ_ENCENDIDA,INPUT);
pinMode(MARCHA_ATRAS,INPUT);
pinMode(SALIDA,OUTPUT);
}

void loop() 
{
  ESTADO_LUZ=digitalRead(LUZ_ENCENDIDA);
  ESTADO_MARCHA=digitalRead(MARCHA_ATRAS);

   if (ESTADO_LUZ==LOW & ESTADO_MARCHA==LOW) // Todo apagado
       
       { 
           for(int i=0;i<NUMPIXELS;i++)
          {
           strip.setPixelColor(i, strip.Color(000,000,000)); // LED´s Apagados
          }
          strip.show(); // Simultaneo
        }
        
    if (ESTADO_LUZ==HIGH & ESTADO_MARCHA==LOW) // Luz ambiente encendida

        { if (BANDERA==1)
           for(int i=0;i<NUMPIXELS;i++)
            {
             strip.setPixelColor(i, strip.Color(200,000,000)); // LED´s Rojo
             strip.show(); // Progresivo.
            }
          else
           for(int i=0;i<NUMPIXELS;i++)
            {
             strip.setPixelColor(i, strip.Color(200,000,000)); // LED´s Rojo
            }
            strip.show();   // Simultaneo    
         }    

    if (ESTADO_LUZ==HIGH & ESTADO_MARCHA==HIGH) // Marcha atrás puesta
     { 
      for(int i=0;i<NUMPIXELS;i++)
      {
        strip.setPixelColor(89-i, strip.Color(150,150,150)); // LED´s Blanco 255 seria brillo máximo
        strip.show(); // Progresivo.
      }
      BANDERA==1;
     } 
}

LED off
A = + red color (simultaneous ignition)
A = + and B = + white color (progressive ignition)
A = + (after white) change from white to progressive red
LED off (simultaneous shutdown)

Sorry but I have no idea what that means.

Sorry my mistake

A = +5v in pin 2 Sensor to know the car is on
B= +5v in pin 3 sensor to know the reverse gear is on

The sequence is as follows.

I turn on the car -> the LEDs turn red simultaneously
I turn off the car -> LED's turn off simultaneously

With the car on (leds are red), the leds change to white sequentiall when reverse gear is on, when the reverse gear is removed, the LEDs change back to red sequentially.

I hope to have explained myself better.

Still not sure I understand. But your code is written using just two functions setup and loop.

It will be a lot simpler to understand if you make each action you want the LEDs to do into its own function. Then you can concentrate on the logic to read the sensors and call the functions that do the action.

Dear

I make this changes but not work I think that is somenting wrong in the logic but a have no idea to how make it.

The behavior should be as follows:

When the car is switched on (PIN 2 has + 5v) the LEDs turn on at the same time in red. (This works)

When the car is switched off (PIN 2 has 0) the LEDs turn off at the same time in red. (This works)

If the car is on (PIN 2 has + 5v) and rear gear is on (PIN 3 has + 5v) the LEDs change to white sequentially (this works)

When rear gear is off again LEDs should return to red sequentially (This fails)

#include <Adafruit_NeoPixel.h>

// Pines totales tira
#define NUMPIXELS 90

// Puerto de datos tira
#define PIN 4

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); //Define el objeto pixels que es la tira de leds

const int LUZ_ENCENDIDA=2;
const int MARCHA_ATRAS=3;
const int SALIDA=4;
int ESTADO_LUZ;
int ESTADO_MARCHA;
int BANDERA;

void Turn_ON() // Car turn ON and all LED´s turn ON same time
{
  for(int i=0;i<NUMPIXELS;i++)
  {
   strip.setPixelColor(i, strip.Color(200,000,000)); // LED´s Red.
  }
  strip.show(); // Update LED´s. 
}

void Turn_OFF() // Car turn OFF and all LED´s turn OFF same time
{
  for(int i=0;i<NUMPIXELS;i++)
  {
   strip.setPixelColor(i, strip.Color(000,000,000)); // LED´s OFF
  }
  strip.show(); // Update LED´s.
}

void Rear_Gear_ON() // Rear gear ON and all LED´s turn ON one by one
{
  for(int i=0;i<NUMPIXELS;i++)
  {
  strip.setPixelColor(89-i, strip.Color(150,150,150)); // LED´s White
  strip.show(); // Update LED´s.
  }
}

void Rear_Gear_OFF() // Rear gear OFF and all LED´s turn OFF one by one (change to red one by one)
{
  for(int i=0;i<NUMPIXELS;i++)
  {
   strip.setPixelColor(i, strip.Color(200,000,000)); // LED´s Red.
   strip.show(); // Update LED´s.
  }
}

void setup() 
{
strip.begin();
pinMode(LUZ_ENCENDIDA,INPUT);
pinMode(MARCHA_ATRAS,INPUT);
pinMode(SALIDA,OUTPUT);
}

void loop() 
{
  ESTADO_LUZ=digitalRead(LUZ_ENCENDIDA);
  ESTADO_MARCHA=digitalRead(MARCHA_ATRAS);
  BANDERA = 0;

   if (ESTADO_LUZ==LOW & ESTADO_MARCHA==LOW) // LED´s OFF   
       { 
        Turn_OFF();   
       }
        
    if (ESTADO_LUZ==HIGH & ESTADO_MARCHA==LOW) // LED´s in RED
       { 
        Turn_ON(); 
       }   
        
    if (ESTADO_LUZ==HIGH & ESTADO_MARCHA==HIGH) // LED´in white
       { 
         BANDERA==BANDERA+1;
          if (BANDERA > 0)
          {
            Rear_Gear_OFF();  
          }
          else
          {
            Rear_Gear_ON();
          }
       }
}

Try this, it should work :slight_smile:

void loop() {
  ESTADO_LUZ = digitalRead(LUZ_ENCENDIDA);
  ESTADO_MARCHA = digitalRead(MARCHA_ATRAS);

  if (ESTADO_LUZ == LOW && ESTADO_MARCHA == LOW) { // LED´s OFF
    Turn_OFF();
  } else if (ESTADO_LUZ == HIGH && ESTADO_MARCHA == LOW) { // LED´s in RED
    if (BANDERA == 1) {
      Rear_Gear_OFF();
      BANDERA = 0;
    } else {
      Turn_ON();
    }
  } else if (ESTADO_LUZ == HIGH && ESTADO_MARCHA == HIGH) { // LED´s in white
    Rear_Gear_ON();
    BANDERA = 1;
  }
}

It works perfect!!! thanks you !!!

Dear

I continue with this project and I saw that I have some problem.

I am trying that a strip of LEDs behave in one way or another depending on the entry by 2 pins (PIN 2 and PIN 3) the problem I have is that one of them (PIN 3) occasionally sends voltage to verify the circuit and creates false positives.

I have thought that only the output is activated when PIN 3 is for example 1 second active, but I can not get it to work correctly.

the problem I have is that one of them (PIN 3) occasionally sends voltage to verify the circuit and creates false positives.

If that is truly the problem then you have a hardware problem. So to be able to diagnose this we need a schematic of your circuit along with a photograph of you hardware that is in sufficient clarity that we can follow the wiring.

I have thought that only the output is activated when PIN 3 is for example 1 second active,

That is just a sticking plaster and will not actually cure the problem.

A = +5v in pin 2 Sensor to know the car is on
B= +5v in pin 3 sensor to know the reverse gear is on

So is this car a real grownups car or is it a model?

If this is a real car then what have you done to mitigate the hostile electric environment you have?
What is the value of your pull down resistor and do you have a 0.1uF ceramic capacitor across this resistor.

It is the normal behavior of the car, when turned on it sends current peaks and analyzes them to detect fused light bulbs. There is no fault here.

Wait 1 second is a valid solution since there is no hardware problem

Is a real car.

Voltage problems are solved with voltage transformers and stabilizers at 5v so even if the car goes up to 15v it will always deliver 5v to arduino.

Wait 1 second is a valid solution since there is no hardware problem

Well I am telling you there is a problem with your hardware. If you don't want to here this then it is up to you. But I will waste no more time advising you.
Goodbye and good luck.

.