Advertisement
Guest User

arduino visualizer

a guest
Jan 31st, 2012
7,649
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.51 KB | None | 0 0
  1. // Arduino Visualizer code by Mark Neuburger
  2.  
  3. // Alex Leone's Arduino TLC5490 library
  4. // http://code.google.com/p/tlc5940arduino/
  5. // see his excellent documentation to learn how to use
  6. #include "Tlc5940.h"
  7.  
  8. // keep track of spectrum analyzer vals (0-1023) for 7 bands
  9. int SpectrumLeft[7];
  10. int SpectrumRight[7];
  11.  
  12. // max of L/R channels (0-1023) for last displayed spectrum values
  13. int lastSpectrum[7];
  14.  
  15. // setup pins for spectrum analayzer
  16. int spectrumReset=5;
  17. int spectrumStrobe=4;
  18. int spectrumAnalogLeft=0;
  19. int spectrumAnalogRight=1;
  20.  
  21. // 12 bits/channel TLC5490 takes values from 0-4095
  22. // 4095 is bright enough to make looking at it painful with my
  23. // high intensity LEDs
  24. const int MAX_BRIGHT = 1900;
  25.  
  26. // map LED order to TLC5490 pins
  27. // i.e. bottom-left LED is connected to TLC5490 pin 54,
  28. // the one to its immediate right is connected to TLC5490 pin 51
  29. // TLC5490s are daisy chained and numbering goes like this:
  30. // 0-15 (first TLC5490), 16-31 (second TLC5490), etc.
  31. int pins[] = {
  32.   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};
  33.  
  34. void setup() {
  35.   spectrum_init();
  36.   Tlc.init(0);
  37.  
  38.   for (byte i = 0; i < 7; i++ ) {
  39.     lastSpectrum[i] = 0;
  40.   }
  41. }
  42.  
  43. void loop() {
  44.   Tlc.clear();
  45.  
  46.   int channel, newSpectrum, oldSpectrum, updatedSpectrum;;
  47.  
  48.   // each band is represented as a column
  49.   for (byte band = 1; band <= 7; band++) {
  50.     // read spectrum analyzer  
  51.     SpectrumLeft[band-1] = analogRead(spectrumAnalogLeft);
  52.     SpectrumRight[band-1] = analogRead(spectrumAnalogRight);
  53.     digitalWrite(spectrumStrobe,HIGH);
  54.     digitalWrite(spectrumStrobe,LOW);
  55.  
  56.     newSpectrum = max(SpectrumLeft[band-1], SpectrumRight[band-1]);
  57.     oldSpectrum = lastSpectrum[band-1];
  58.  
  59.     // rather than displaying current exact values,
  60.     // smooth out the display to sacrifice a little bit of accuracy
  61.     // to get a more pleasing rising and falling effect
  62.     // also, adjust coefficients make bars climb faster than they drop
  63.     if (oldSpectrum < newSpectrum)
  64.       updatedSpectrum = (newSpectrum * .7) + (oldSpectrum * .3);
  65.     else
  66.       updatedSpectrum = (newSpectrum * .4) + (oldSpectrum * .6);
  67.  
  68.     // set a lower threshold to clean up display a little
  69.     // (don't display anything if it's just some noise)
  70.     if (updatedSpectrum < 150) updatedSpectrum = 0;
  71.  
  72.     // bump up some of the very loud notes to red to liven the display a little
  73.     if (updatedSpectrum > 975) updatedSpectrum = 1023;
  74.  
  75.     // convert value from 0-1023 to 0-7 for display
  76.     // eschew using map() to experiment with non-linear scaling
  77.     int displayHeight = min(7, int(0.5 + (updatedSpectrum / (1023 / 7))));
  78.  
  79.     // set which LEDs should be lit up (doesn't actually change anything)
  80.     for (int row = 1; row <= displayHeight; row++) {
  81.       channel = (row-1)*7+band-1;
  82.       Tlc.set(pins[channel], MAX_BRIGHT);
  83.     }
  84.    
  85.     // remember the current values for use in the next iteration
  86.     lastSpectrum[band-1] = updatedSpectrum;
  87.   }
  88.   // actually update the TLC5490
  89.   Tlc.update();
  90.  
  91.   delay(25);
  92. }
  93.  
  94. void spectrum_init()
  95. {
  96.   // setup pins to drive the spectrum analyzer
  97.   pinMode(spectrumReset, OUTPUT);
  98.   pinMode(spectrumStrobe, OUTPUT);
  99.  
  100.   // initialize spectrum analyzer
  101.   digitalWrite(spectrumStrobe,LOW);
  102.   delay(1);
  103.   digitalWrite(spectrumReset,HIGH);
  104.   delay(1);
  105.   digitalWrite(spectrumStrobe,HIGH);
  106.   delay(1);
  107.   digitalWrite(spectrumStrobe,LOW);
  108.   delay(1);
  109.   digitalWrite(spectrumReset,LOW);
  110.   delay(5);
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement