Hello,
I'm working on a project to get light to react to music. A max sketch I found monitors the outgoing sound on my pc and sends data about it (low, mid and high frequencies) to my arduino over serial connection. The Arduino is programmed to receive this data and turn (it doesn't only fully turn the leds on or off, but it uses pwm to adjust the brightness.) on 6 leds. (one for low, mid and high frequencies in stereo). This is the code the Arduino uses:
///////////////////////////////////////////////////////////////////////////////////////////
// Stereo Color Organ (Receiver)
// Collin Cunningham - Makezine.com
// http://blog.makezine.com/archive/2010/11/powerleds
//
// - For controlling six LEDs via a serial connection
//
// - Listens for a six ASCII value message sent over serial,
// and parses those messages with the Arduino Messenger library:
// http://www.arduino.cc/playground/Code/Messenger
// ... then uses the values to set pins 3, 5, 6, 9, 10, 11 accordingly.
//
// - Each message is composed of 6 ASCII values ranging from 0-255,
// values are seperated by a single space and the entire message
// completed with a carriage return.
// Example: 135 20 234 189 55 255 (carriage return == ASCII code 13)
//
// - Any value over 255 is redefined as a random value between 0-255
//
// - Values are inverted for use with 2 of SparkFun's ZXLD1360 LED Driver boards
// Inversion can be disabled by removing the second line of the processValue function:
// else { value = map(value,0,255,255,0); }
//
///////////////////////////////////////////////////////////////////////////////////////////
//include and initialize the Messenger library for parsing ASCII messages
#include <Messenger.h>
Messenger message = Messenger();
//define our PWM output pins
int HighL = 3;
int MidL = 5;
int LowL = 6;
int HighR = 9;
int MidR = 11;
int LowR = 10;
//create variables for our output values
int highValLeft, midValLeft, lowValLeft;
int highValRight, midValRight, lowValRight;
void setup() {
//set our PWM pins as outputs
pinMode(HighL, OUTPUT);
pinMode(MidL, OUTPUT);
pinMode(LowL, OUTPUT);
pinMode(HighR, OUTPUT);
pinMode(MidR, OUTPUT);
pinMode(LowR, OUTPUT);
//set them all to Low, initially
digitalWrite(HighR, LOW);
digitalWrite(MidR, LOW);
digitalWrite(LowR, LOW);
digitalWrite(HighL, LOW);
digitalWrite(MidL, LOW);
digitalWrite(LowL, LOW);
//Begin serial connection for receiving values and tell 'em we're ready
Serial.begin(115200);
Serial.println("READY");
//associate an action for messages handled by Messenger library
message.attach(messageReady);
}
void loop() {
//when there's new serial data available,
//process it with the Messenger library
while ( Serial.available() ) { message.process( Serial.read () );}
}
void messageReady(){
//use values from serial data to define the output values
while (message.available()) {
highValLeft = message.readInt();
midValLeft = message.readInt();
lowValLeft = message.readInt();
highValRight = message.readInt();
midValRight = message.readInt();
lowValRight = message.readInt();
//write the above variables to the output pins
writeLEDs();
}
}
void writeLEDs(){
//write the LED values inverted for the left channel
analogWrite( HighL, processValue(highValLeft) );
analogWrite( MidL, processValue(midValLeft) );
analogWrite( LowL, processValue(lowValLeft) );
//write the LED values inverted for the right channel
analogWrite( HighR, processValue(highValRight) );
analogWrite( MidR, processValue(midValRight) );
analogWrite( LowR, processValue(lowValRight) );
}
int processValue(int value){
//any value over 255 is replaced with a random value between 0-255
if (value > 255) value = random(0,255);
return value;
}
This all works fine, but I really would like to use my digital ledstrip instead of 6 little leds. I have a 1 meter digital neopixel RGB ledstrip from adafruit. (Adafruit NeoPixel Digital RGB LED Strip - White 30 LED [WHITE] : ID 1376 : $84.75 : Adafruit Industries, Unique & fun DIY electronics and kits) This is where I really need some help, my programming skills just aren't good enough to program my Arduino so that it fades all the blue, red and green leds in my ledstrip on and off.
Some basic code for my ledstrip to make it do some example stuff. Maybe it helps?
#include <Adafruit_NeoPixel.h>
#define PIN 6
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_RGB Pixels are wired for RGB bitstream
// NEO_GRB Pixels are wired for GRB bitstream
// NEO_KHZ400 400 KHz bitstream (e.g. FLORA pixels)
// NEO_KHZ800 800 KHz bitstream (e.g. High Density LED strip)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);
void setup() {
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
// Some example procedures showing how to display to the pixels:
colorWipe(strip.Color(255, 0, 0), 50); // Red
colorWipe(strip.Color(0, 255, 0), 50); // Green
colorWipe(strip.Color(0, 0, 255), 50); // Blue
rainbow(20);
rainbowCycle(20);
}
// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
void rainbow(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i+j) & 255));
}
strip.show();
delay(wait);
}
}
// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
for(i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
delay(wait);
}
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
if(WheelPos < 85) {
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}
Thanks a lot in advance.