Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Arduino Visualizer code by Mark Neuburger
- // Alex Leone's Arduino TLC5490 library
- // http://code.google.com/p/tlc5940arduino/
- // see his excellent documentation to learn how to use
- #include "Tlc5940.h"
- // keep track of spectrum analyzer vals (0-1023) for 7 bands
- int SpectrumLeft[7];
- int SpectrumRight[7];
- // max of L/R channels (0-1023) for last displayed spectrum values
- int lastSpectrum[7];
- // setup pins for spectrum analayzer
- int spectrumReset=5;
- int spectrumStrobe=4;
- int spectrumAnalogLeft=0;
- int spectrumAnalogRight=1;
- // 12 bits/channel TLC5490 takes values from 0-4095
- // 4095 is bright enough to make looking at it painful with my
- // high intensity LEDs
- const int MAX_BRIGHT = 1900;
- // map LED order to TLC5490 pins
- // i.e. bottom-left LED is connected to TLC5490 pin 54,
- // the one to its immediate right is connected to TLC5490 pin 51
- // TLC5490s are daisy chained and numbering goes like this:
- // 0-15 (first TLC5490), 16-31 (second TLC5490), etc.
- int pins[] = {
- 54,51,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,30,22,23,24,25,26,27,28,29,31,32,33,34,35,36,37,38,39,40,41,42,53,55,59,49,47,43};
- void setup() {
- spectrum_init();
- Tlc.init(0);
- for (byte i = 0; i < 7; i++ ) {
- lastSpectrum[i] = 0;
- }
- }
- void loop() {
- Tlc.clear();
- int channel, newSpectrum, oldSpectrum, updatedSpectrum;;
- // each band is represented as a column
- for (byte band = 1; band <= 7; band++) {
- // read spectrum analyzer
- SpectrumLeft[band-1] = analogRead(spectrumAnalogLeft);
- SpectrumRight[band-1] = analogRead(spectrumAnalogRight);
- digitalWrite(spectrumStrobe,HIGH);
- digitalWrite(spectrumStrobe,LOW);
- newSpectrum = max(SpectrumLeft[band-1], SpectrumRight[band-1]);
- oldSpectrum = lastSpectrum[band-1];
- // rather than displaying current exact values,
- // smooth out the display to sacrifice a little bit of accuracy
- // to get a more pleasing rising and falling effect
- // also, adjust coefficients make bars climb faster than they drop
- if (oldSpectrum < newSpectrum)
- updatedSpectrum = (newSpectrum * .7) + (oldSpectrum * .3);
- else
- updatedSpectrum = (newSpectrum * .4) + (oldSpectrum * .6);
- // set a lower threshold to clean up display a little
- // (don't display anything if it's just some noise)
- if (updatedSpectrum < 150) updatedSpectrum = 0;
- // bump up some of the very loud notes to red to liven the display a little
- if (updatedSpectrum > 975) updatedSpectrum = 1023;
- // convert value from 0-1023 to 0-7 for display
- // eschew using map() to experiment with non-linear scaling
- int displayHeight = min(7, int(0.5 + (updatedSpectrum / (1023 / 7))));
- // set which LEDs should be lit up (doesn't actually change anything)
- for (int row = 1; row <= displayHeight; row++) {
- channel = (row-1)*7+band-1;
- Tlc.set(pins[channel], MAX_BRIGHT);
- }
- // remember the current values for use in the next iteration
- lastSpectrum[band-1] = updatedSpectrum;
- }
- // actually update the TLC5490
- Tlc.update();
- delay(25);
- }
- void spectrum_init()
- {
- // setup pins to drive the spectrum analyzer
- pinMode(spectrumReset, OUTPUT);
- pinMode(spectrumStrobe, OUTPUT);
- // initialize spectrum analyzer
- digitalWrite(spectrumStrobe,LOW);
- delay(1);
- digitalWrite(spectrumReset,HIGH);
- delay(1);
- digitalWrite(spectrumStrobe,HIGH);
- delay(1);
- digitalWrite(spectrumStrobe,LOW);
- delay(1);
- digitalWrite(spectrumReset,LOW);
- delay(5);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement