Go Down

Topic: Led Strip ws2812b with button (Read 915 times) previous topic - next topic

Aveigal

Feb 23, 2019, 04:02 pm Last Edit: Feb 27, 2019, 03:32 am by Aveigal
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:

Code: [Select]
// 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

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.

danardo

Change both "for" to:
Code: [Select]
for(int i=0;i<NUMPIXELS;i++)

Aveigal

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.

Aveigal

Change both "for" to:
Code: [Select]
for(int i=0;i<NUMPIXELS;i++)
This onlu change the direction of the leds

danardo

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

Aveigal

#6
Feb 26, 2019, 01:42 pm Last Edit: Feb 26, 2019, 06:51 pm by Aveigal
Hello

You're right I was wrong.

Then how can change the direcction of the leds?

Thanks in advance

Grumpy_Mike

Quote
Then how can change the direcction of the leds?
Code: [Select]
for(int i=NUMPIXELS; i>0; i--)

Aveigal

#8
Mar 12, 2019, 06:01 pm Last Edit: Mar 12, 2019, 06:02 pm by Aveigal
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.

Code: [Select]

#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;
     }
}

Grumpy_Mike

Quote
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.

Aveigal

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.

Grumpy_Mike

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.

Aveigal

#12
Mar 15, 2019, 10:50 am Last Edit: Mar 15, 2019, 10:56 am by Aveigal
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)

Code: [Select]

#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();
          }
       }
}

danardo

Try this, it should work  :)

Code: [Select]
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;
  }
}

Aveigal

It works perfect!!! thanks you !!!

Go Up