Go Down

Topic: leds ws2801 flashs problem (Read 406 times) previous topic - next topic

kurto

hi,

Sorry i'dont' know if i'm in the good section.

With these codes, the LEDs work well at the beginning but after about 1minutes, the LED flashes as if arduino could no longer keep the rythm , i use these code for vizualizer audio, is there a problem in the coding? why my leds flashs ? arduino not powerful enough?
please, help me i search since many days but i find no problem as mine on the forum.i use an arduino uno , 4*25 leds ws2801 , on windows vista.


code arduino.
[
//SPI Library
#include <SPI.h>

//RGB LED Library
#include <Adafruit_WS2801.h>

//Light Control Pins
const int light_data = 11;
const int light_clk = 13;

//Set Strip Constants
const int length = 100;

uint32_t Couleur[25];

const int NbrPlageAmp=25;

const byte RougeBasseClaire=0;
const byte VertBasseClaire=0;
const byte BleuBasseClaire=80;

const byte RougeBasseFonce=0;
const byte VertBasseFonce=23;
const byte BleuBasseFonce=234;           
 
float CoeffRouge;
float CoeffVert;
float CoeffBleu;

float MinRouge;
float MinVert;
float MinBleu;




// Create a 24 bit color value from R,G,B
uint32_t Color(byte r, byte g, byte b)
{
  uint32_t c;
  c = r;
  c <<= 8;
  c |= g;
  c <<= 8;
  c |= b;
  return c;
}


//Library Setup
Adafruit_WS2801 strip = Adafruit_WS2801(length, light_data, light_clk);
int array[2] =    {0,0};

void setup ()
{
   //vert
  Couleur[0]=Color(0,255,0);
  Couleur[1]=Color(0,255,0);
  Couleur[2]=Color(0,255,0);
  Couleur[3]=Color(0,255,0);
  Couleur[4]=Color(0,255,0);
  Couleur[5]=Color(0,255,0);
  Couleur[6]=Color(0,255,0);
  Couleur[7]=Color(0,255,0);
  Couleur[8]=Color(0,255,0);
  Couleur[9]=Color(0,255,0);
  Couleur[10]=Color(0,255,0);
  Couleur[11]=Color(0,255,0);
  Couleur[12]=Color(0,255,0);
  // jaune
  Couleur[13]=Color(255,255,0);
  Couleur[14]=Color(255,230,0);
  Couleur[15]=Color(255,200,0);
  Couleur[16]=Color(255,175,0);
  Couleur[17]=Color(255,150,0);
  //orange
  Couleur[18]=Color(255,128,0) ;
  Couleur[19]=Color(255,96,0);
  Couleur[20]=Color(255,64,0);
  Couleur[21]=Color(255,32,0);
  //rouge
  Couleur[22]=Color(255,0,0) ;
  Couleur[23]=Color(150,0,0);
  Couleur[24]=Color(80,0,0);
 

  CoeffRouge=(RougeBasseFonce-RougeBasseClaire)/NbrPlageAmp;
  CoeffVert=(VertBasseFonce-VertBasseClaire)/NbrPlageAmp;
  CoeffBleu=(BleuBasseFonce-BleuBasseClaire)/NbrPlageAmp;
 

 
  if (RougeBasseFonce<RougeBasseClaire){MinRouge=RougeBasseFonce;}else {MinRouge=RougeBasseClaire;}
  if (VertBasseFonce<VertBasseClaire){MinVert=VertBasseFonce;}else {MinVert=VertBasseClaire;}
  if (BleuBasseFonce<BleuBasseClaire){MinBleu=BleuBasseFonce;}else {MinBleu=BleuBasseClaire;}
 
  Serial.begin(9600);
  strip.begin();


  // Update LED contents, to start they are all 'off'
  strip.show();
}


void loop()
{   
     int blue;
     int green;
     int red;
     int i;
     int j;
     int k;
     int l;
     
     uint32_t CouleurAigu;
     
    if(Serial.read() == 0xff){
     for(k=0; k<2; k++){
      array[k] = Serial.read();
      Serial.println (array[k]);
     }
   
    }
    for(j=0; j<2; j++){
       if (j==0) {
         //Basse
         
            blue=BleuBasseClaire+(CoeffBleu*array[j]);
            green=VertBasseClaire+(CoeffVert*array[j]);
            red=RougeBasseClaire+(CoeffRouge*array[j]);
             
           for ( i = 25; i < 75; ++i)
            {
              //set pixel color
             
              strip.setPixelColor(i,0,Color(red,green,blue));
             
            }
             
         
       }else{
         
               for ( l = 0; l < 25; ++l)
              {
                  if (array[j]>l){
                       CouleurAigu=Couleur[l];
                  }else{
                        CouleurAigu=0;
                  }
                   //set pixel color
                  strip.setPixelColor(l,0,CouleurAigu);
                  strip.setPixelColor(length-l-1,0,CouleurAigu);
              }
                   
           
          }
     }
 
strip.show();   
   
}

void stop()
{

}]


processing code :

[
import ddf.minim.analysis.*;
import ddf.minim.*;
import processing.serial.*; //library for serial communication

Serial port; //creates object "port" of serial class

Minim minim;
AudioInput in;
FFT fft;
float[] peaks;
BeatDetect beat;
BeatListener bl;
int peak_hold_time = 1;  // how long before peak decays
int[] peak_age;  // tracks how long peak has been stable, before decaying

// how wide each 'peak' band is, in fft bins
int binsperband = 5;
int peaksize; // how many individual peak bands we have (dep. binsperband)
float gain = 40; // in dB
float dB_scale = 2.0;  // pixels per dB

int buffer_size = 512;  // also sets FFT size (frequency resolution)
float sample_rate = 44100;

float snareSize;

int spectrum_height = 176; // determines range of dB shown

int[] freq_array = {0,0};
int i,g;
float f;


float[] freq_height = {0,0};  //avg amplitude of each freq band


class BeatListener implements AudioListener
{
  private BeatDetect beat;
  private AudioInput in;
 
  BeatListener(BeatDetect beat, AudioInput in)
  {
    this.in = in;
    this.in.addListener(this);
    this.beat = beat;
  }
 
  void samples(float[] samps)
  {
    beat.detect(in.mix);
  }
 
  void samples(float[] sampsL, float[] sampsR)
  {
    beat.detect(in.mix);
  }
}
void setup()
{
  size(200, 200);

  minim = new Minim(this);
  port = new Serial(this, "COM2"); //set baud rate
  in = minim.getLineIn(Minim.MONO,buffer_size,sample_rate);

  // create an FFT object that has a time-domain buffer
  // the same size as line-in's sample buffer
  fft = new FFT(in.bufferSize(), in.sampleRate());
 

beat = new BeatDetect(in.bufferSize(), in.sampleRate());
  beat.setSensitivity(10);
  snareSize=1;
bl = new BeatListener(beat, in );
 
  // Tapered window important for log-domain display
  fft.window(FFT.HAMMING);

  // initialize peak-hold structures
  peaksize = 1+Math.round(fft.specSize()/binsperband);
  peaks = new float[peaksize];
  peak_age = new int[peaksize];
}


void draw()
{
for(int k=0; k<2; k++){
freq_array[k] = 0;
}
   if ( beat.isSnare() )
  { snareSize=25; }
   
   { snareSize= constrain(snareSize * 0.98, 0, 25);
println(snareSize);
}
 

  // perform a forward FFT on the samples in input buffer
  fft.forward(in.mix);
 
// Frequency Band Ranges  0 - 5600     
freq_height[0] = fft.calcAvg((float) 0, (float)50 );
freq_height[1] = round(snareSize);


// Amplitude Ranges  if else tree
  for(int j=0; j<2; j++){   
    if (freq_height[j] < 200000 && freq_height[j] > 15){freq_array[j] = 24;}
    else{ if (freq_height[j] <= 15 && freq_height[j] > 14.5){freq_array[j] = 23;}
    else{ if (freq_height[j] <= 14.5 && freq_height[j] > 13.8){freq_array[j] = 22;}
    else{ if (freq_height[j] <= 13.8 && freq_height[j] > 13.2){freq_array[j] = 21;}
    else{ if (freq_height[j] <= 13.2 && freq_height[j] > 12.6){freq_array[j] = 20;}
    else{ if (freq_height[j] <= 12.6 && freq_height[j] > 12){freq_array[j] = 19;}
    else{ if (freq_height[j] <= 12 && freq_height[j] > 11.4){freq_array[j] = 18;}
    else{ if (freq_height[j] <= 11.4 && freq_height[j] > 10.8){freq_array[j] = 17;}
    else{ if (freq_height[j] <= 10.8 && freq_height[j] > 10.2){freq_array[j] = 16;}
    else{ if (freq_height[j] <= 10.2 && freq_height[j] > 9.6){freq_array[j] = 15;}
    else{ if (freq_height[j] <= 9.6 && freq_height[j] > 9){freq_array[j] = 14;}
    else{ if (freq_height[j] <= 9 && freq_height[j] > 8.4){freq_array[j] = 13;}
    else{ if (freq_height[j] <= 8.4 && freq_height[j] > 7.8){freq_array[j] = 12;}
    else{ if (freq_height[j] <= 7.8 && freq_height[j] > 7.2){freq_array[j] = 11;}
    else{ if (freq_height[j] <= 7.2 && freq_height[j] > 6.6){freq_array[j] = 10;}
    else{ if (freq_height[j] <= 6.6 && freq_height[j] > 6){freq_array[j] = 9;}
    else{ if (freq_height[j] <= 6 && freq_height[j] > 5.4){freq_array[j] = 8;}
    else{ if (freq_height[j] <= 5.4 && freq_height[j] > 4.8){freq_array[j] = 7;}
    else{ if (freq_height[j] <= 4.8 && freq_height[j] > 4.2){freq_array[j] = 6;}
    else{ if (freq_height[j] <= 4.2 && freq_height[j] > 3.6){freq_array[j] = 5;}
    else{ if (freq_height[j] <= 3.6 && freq_height[j] > 3){freq_array[j] = 4;}
    else{ if (freq_height[j] <= 3 && freq_height[j] > 2.4){freq_array[j] = 3;}
    else{ if (freq_height[j] <= 2.4 && freq_height[j] > 1.8){freq_array[j] = 2;}
    else{ if (freq_height[j] <= 1.8 && freq_height[j] >= 1.2 ){freq_array[j] = 1;}
    else{ if (freq_height[j] < 0.6 ){freq_array[j] = 0;}
  }}}}}}}}}}}}}}}}}}}}}}}}}

  //send to serial
  port.write(0xff); //write marker (0xff) for synchronization

 
  for(i=0; i<2; i++){
 
   
    port.write((byte)(freq_array));
  }
  //delay(2); //delay for safety
}


void stop()
{
  // always close Minim audio classes when you finish with them
  in.close();
  minim.stop();

  super.stop();
}
]

Go Up