Skip to content

Instantly share code, notes, and snippets.

@MichMich
Last active April 20, 2018 02:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MichMich/9854519 to your computer and use it in GitHub Desktop.
Save MichMich/9854519 to your computer and use it in GitHub Desktop.
A small example for using the Sparkfun Spectrum Shield together with two Adafruit Neopixel Strips.
#include <Adafruit_NeoPixel.h>
#define PIN_NEOPIXELS 7
#define PIN_STROBE 4
#define PIN_RESET 5
#define PIN_LEFT 0 //analog
#define PIN_RIGHT 1 //analog
#define NUMLEDSPERSEGMENT 8
#define NUMLEDS NUMLEDSPERSEGMENT * 2
#define MINPOWER 128
#define MAXPOWER 1023
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMLEDS, PIN_NEOPIXELS, NEO_GRB + NEO_KHZ800);
//band arrays
int left[7];
int right[7];
void setup() {
//initialize neopixels
strip.begin();
strip.setBrightness(32); //otherwise I'll become blind during testing ... ;)
strip.show();
//initialize eq
pinMode(PIN_RESET, OUTPUT); // reset
pinMode(PIN_STROBE, OUTPUT); // strobe
digitalWrite(PIN_RESET,LOW); // reset low
digitalWrite(PIN_STROBE,HIGH); //pin 5 is RESET on the shield
}
void loop() {
readMSGEQ7();
int rLeft = colorValue((left[0]+left[1])/2);
int gLeft = colorValue((left[2]/2+left[3]+left[4]/2)/2);
int bLeft = colorValue((left[5]+left[6])/2);
int rRight = colorValue((right[0]+right[1])/2);
int gRight = colorValue((right[2]/2+right[3]+right[4]/2)/2);
int bRight = colorValue((right[5]+right[6])/2);
for (int i = 0; i < NUMLEDSPERSEGMENT; i++) {
strip.setPixelColor(i, (i<=rLeft) ? 255 : 0,(i<=gLeft) ? 255 : 0,(i<=bLeft) ? 255 : 0);
strip.setPixelColor(i + NUMLEDSPERSEGMENT, (i<=rRight) ? 255 : 0,(i<=gRight) ? 255 : 0,(i<=bRight) ? 255 : 0);
}
strip.show();
delay(40);
}
int colorValue(int powerValue)
{
return map(powerValue, MINPOWER, MAXPOWER, 0, NUMLEDSPERSEGMENT);
}
void readMSGEQ7()
{
//reset the data
digitalWrite(PIN_RESET, HIGH);
digitalWrite(PIN_RESET, LOW);
//loop thru all 7 bands
for(int band=0; band < 7; band++) {
digitalWrite(PIN_STROBE,LOW); // go to the next band
delayMicroseconds(50); //gather some data
left[band] = analogRead(PIN_LEFT); // store left band reading
right[band] = analogRead(PIN_RIGHT); // store right band reading
digitalWrite(PIN_STROBE,HIGH); // reset the strobe pin
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment