Advertisement
Guest User

WSEGQ7 / NeoMatrix Arduino

a guest
Feb 1st, 2015
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ARM 2.54 KB | None | 0 0
  1. #include <Adafruit_NeoPixel.h>
  2. #include <avr/power.h>
  3.  
  4. #define PIN 6
  5.  
  6. int analogPin = 0; // read from multiplexer using analog input 0
  7. int strobePin = 2; // strobe is attached to digital pin 2
  8. int resetPin = 3; // reset is attached to digital pin 3
  9. int spectrumValue[7]; // to hold a2d values
  10. int lightValue[5];  // resized array
  11.  
  12. Adafruit_NeoPixel strip = Adafruit_NeoPixel(64, PIN, NEO_GRB + NEO_KHZ800);
  13.  
  14. void setup()
  15. {
  16.  Serial.begin(9600);
  17.  pinMode(analogPin, INPUT);
  18.  pinMode(strobePin, OUTPUT);
  19.  pinMode(resetPin, OUTPUT);
  20.  analogReference(DEFAULT);
  21.  
  22.  digitalWrite(resetPin, LOW);
  23.  digitalWrite(strobePin, HIGH);
  24.  
  25.  Serial.println("MSGEQ7 test by J Skoba");
  26.  
  27.   strip.begin();
  28.   strip.show(); // Initialize all pixels to 'off'
  29. }
  30.  
  31. void loop()
  32. {
  33.   int ledValue, grnVal, redVal, tmpVal, intensity;
  34.   uint32_t c;
  35.   digitalWrite(resetPin, HIGH);
  36.   digitalWrite(resetPin, LOW);
  37.  
  38.   for (int i = 0; i < 7; i++){
  39.     digitalWrite(strobePin, LOW);
  40.     delayMicroseconds(30); // to allow the output to settle
  41.     spectrumValue[i] = analogRead(analogPin);
  42.      
  43.     Serial.print("\t");
  44.     Serial.print(spectrumValue[i]);
  45.  
  46.     digitalWrite(strobePin, HIGH);
  47.   } // for i
  48.   Serial.println();
  49.  
  50.   // Update LEDs
  51.   for(int i=0;i<7;i++){
  52.  
  53.     if(spectrumValue[i] > 127){
  54.       tmpVal = spectrumValue[i];
  55.     } else{
  56.       tmpVal = 0;
  57.     }
  58.  
  59.     ledValue = map(tmpVal, 0, 1023-50, 0, 255);
  60.     intensity = map(tmpVal, 0, 1023-50, 0, 7);
  61.    
  62.     c = Wheel(ledValue); //strip.Color(0,0,Wheel(ledValue) );
  63.     for(int j=0;j<8;j++){
  64.       if( j<intensity){
  65.         strip.setPixelColor(j+(8*i), c );
  66.       } else{
  67.         strip.setPixelColor(j+(8*i), strip.Color(0,0,0) );
  68.       }
  69.     } // for j
  70.   }
  71.   strip.show();
  72.   delay(50); // helps smooth the update flicker a bit
  73.  
  74.  
  75. }
  76.  
  77.  
  78. // Input a value 0 to 255 to get a color value.
  79. // The colours are a transition r - g - b - back to r.
  80. uint32_t Wheel(byte WheelPos) {
  81.   WheelPos = 255 - WheelPos;
  82.   if(WheelPos < 85) {
  83.     /* 1
  84.     return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  85.     */
  86.     WheelPos -= 170;
  87.     return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  88.  
  89.   } else if(WheelPos < 170) {
  90.     /* 2
  91.     WheelPos -= 85;
  92.     return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  93.     */
  94.  
  95.     return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  96.   } else {
  97.     /* 3
  98.     WheelPos -= 170;
  99.     return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  100.     */
  101.     WheelPos -= 85;
  102.     return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  103.   }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement