Led strip WS2812 . problem with code and hw response

Status
Not open for further replies.

Jocman

Member
Hi all, I'm new in this forum and in teensy world.
First of all I'm not a technician, just a simple "lover"; I'm just trying to make something bigger starting from little experiments.
I'm trying to develope a simple code to drive some WS2812 leds (by now only 80, but I'm planning to add more stripes - 240 leds and 130 leds).
By the way, currently I'm having problem with the 80 leds.

My goal is to light 16 groups of 5 leds in sequence, using an IR switch to light them, then they have to fade off all together after a while. each group of 5 lights at the same time.
this has to be done in both directions.
To make it easy to understand, I want to light a stair (16 steps) by lightning in sequence all the step depending on the direction (I've one IR switch at the bottom and one at the top of the stair), then wait for a while and fade off. Each step is lightened by 5 leds.
I started this project by using a Trinket board, and for my tests I've been using only 4 groups of 5 leds (total 20 leds to drive).
The test worked very fine: depending on wich switch I used, the "virtual steps" lightened and faded of in the right way. So I just updated the sketch with the number of leds i'll need in the stair.
The problem was that trinket, as I understood, can't drive more then a fixed number of WS2812, because the low RAM. I tried the same to use a trinket with the 80 leds, but I got just something crazy: as I gave power, all the led started switching on as a rainbow, then switched all to white, and at the same time some of them started changing color, and so on.
I tought it was because the RAM limit of trinket.
So I got a Teensy 3.1, 'cause as I understood he has more RAM and can easily drive the 80 leds.
I changed some pin value in my sketch then uploaded to teensy. But when I put the power on i got the same results as the "crazy trinket".
I'm using an external 5v source to drive leds, teensy and switches. Arduino environment + teensy loader to program the code (win 7) and Neopixel libraries
I post down the code: the code is longer than the descripion I made up, because I programmed the sketch to drive 2 strips in different behaviour. By the way I presume that connecting only the stripe A and not the B too shouldn't make this difference, so to justify the craziness....
Thanks in advance for your help.

Andrea



Code:
/*
ACCENSIONE IN SEQUENZA E SPEGNIMENTO IN FADING DEI GRADINI
CONTEMPORANEAMENTE ILLUMINO IN SEQUENZA LA CORSIA SEGUENDO GLI SCALINI
RAMPA TAVERNA>SALA SERVITA DAI LED 0>59 (12 scalini - 5 LED A SCALNO)
RAMPA SALA>NOTTE SERVITA DAI LED 0>79 (16 scalini - 5 LED A SCALINO)
PROGRAMMO LA RAMPA SALA>ZONA NOTTE
ADATTATO SU TEENSYDUINO
============================================================
NOTA
QUANDO SI PROGRAMMERANNO GLI IRSWITCH RICORDARSI CHE A
INIZIO SKETCH I VALORI VANNO SETTATI A HIGH E DI CONSEGUENZA
LE ISTRUZIONI IF VANNO SETTATE A LOW
============================================================

*/

#include <Adafruit_NeoPixel.h>

#define PIN_A 10  //definisco una costante PIN a cui assegno il valore 3 - LINEA DATI stripA - assegno il pin 10 su Teensy
#define PIN_B 9  //definisco una costante PIN a cui assegno il valore 9 - LINEA DATI stripB - assegno il pin 9 su Teensy
#define NumLed_A 80  //definisco una costante NumLed che mi indica quanti led ho nella striscia A - Scalini
#define NumLed_B 240  //definisco una costante NumLed che mi indica quanti led ho nella striscia B - Corsia

Adafruit_NeoPixel strip_A = Adafruit_NeoPixel(NumLed_A, PIN_A, NEO_GRB + NEO_KHZ800); //inizializzo la strip A
Adafruit_NeoPixel strip_B = Adafruit_NeoPixel(NumLed_B, PIN_B, NEO_GRB + NEO_KHZ800); //inizializzo la strip B

#define timer01 1000  //definisco il temporizzatore della scala
#define timer02 750   //definisco il timer che regola la velocità di accensione degli scalini
#define timer03 20    //definisco il timer che regola la velocità di accensione della corsia
#define timer04 25    //definisco il timer che regola la velocità di spegnimento di tutto

const unsigned int IR1=7;   //definisco la costante IR1 e la assegno al pin 7 - switch 1 rampa sala>notte
const unsigned int IR2=1;   //definisco la costante IR2 e la assegno al pin 1 - switch 2 rampa notte>sala

int IR1State = HIGH;    //definisco la variabile IR1State e le setto a HIGH - stato dello switch 1
int IR2State = HIGH;    //definisco la variabile IR2State e le setto a HIGH - stato dello switch 2



void setup() {
  
  strip_A.begin();  //inizializzo la strip A
  strip_B.begin();  //inizializzo la strip B
  strip_A.show(); // Inizializzo tutti i LED A su 'off'
  strip_B.show(); // Inizializzo tutti i LED B su 'off'
  pinMode (IR1, INPUT);  //stabilisco che il pin di IR1 è in input
  pinMode (IR2, INPUT);  //stabilisco che il pin di IR2 è in input
  
  
}

void loop() {
 IR1State = digitalRead (IR1);  //leggo lo stato di IR1
 IR2State = digitalRead (IR2);  //leggo lo stato di IR2
 
 // Rampa01();
 
 
  if (IR1State == LOW) {  //accende in salita la scala taverna>sala
    Rampa01();
  }

  if (IR2State == LOW) {  //accende in discesa la scala taverna>sala
    Rampa02();
  }

} //fine loop


/*
DEFINISCO LA FUNZIONE CHE ACCENDE E SPEGNE I LED DELLA RAMPA 1 (ZONA TAVERNA/SALA) IN UN SENSO
CREO UNA VARIABILE m COMPRESA TRA 0 E 5 (CIOE' 6 LED - O ADATTARE AL NUMERO DI LED TOTALI ) 
E FACCIO IN MODO CHE IL VALORE SALTA 1 LED PRIMA DI AGGIORNARSI (m+1). CIOE' CONSIDERA I GRUPPI DI LED 
A INTERVALLI DEFINITI DA m+1. IL VALORE DI m POI LO USO ANCHE COME INDICATORE DEL COMANDO strip
PER DEFINIRE QUALE LED PILOTARE A OGNI CICLO

*/
void Rampa01(){    //Gestisce i LED in un senso
  
  strip_A.setBrightness(255);
  strip_B.setBrightness(255);
  strip_A.show();
  strip_B.show();
  
       
      //}
    
    for (int n=0; n<240; n++) {  //questo accende i LED della corsia
      strip_B.setPixelColor (n, 255, 255, 255);
      strip_B.show();
      delay(timer03);
    }
    
    for (int m=0; m<80; m=m+1)  {
      strip_A.setPixelColor (m, 255, 255, 255);  //in questo modo accendo gli scalini
      strip_A.setPixelColor (m=m+1, 255, 255, 255);
      strip_A.setPixelColor (m=m+1, 255, 255, 255);
      strip_A.setPixelColor (m=m+1, 255, 255, 255);
      strip_A.setPixelColor (m=m+1, 255, 255, 255);
      strip_A.show();
      delay(timer02);

    }
    
    delay(timer01);
 

  for (int i=255; i>=0; i--){  //spengo tutto in fading
    strip_A.setBrightness(i);
    strip_B.setBrightness(i);
    
    strip_A.show();
    strip_B.show();
    delay(timer03);
    }

}

/*
DEFINISCO LA FUNZIONE CHE ACCENDE E SPEGNE I LED DELLA RAMPA 2 (ZONA TAVERNA/SALA) IN UN SENSO
CREO UNA VARIABILE m COMPRESA TRA 0 E 5 (CIOE' 6 LED - O ADATTARE AL NUMERO DI LED TOTALI ) 
E FACCIO IN MODO CHE IL VALORE SALTA 1 LED PRIMA DI AGGIORNARSI (m+1). CIOE' CONSIDERA I GRUPPI DI LED 
A INTERVALLI DEFINITI DA m+1. IL VALORE DI m POI LO USO ANCHE COME INDICATORE DEL COMANDO strip
PER DEFINIRE QUALE LED PILOTARE A OGNI CICLO
I LED IN QUESTIONE VANNO DA 0 A 71 (O VICEVERSA)
*/

void Rampa02(){    //Gestisce i LED in senso opposto
  
  strip_A.setBrightness(255);
  strip_B.setBrightness(255);
  strip_A.show();
  strip_B.show();
  
       
      //}
    
    for (int n=239; n>=0; n--) {  //questo accende i LED della corsia
      strip_B.setPixelColor (n, 255, 255, 255);
      strip_B.show();
      delay(timer03);
    }
    
    for (int m=79; m>=0; m=m-1)  {
      strip_A.setPixelColor (m, 255, 255, 255);  //in questo modo accendo gli scalini
      strip_A.setPixelColor (m=m-1, 255, 255, 255);
      strip_A.setPixelColor (m=m-1, 255, 255, 255);
      strip_A.setPixelColor (m=m-1, 255, 255, 255);
      strip_A.setPixelColor (m=m-1, 255, 255, 255);
      strip_A.show();
      delay(timer02);

    }
    
    delay(timer01);
 

  for (int i=255; i>=0; i--){  //spengo tutto in fading
    strip_A.setBrightness(i);
    strip_B.setBrightness(i);
    
    strip_A.show();
    strip_B.show();
    delay(timer03);
    }

}
 
I'd suggest checking out the FastLED library — the lastest pre release version 2.1 works with the Teensy.

Weel, I'll try to check this out.



What is the amp rating of your power source?

Currently I'm using a 2,2 A power source. But tomorrow or thuesday I'm waiting to get a 5V 40A power source.
Btw, the eventual lack of amp rating can make all get crazy? I mean, if the ampere rate isn't enough, I suppose maybe it doesn't work at all, not to have a crazy behaviour (actually all the 80 leds in the stripe are fully on, at full brightness). Just I espect to have them all white, and turning on sequencially, but I get all the leds on at the same time (at full brightness) and changing color continuosly, or so...



about the code? I think it's an easy code, but maybe something wrong in there?

Andrea
 
My first guess is that's the problem -- I've seen that myself with a few setups. 80 LEDs @ 50 mA = 4 Amps.

Try editing your code to use the range 0..63 for your LEDs instead of 0..255, that should take the current usage down to 1-ish Amps. If things are still weird then it's not a power issue and we can keep looking.
 
My first guess is that's the problem -- I've seen that myself with a few setups. 80 LEDs @ 50 mA = 4 Amps.

Try editing your code to use the range 0..63 for your LEDs instead of 0..255, that should take the current usage down to 1-ish Amps. If things are still weird then it's not a power issue and we can keep looking.

Ok I'll try this and let you know


Andrea
 
OK, I choosed 40 leds so to avoid problems with the power source (I should drain 2 amps, right? the power source is 2250 mA)

But the "crazyness" goes on:
when I activate the IR switch this is what happens:
all the strip lights on white full brightness; then the sequence starts with the leds #0 to #10 all switched on already, and goes on lighting 5 leds for steps till the 40 leds are on (white full brightness). After the timing, all of them start to fade off, but when on "midway" they start flickering all coloured, then all leds stay on full brightness but different color each one....I got such a rainbow....

as i've been getting crazy for a while, I tryed another way: from the beginning.....

this is the code I made quickly:

Code:
#include <Adafruit_NeoPixel.h>

#define PIN_A 10  

#define NumLed_A 40


Adafruit_NeoPixel strip_A = Adafruit_NeoPixel(NumLed_A, PIN_A, NEO_GRB + NEO_KHZ800);


#define timer01 2000  
#define timer02 250   




void setup() {
  
  strip_A.begin();  
  
  strip_A.show(); 

  
}

void loop() {
strip_A.setBrightness(255);
strip_A.show();
  
for (int m=0; m<40; m=m+1)  {
      strip_A.setPixelColor (m, 255, 255, 255);  
     
      strip_A.show();
      delay(timer02);

    }
delay(timer01);
for (int m=0; m<40; m=m+1)  {
      strip_A.setPixelColor (m, 0, 0, 0); 
     
      strip_A.show();
      delay(timer02);

    }

}


It means just to switch on sequencially the 40 leds from 0 to 39, then switch off them same way, I'm right?

Well, this is the resoult:

As I put the power, the leds start to switch on sequencially; but when they start to switch off it happens: when the led #35 switch off, the leds from #0 to #34 start randomly some color cycles....

Then the loop start again....

Tomorrow I think I'll test the FastLed library, but honestly I don't understand the behaviour....When testing Trinket using 4 groups of 5 leds, and 2 IR switches it worked all fine.... The code has been only updated concerning the real total led (meaning I didn't change any code line), and that code I put in Teensy.....

I'm really getting disappointed....

Andrea
 
Hi,

I apologize that I can't look closely at your code today, I am on my phone, but it looks like it should work.

Can you look at the interconnections between the LEDs? Is it a single strip of LEDs or do you have any bridges some where? If you try your same test with a very small number of LEDs does it work as expected?

You can also try the FastLED library instead of Adafruit_Neopixel, it should be very easy to adapt and maybe there is a timing problem with the Teensy3 and the Adafruit library (I've never used them together).
 
Hi,

I apologize that I can't look closely at your code today, I am on my phone, but it looks like it should work.

Can you look at the interconnections between the LEDs? Is it a single strip of LEDs or do you have any bridges some where? If you try your same test with a very small number of LEDs does it work as expected?

You can also try the FastLED library instead of Adafruit_Neopixel, it should be very easy to adapt and maybe there is a timing problem with the Teensy3 and the Adafruit library (I've never used them together).

Don't apologize, and don't worry, I'm happy that you support me.
About the code, I'll try as soon as I can (during week I'm little bit busy with my job) and I'll post the resoult.
By the way, I'll try using FastLed and starting from the beginning.
About my current tests, I'm using a single strip, about 150 leds setting the max led numbers via code (is the stripe remained from the stairs connection); the stairs are already connected and waiting for the pic. I just hope all the connections are fine, otherwise it will be really a mess to fix it....

Andrea
 
I'm sorry for being late, but last week has been very busy.
Btw, yesterday I tryed to start from the beginning but using FastLED library.
Nothing special, just 3 test codes:
1) switch on/off one led
2) switch on/off in a sequence 20 leds
3) switch on/off a sequence of group of 5 (basically the function I'm interesting in by now)

well, everything was fine; more I set the full led strip (160 leds) with no problem at all: 32 groups of 5 leds switching on sequencially. And I used the little power source (2.2 A) (maybe the final part of the chain a little bit less bright).
As soon as i can, i'll go on with the full code test (use of 2 IR switches and faded switch off).
The only HW difference, this time, is a small resistor on the data line (i forgot it the first time)

Anyway, at this point (i don't think the problem was the resistor - i tried to load the old code > everything got crazy again) I think the main problem is the Neopixel library

I go on with the test

Andrea
 
PS: I'm trying to get a reference of all the commands / functions of FastLED, but googling I can't find nothing.... Any suggestion about?

Thanks
Andrea
 
Maybe look at FastLED.h ?

Ops....

Well, you're right...
But I was looking for something more...."me-friendly".
When I started playing with Neopixel library I found on Adafruit website a nice reference manual with all the neopixel function listed with some code line for each function.
Well, such a "Neopixel for dummies", from wiring to coding....But honestly really a good support.

Andrea
 
Status
Not open for further replies.
Back
Top