Sound sensor working with an RGB LED strand (respond to the change in music)

Hi!
I'm new to Arduino, so please forgive me if I am posting this on the wrong forum. I want to control an RGB (NeoPixel?) strand to work with a sound sensor.

I have a code but my problem is making sure that it actually lights to the music and it is VISIBLE - not just for the computer but for the human eye as well. Could really use some help with adjusting this.

Another issue I encountered is that sometimes the lights flicker to the change in sound and sometimes not. The sensor does not recognise any change until you pat the microphone itself. The sensor also has 4 legs as it can be both AO and DO.

Any help would be much appreciated!

The code:
#include <LPD8806.h>

#include <Adafruit_NeoPixel.h>

#define PIN 6
int threshold = 490; //Change This
int volume;

// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
strip.begin();
strip.show(); // Initialize all pixels to 'off'

Serial.begin(9600); // For debugging
}

void loop() {
volume = analogRead(A0); // Reads the value from the Analog PIN A0
/*
//Debug mode
Serial.println(volume);
delay(100);
*/
// Serial.println(volume);
if(volume>=threshold) {
// Change all the pixel with this nice for loop
for (int i = 0; i < 60; i++) {
strip.setPixelColor(i, 255, 0, 255);
}
// Change the brightness
//strip.setBrightness(10);
// Send out all the calculations to the LEDs
strip.show();
delay (1);
}
else {
for (int i = 0; i < 60; i++)
// Turn off all the LEDs here
strip.setPixelColor( i, 0, 0 , 255);
strip.show();
delay (1);

}
}

The mic needs av amplifier to be connected to the analog output.
The amp can be a rail-to-rail opamp (or other amplifier..)

I changed your code so its easier to se the effect og the mic..
(try tapping it while this runs)

#include <LPD8806.h>
#include <Adafruit_NeoPixel.h>
#define PIN 6
byte volume; // !!! int in final version

Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);

void setup() 
{
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop()
{
  for (int i = 0; i < 60; i++)
  {
    volume = analogRead(A0); // Reads the value from the Analog PIN A0
    strip.setPixelColor(i, volume, 0, 0);
  }
  strip.show();
  delay (100);
}

Hey!

Thanks for your reply. Indeed, the change in the code makes it easier to see the response however, when I tried changing the colour from red to blue (or any other) it can no longer upload to board. Saying:

</>
"avrdude: ser_open(): can't open device "/dev/cu.usbmodem1411": No such file or directory
ioctl("TIOCMGET"): Inappropriate ioctl for device
Problem uploading to board. See http://www.arduino.cc/en/Guide/Troubleshooting#upload for suggestions.
</>

I was changing the colour in the line:

</> strip.setPixelColor(i, volume, 0, 0); </>

from the first 0 to 150 or other. Should I change something else?

Thanks!

It compiles fine with all variants:
strip.setPixelColor(i, volume, 0,0);
strip.setPixelColor(i, 0,volume, 0);
strip.setPixelColor(i, 0,0,volume);

It uploads nicely onto my Duemilanove.

Thank you for your help! I managed to make it work! :slight_smile:

Hello,
I run the corrected code above by knut_ny
it works good.

I wanted to do modification on code, which i am not successfull. :frowning:

i want out to be in following:

  1. when no music all leds off. Same like the code but brightness is problem when sound from computer is connected. Brightness is low since computer values read from 20-50 around only on high volume, and 4-20 on low volume.
  2. when the sound is there from the computer(analog input to arduino), i want to make the leds to have some patterns depending on beats. Problem while doing this is when i checked the serial print value, there is difference when sound is low and sound is high. So coding wise i cannot tell code for such levels do certain pattern.
    Pattern i want is fast changing leds, means, suppose in one pattern first 10 leds and some 20 leds in middle are on and they immidiately switch to last 10 leds and others not used.

Can someone help me in getting me atleast one pattern and i can modify. Thanks