Help? RGB LED to react to sound (VU meter)

Hello, I'm new to the Arduino / Programming code world and was wondering if anyone could please help with any of this project? (I've researched and tried out a few things but seem to keep getting stuck).

The idea is to make the blue led levels slowly fade in and out (around 4 seconds or so) at different times. I think achievable using fading?

The red led levels would react to music/sound read by the piezoelectric buzzer, as in a VU meter.

The Green pin is not being used.

Project pictured here; img34.imageshack.us/img34/9031/ledproject1.jpg

I've gone through todbot.com/blog/bionicarduino/ and also arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1238104805 (I tried these codes and this worked to an extent but I couldn't get very reactive light flashes/ buzzer not sensitive enough?).

The code is confusing me a bit, any help appreciated! Thanks.

Ok so I got a bit further but am having trouble combing the 3 codes, any help?

To fade an LED

int value = 0;                            // variable to keep the actual value 
int ledpin = 9;                           // light connected to digital pin 9

void setup() 
{ 
  // nothing for setup 
} 

void loop() 
{ 
  for(value = 0 ; value <= 255; value+=5) // fade in (from min to max) 
  { 
    analogWrite(ledpin, value);           // sets the value (range from 0 to 255) 
    delay(30);                            // waits for 30 milli seconds to see the dimming effect 
  } 
  for(value = 255; value >=0; value-=5)   // fade out (from max to min) 
  { 
    analogWrite(ledpin, value); 
    delay(30); 
  }  
}

To cycle LED in an array

int timer = 100;                   // The higher the number, the slower the timing.
int pins[] = { 2, 3, 4, 5, 6, 7 }; // an array of pin numbers
int num_pins = 6;                  // the number of pins (i.e. the length of the array)

void setup()
{
  int i;

  for (i = 0; i < num_pins; i++)   // the array elements are numbered from 0 to num_pins - 1
    pinMode(pins[i], OUTPUT);      // set each pin as an output
}

void loop()
{
  int i;

  for (i = 0; i < num_pins; i++) { // loop through each pin...
    digitalWrite(pins[i], HIGH);   // turning it on,
    delay(timer);                  // pausing,
    digitalWrite(pins[i], LOW);    // and turning it off.
  }
  for (i = num_pins - 1; i >= 0; i--) { 
    digitalWrite(pins[i], HIGH);
    delay(timer);
    digitalWrite(pins[i], LOW);
  }
}

To control the VU meter (as a seperate function)

byte voltageReferencePin = 0;
byte voltagePin = 2;


byte ledPins[6] = { 8 , 9 , 10 , 11 , 12 , 13 };
byte voltageComparisonThresholds[] = { 0 , 10 , 30 , 55 , 80 , 100 };

int voltageReference = 0; //contain analogRead(voltageReferencePin);

int samples[NUMBER_OF_SAMPLES] = {0};
int sample = 0;
int sampleTotal = 0;
byte sampleIndex  = 0;


void setup() {
 for (byte i=0; i<NUMBER_OF_LEDS; i++){
   pinMode(ledPins[i],OUTPUT);
 }
 pinMode(voltageReferencePin,INPUT);
 pinMode(voltagePin,INPUT);
 
 if(DEBUG){Serial.begin(9600);}
}

void loop(){
 sampleTotal -= samples[sampleIndex]; 
 samples[sampleIndex] = analogRead(voltageReferencePin);//read value
 sampleTotal += samples[sampleIndex++];  
 if (sampleIndex >= NUMBER_OF_SAMPLES) {sampleIndex = 0;}
 sample = sampleTotal / NUMBER_OF_SAMPLES; 

 if(DEBUG){Serial.print("virtual vu: ");}
 

 for (byte i=0; i<NUMBER_OF_LEDS; i++){
   if ( analogRead(voltagePin) >= voltageReference + voltageComparisonThresholds[i] ) {
     digitalWrite(ledPins[i],HIGH); if(DEBUG){Serial.print("|");}
   }else{
     digitalWrite(ledPins[i],LOW);  if(DEBUG){Serial.print(" ");} 
   }
   delay(VU_METER_DISPLAY_DELAY);
 }

 if(DEBUG){ Serial.println(" "); }
 
}

Don't use the delay() function as this stopes anything else from happening.

Re write your three loop() functions and call them something else. Remove the delays from them and replace them with a test of a variable (a ling int) to millis(). The variable holds the time in millis() when the next thing is to happen. Like this nextTime = millis() + delay_time;

Do this for every where you have a delay(), see how you get on with that idea. Also look up "state machine" that is where a variable is used to determine what the next thing to happen is when nextTime comes around.

thanks for your help, I'll keep trying to reconfig the code.

What input are you using? Make sure there is enough signal to do what you want!

I made this real-time VU display a while back using an electret mic as input.

http://www.youtube.com/watch?v=ZoocFDhX0Cw&feature=channel_page

Is this close to what you have in mind?

Hey that looks pretty sweet, Its kinda along the same lines as what i'm trying to do. The input seems to be working ok, I'm trying to make it a little more reactive. Just getting used to the language of code and trying to make a couple of things happen at once, working on changing the delay feature to time.

Here is the process of what i'm trying to make if that clears things up a bit...

In terms of the code and getting both to happen, I'm also trying to use do in while loop or if in while loop so if anyone has any examples that be good.

At the moment I’m trying to get this code to work… any suggestions or help with this one?

int L3= 3;
int L5= 5;
int L6= 6;
int L9= 9;
int L10 = 10;
int L11 = 11;

int Phase1 = { 5, 9, 11 }
int Phase2 = { 3, 6, 10 }
int Phase3 = { 3, 5 }
int Phase4 = { 5, 6 }
int Phase5 = { 9, 11 }  
  
pinMode (L3, OUTPUT)
pinMode (L5, OUTPUT)
pinMode (L6, OUTPUT}
pinMode (L9, OUTPUT)
pinMode (L10, OUTPUT)
pinMode (L11, OUTPUT}

void setup() 
{ 

} 

void loop() 
{ 
  for(value = 0 ; value <= 255; value+=5)  
  { 
    analogWrite(Phase1, value);           
    delay(1000);                           
  } 
  for(value = 255; value >=0; value-=5)   
  { 
    analogWrite(Phase1, value); 
    delay(1000); 
  }  
   for(value = 0 ; value <= 255; value+=5)  
  { 
    analogWrite(Phase2, value);           
    delay(100);                           
  } 
  for(value = 255; value >=0; value-=5)   
  { 
    analogWrite(Phase2, value); 
    delay(1000); 
    for(value = 0 ; value <= 255; value+=5)  
  { 
    analogWrite(Phase3, value);           
    delay(1000);                           
  } 
  for(value = 255; value >=0; value-=5)   
  { 
    analogWrite(Phase3, value); 
    delay(1000); 
  }  
   for(value = 0 ; value <= 255; value+=5)  
  { 
    analogWrite(Phase4, value);           
    delay(1000);                           
  } 
  for(value = 255; value >=0; value-=5)   
  { 
    analogWrite(Phase4, value); 
    delay(1000); 
    }  
   for(value = 0 ; value <= 255; value+=5)  
  { 
    analogWrite(Phase5, value);           
    delay(1000);                           
  } 
  for(value = 255; value >=0; value-=5)   
  { 
    analogWrite(Phase5, value); 
    delay(1000); 
}
int Phase1 = { 5, 9, 11 }
int Phase2 = { 3, 6, 10 }
int Phase3 = { 3, 5 }
int Phase4 = { 5, 6 }
int Phase5 = { 9, 11 }

Not sure what you're trying to do here - you're declaring a scalar, and trying to initialise it as a vector.

int Phase1 []= { 5, 9, 11 }
int Phase2 []= { 3, 6, 10 }
int Phase3 []= { 3, 5 }
int Phase4 []= { 5, 6 }
int Phase5 []= { 9, 11 }

But then you're using "Phasex" as a scalar.

pinMode (L3, OUTPUT)
pinMode (L5, OUTPUT)
pinMode (L6, OUTPUT}
pinMode (L9, OUTPUT)

These should be inside "setup ()".

Thanks for your help, I was trying to initally make a knight rider kit version that had a slower blend. In the last code I am trying to make a few different light patterns come on one after the other, So there were a few things going wrong.

The phase sections were aimed to be the different sequences ie. LED 5, 9 and 11 come on in a fade then it moves to the next phase. I've set this up wrong so will try to change it somehow.

I have been looking for a circuit to filter and divide the sound signal into three frequency ranges for the Arduino analog inputs. That would make for interesting RGB LED displays since all the magic would happen in the programming.

I have been looking through some vintage Popular Electronics Magazines. I remember seeing a very simple passive filter circuit schematic as a featured project.

Any ideas?