Skip to content

Instantly share code, notes, and snippets.

@jsonpoindexter
Created May 13, 2014 03:41
Show Gist options
  • Save jsonpoindexter/ff4f42f953cdfd2d312f to your computer and use it in GitHub Desktop.
Save jsonpoindexter/ff4f42f953cdfd2d312f to your computer and use it in GitHub Desktop.
#define WAIT_FOR_KEYBOARD 1 // Use keyboard to pause/resume program.
/* Debug parameters: */
#define PRINT_LOOP_TIME 1
////////LED IMPORTS////////
#include "LPD8806.h"
#include "SPI.h"
/* LED parameters: */
// Number of RGB LEDs in strand:
#define LED_COUNT 120
// Chose 2 pins for output; can be any valid output pins:
//#define DATA_PIN 12
//#define CLOCK_PIN 6
// First parameter is the number of LEDs in the strand. The LED strips
// are 32 LEDs per meter but you can extend or cut the strip. Next two
// parameters are SPI data and clock pins:
//LPD8806 strip = LPD8806(LED_COUNT, DATA_PIN, CLOCK_PIN);
// You can optionally use hardware SPI for faster writes, just leave out
// the data and clock pin parameters. But this does limit use to very
// specific pins on the Arduino. For "classic" Arduinos (Uno, Duemilanove,
// etc.), data = pin 11, clock = pin 13. For Arduino Mega, data = pin 51,
// clock = pin 52. For 32u4 Breakout Board+ and Teensy, data = pin B2,
// clock = pin B1. For Leonardo, this can ONLY be done on the ICSP pins.
LPD8806 strip = LPD8806(LED_COUNT);
/* MATRIX parameters: */
#define MatrixHieght 10
#define MatrixWidth 12
uint8_t LED_Matrix[MatrixWidth][MatrixHieght] = {
{11, 12, 35, 36, 59, 60, 83, 84, 107, 108},
{10, 13, 34, 37, 58, 61, 82, 85, 106, 109},
{ 9, 14, 33, 38, 57, 62, 81, 86, 105, 110},
{ 8, 15, 32, 39, 56, 63, 80, 87, 104, 111},
{ 7, 16, 31, 40, 55, 64, 79, 88, 103, 112},
{ 6, 17, 30, 41, 54, 65, 78, 89, 102, 113},
{ 5, 18, 29, 42, 53, 66, 77, 90, 101, 114},
{ 4, 19, 28, 43, 52, 67, 76, 91, 100, 115},
{ 3, 20, 27, 44, 51, 68, 75, 92, 99, 116},
{ 2, 21, 26, 45, 50, 69, 74, 93, 98, 117},
{ 1, 22, 25, 46, 49, 70, 73, 94, 97, 118},
{ 0, 23, 24, 47, 48, 71, 72, 95, 96, 119}
};
float brightness = 1; //brightness 0.0 to 1.0.
uint8_t xcord;
uint8_t ycord;
uint8_t pixel;
//////MSGEQ7 Equalizer /////
int analogPin = 0; // read from multiplexer using analog input 0
int strobePin = 2; // strobe is attached to digital pin 2
int resetPin = 3; // reset is attached to digital pin 3
int spectrumValue[7]; // to hold a2d values
void setup() {
if (WAIT_FOR_KEYBOARD) {
Serial.begin(9600);
// Wait for serial to initalize.
while (!Serial) { }
Serial.println("Strike any key to start...");
// Wait for the next keystroke.
while (!Serial.available()) { }
// Clear the serial buffer.
Serial.read();
}
MSGEQ7init(); // MSGEQ7 Equalizer initilization
// Start the receiver
// Start up the LED strip
strip.begin();
//Set all pixel to off
clearPixels();
// Update the strip, to start they are all 'off'
strip.show();
delay(00);
Serial.print("Let's go! \n");
}
int eqValue = 0; // var that will hold analog data from the EQ
void loop() {
// loopDebug();
// Serial.print("******BEGIN******\n");
// drawSquare( 2, 2, 0, 8, (random( 0, 384))); //drawSquare(width, length, start at x-cordinate, start at y-cordinate, color (between 0 -384))
// drawSquareRandom( 2, random( 0, 384)); //(dimension of squares, color) - makes squares appear in random locations not overlapping
eqValue = MSGEQ7();
if (eqValue > 1000) {
squareSequence(2, 0, 0); //(squareDimension, color between 0-384, delay in ms)
}
else
{
return;
}
}
void squareSequence(uint8_t squareDim, uint8_t setcolor, int timedelay ) {
Serial.print("Entered squareSequence \n");
uint8_t xcord = 0;
uint8_t ycord = 0;
for (int j = 0; j < MatrixHieght; j = j + squareDim) {
ycord = j;
for (int i = 0; i < MatrixWidth; i = i + squareDim)
{
xcord = i;
drawSquare(squareDim, xcord, ycord, random( 0, 384));
delay(timedelay);
}
}
}
//Fills in the full LED_Matrix with color
void drawSquare(uint8_t squareDim, uint8_t xcord, uint8_t ycord, uint32_t setcolor) {
Serial.print ("Entered drawSquare() \n");
for (int i = ycord; i < squareDim + ycord; i++) {
for (int j = xcord; j < squareDim + xcord; j++) {
Serial.print("( ");
Serial.print(i);
Serial.print(" , ");
Serial.print(j);
Serial.print(" ) \n");
uint8_t pixel = LED_Matrix[j][i];
Serial.print("Set Pixel: ");
Serial.print(pixel);
Serial.println("");
// boolean pixelsUsed = trackPixels(pixel);
// if (!pixelsUsed) {
strip.setPixelColor(pixel, color(setcolor, brightness));
// }
}
}
Serial.print("strip.show() \n");
strip.show();
}
//draws randoms square on the matrix that do not overlap
void drawSquareRandom(uint8_t squareDim, int32_t setcolor) {
// Serial.print ("Entered drawSquareRanndom() \n");
uint8_t ycordMax;
uint8_t xcordMax;
// Serial.print("Square Demensions: ");
// Serial.print(squarewidth);
// Serial.print("x");
// Serial.print(squarehieght);
// Serial.println("");
ycordMax = (MatrixHieght / squareDim);
// Serial.print("ycordMax = ");
// Serial.print(ycordMax);
// Serial.println("");
xcordMax = (MatrixWidth / squareDim);
// Serial.print("xcordMax = ");
// Serial.print(xcordMax);
// Serial.println("");
ycord = ((random( 0, ycordMax)) * squareDim); //creates random ycordinates that are spaced 'squareHieght' spaces apart so the square does not overlap;
xcord = ((random( 0, xcordMax)) * squareDim);
// Serial.print("(xcord, ycord) \n");
// Serial.print("( ");
// Serial.print(xcord);
// Serial.print(" , ");
// Serial.print(ycord);
// Serial.print(" ) \n");
drawSquare(squareDim, xcord, ycord, setcolor);
}
////////////////////////will check to see if a incoming pixel has aleady been used. If it has then it will exit the function. If not then it will add the pixel to the usedPixels array and continue////////
int usedPixels[MatrixWidth * MatrixHieght];
int dataCounter = 0;
boolean usedPixel = false;
boolean trackPixels(uint8_t pixel) {
Serial.print("Entered trackCordinates \n");
usedPixel = false;
dataCounter = dataCounter;
for(int i = 0; i <= dataCounter; i++){
if (usedPixels[i] == pixel) {
Serial.print("incoming pixel (");
Serial.print(pixel);
Serial.print(") has been used at trackPixels[");
Serial.print(i);
Serial.print("] \n");
usedPixel = true;
break;
}
}
if (usedPixel == false) {
Serial.print("usedPixel == false \n");
Serial.print("dataCounter = ");
Serial.print(dataCounter);
Serial.println("");
usedPixels[dataCounter] = pixel;
Serial.print("usedPixels[");
Serial.print(dataCounter);
Serial.print("] = ");
Serial.print(pixel);
Serial.println("");
dataCounter++;
if (dataCounter == MatrixHieght * MatrixWidth) {
dataCounter = 0;
}
}
return usedPixel;
}
//Inputa value 0 to 384 to get a color value.
//The colours are a transition r - g -b - back to r
// Color 1 from 384; brightness 0.0 to 1.0.
// Color 1 from 384; brightness 0.0 to 1.0.
uint32_t color(uint16_t color, float brightness) {
byte r, g, b;
int range = color / 128;
switch (range) {
case 0: // Red to Yellow (1 to 128)
r = 127 - color % 128;
g = color % 128;
b = 0;
break;
case 1: // Yellow to Teal (129 to 256)
r = 0;
g = 127 - color % 128;
b = color % 128;
break;
case 2: // Teal to Purple (257 to 384)
r = color % 128;
g = 0;
b = 127 - color % 128;
break;
}
r *= brightness;
g *= brightness;
b *= brightness;
return strip.Color(r, g, b);
}
void clearPixels () {
int i;
for (i=0; i<LED_COUNT; i++) {
strip.setPixelColor(i, 0, 0, 0);
}
}
// Debug functions controlled by run/debug parameters.
unsigned long before = 0;
void loopDebug() {
if (WAIT_FOR_KEYBOARD) {
pauseOnKeystroke();
}
if (PRINT_LOOP_TIME) {
unsigned long now = millis();
Serial.println(now - before);
before = millis();
}
}
void pauseOnKeystroke() {
if (Serial.available()) {
// Clear the serial buffer.
Serial.read();
Serial.println("Paused. Strike any key to resume...");
// Wait for the next keystroke.
while (!Serial.available()) { }
// Clear the serial buffer.
Serial.read();
}
}
///Audio Equilzer functions////
void MSGEQ7init() {
pinMode(analogPin, INPUT);
pinMode(strobePin, OUTPUT);
pinMode(resetPin, OUTPUT);
analogReference(DEFAULT);
digitalWrite(resetPin, LOW);
digitalWrite(strobePin, HIGH);
}
/// function that returns
int eqChan = 2; //define how many channels you want to listen to 1-7 (63Hz, 160Hz, 400Hz, 1kHz, 2.5kHz, 6.25kHz, and 16kHz)
int MSGEQ7() {
digitalWrite(resetPin, HIGH);
digitalWrite(resetPin, LOW);
for (int i = 0; i < eqChan; i++) {
digitalWrite(strobePin, LOW);
delayMicroseconds(30); // to allow the output to settle
spectrumValue[i] = analogRead(analogPin);
// comment out/remove the serial stuff to go faster
// - its just here for show
// if (spectrumValue[i] < 10)
// {
// Serial.print(" ");
// Serial.print(spectrumValue[i]);
// }
// else if (spectrumValue[i] < 100 )
// {
// Serial.print(" ");
// Serial.print(spectrumValue[i]);
// }
// else
// {
// Serial.print(" ");
// Serial.print(spectrumValue[i]);
// }
//
digitalWrite(strobePin, HIGH);
}
int eqReading = spectrumValue[1];
return eqReading;
// Serial.println();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment