Advertisement
Hanneman

Arduino LED VU meter

Jul 25th, 2014
1,349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.01 KB | None | 0 0
  1. /*
  2. modified by varind in 2013 and again by Hannes in 2014
  3. this code is public domain, enjoy!
  4.  */
  5.  
  6. #include <SPI.h>
  7. #include <LiquidCrystal.h>
  8. #include <fix_fft.h>
  9. //#include <Servo.h>
  10. #define LCHAN 5
  11. #define RCHAN 4
  12. const int channels = 2;
  13. const int xres = 16; //16 is goed, 32 maakt hem duidelijker
  14. const int yres = 16; // 8 voor onderste gedeelte, 16 voor hele display
  15. const int gain = 3;
  16. //int decayrate = 2; // larger number is longer decay
  17. int decaytest = 1;
  18. char im[64], data[64];
  19. char Rim[64], Rdata[64];
  20. char data_avgs[32];
  21. float peaks[32];
  22. int i = 0,val,Rval;
  23. int x = 0, y=0, z=0;
  24. int q = 1; // Band kiezen voor VU uitslag 74hc595- 1
  25. int r = 1; // Band kiezen voor VU uitslag 74hc595- 2
  26. // Liquid Crystal latch pin
  27. LiquidCrystal lcd(9);
  28.  
  29. //const int LEDpins[6] = {3,4,5,6,7,8};
  30.  
  31.     int current;
  32.     int highest = 350;
  33.     int latchPin = 4;
  34.     int clockPin = 2;
  35.     int dataPin = 3;
  36.     int volume[9] = {0b00000000, 0b00000001, 0b00000011, 0b00000111,
  37.                      0b00001111, 0b00011111, 0b00111111, 0b01111111,
  38.                      0b11111111};
  39.                      
  40.     int current2;
  41.     int highest2 = 350;
  42.     int latchPin2 = 7;
  43.     int clockPin2 = 8;
  44.     int dataPin2 = 6;
  45.     int volume2[9] = {0b00000000, 0b00000001, 0b00000011, 0b00000111,
  46.                      0b00001111, 0b00011111, 0b00111111, 0b01111111,
  47.                      0b11111111};
  48.  
  49.  
  50. /*
  51. Servo servo1, servo2, servo3, servo4;
  52. int startpos = 90;
  53. int calibrate = 0;  //locks servos at startpos (lowest value)
  54. int pos1 = startpos, pos2 = startpos, pos3 = startpos, pos4 = startpos;
  55. int servomap, servomap2;
  56. */
  57.  
  58. // VU METER CHARACTERS
  59. byte v1[8] = {
  60.   B00000,B00000,B00000,B00000,B00000,B00000,B00000,B11111};
  61. byte v2[8] = {
  62.   B00000,B00000,B00000,B00000,B00000,B00000,B11111,B11111};
  63. byte v3[8] = {
  64.   B00000,B00000,B00000,B00000,B00000,B11111,B11111,B11111};
  65. byte v4[8] = {
  66.   B00000,B00000,B00000,B00000,B11111,B11111,B11111,B11111};
  67. byte v5[8] = {
  68.   B00000,B00000,B00000,B11111,B11111,B11111,B11111,B11111};
  69. byte v6[8] = {
  70.   B00000,B00000,B11111,B11111,B11111,B11111,B11111,B11111};
  71. byte v7[8] = {
  72.   B00000,B11111,B11111,B11111,B11111,B11111,B11111,B11111};
  73. byte v8[8] = {
  74.   B11111,B11111,B11111,B11111,B11111,B11111,B11111,B11111};
  75.  
  76.  
  77. void setup() {
  78.       pinMode(latchPin, OUTPUT);
  79.       pinMode(clockPin, OUTPUT);
  80.       pinMode(dataPin, OUTPUT);
  81.       pinMode(latchPin2, OUTPUT);
  82.       pinMode(clockPin2, OUTPUT);
  83.       pinMode(dataPin2, OUTPUT);
  84.       Serial.begin(9600);
  85.   // ============= LEDS ==============
  86.  //   for (int x=0; x<6; x++) {
  87.  //     pinMode(LEDpins[x], OUTPUT);
  88.   //}
  89.   // ============= LEDS END ==============
  90.  
  91.   lcd.begin(16, 2);
  92.   lcd.clear();
  93.   lcd.createChar(1, v1);
  94.   lcd.createChar(2, v2);
  95.   lcd.createChar(3, v3);
  96.   lcd.createChar(4, v4);
  97.   lcd.createChar(5, v5);
  98.   lcd.createChar(6, v6);
  99.   lcd.createChar(7, v7);
  100.   lcd.createChar(8, v8);
  101.  
  102.   //analogReference(DEFAULT);
  103.  
  104.   //  servo1.attach(5);
  105.   //  servo2.attach(6);
  106.   //  servo3.attach(9);
  107.   //  servo4.attach(10);
  108.  
  109.   //  servo1.write(pos1);
  110.   //  servo2.write(pos2);
  111.   //  servo3.write(pos3);
  112.   //  servo4.write(pos4);
  113. }
  114.  
  115. void loop() {
  116.  
  117. //  delay(10);
  118.   for (i=0; i < 64; i++){    
  119.     val = ((analogRead(LCHAN) / 4 ) - 128);  // chose how to interpret the data from analog in
  120.     data[i] = val;                                      
  121.     im[i] = 0;  
  122.     if (channels ==2){
  123.       Rval = ((analogRead(RCHAN) / 4 ) - 128);  // chose how to interpret the data from analog in
  124.       Rdata[i] = Rval;                                      
  125.       Rim[i] = 0;  
  126.     }
  127.   };
  128.  
  129.   fix_fft(data,im,6,0); // Send the data through fft
  130.   if (channels == 2){
  131.     fix_fft(Rdata,Rim,6,0); // Send the data through fft
  132.   }
  133.  
  134.   // get the absolute value of the values in the array, so we're only dealing with positive numbers
  135.   for (i=0; i< 32 ;i++){  
  136.     data[i] = sqrt(data[i] * data[i] + im[i] * im[i]);
  137.   }
  138.   if (channels ==2){
  139.     for (i=16; i< 32 ;i++){  
  140.       data[i] = sqrt(Rdata[i-16] * Rdata[i-16] + Rim[i-16] * Rim[i-16]);
  141.     }
  142.   }
  143.  
  144.   // todo: average as many or as little dynamically based on yres
  145.   for (i=0; i<32; i++) {
  146.     data_avgs[i] = (data[i]);// + data[i*2+1]);// + data[i*3 + 2]);// + data[i*4 + 3]);  // add 3 samples to be averaged, use 4 when yres < 16
  147.     data_avgs[i] = constrain(data_avgs[i],0,9-gain);  //data samples * range (0-9) = 9
  148.     data_avgs[i] = map(data_avgs[i], 0, 9-gain, 0, yres);        // remap averaged values
  149.   }
  150.  
  151.  
  152.  
  153.   //servos();
  154.   decay(1);
  155.   //thirtytwoband();
  156.   mono();
  157.   //stereo8();
  158.   //stereo16();
  159.  
  160.  
  161. } // end loop
  162.  
  163. void decay(int decayrate){
  164.   //// reduce the values of the last peaks by 1
  165.   if (decaytest == decayrate){
  166.     for (x=0; x < 32; x++) {
  167.       peaks[x] = peaks[x] - 1;  // subtract 1 from each column peaks
  168.       decaytest = 0;
  169.      
  170.     }
  171.   }
  172.   decaytest++;
  173.  
  174. }
  175.  
  176. void mono(){
  177.   for (x=0; x < xres; x++) {  // repeat for each column of the display horizontal resolution
  178.     y = data_avgs[x];  // get current column value
  179.         z= peaks[x];
  180.     if (y > z){
  181.       peaks[x]=y;
  182.     }
  183.     y= peaks[x];
  184.    
  185.       current = map(data[q], 0, 18, 0, 9);
  186.       digitalWrite(latchPin, LOW);
  187.       shiftOut(dataPin, clockPin, LSBFIRST, volume[current]);  
  188.       digitalWrite(latchPin, HIGH);
  189.      
  190.       current2 = map(data[r], 0, 18, 0, 9);
  191.       digitalWrite(latchPin2, LOW);
  192.       shiftOut(dataPin2, clockPin2, LSBFIRST, volume2[current2]);  
  193.       digitalWrite(latchPin2, HIGH);
  194.           /*
  195.     if (data[q]  >1) {
  196.      digitalWrite(LEDpins[0], HIGH);
  197.     } else {
  198.       digitalWrite(LEDpins[0], LOW);
  199.     }
  200.    
  201.         if (data[q]  >3) {
  202.      digitalWrite(LEDpins[1], HIGH);
  203.     } else {
  204.       digitalWrite(LEDpins[1], LOW);
  205.     }
  206.    
  207.    
  208.         if (data[q]  >5) {
  209.      digitalWrite(LEDpins[2], HIGH);
  210.     } else {
  211.       digitalWrite(LEDpins[2], LOW);
  212.     }
  213.    
  214.         if (data[q]  >7) {
  215.      digitalWrite(LEDpins[3], HIGH);
  216.     } else {
  217.       digitalWrite(LEDpins[3], LOW);
  218.     }
  219.    
  220.             if (data[q]  >9) {
  221.      digitalWrite(LEDpins[4], HIGH);
  222.     } else {
  223.       digitalWrite(LEDpins[4], LOW);
  224.     }
  225.    
  226.    
  227.         if (data[q]  >11) {
  228.      digitalWrite(LEDpins[5], HIGH);
  229.     } else {
  230.       digitalWrite(LEDpins[5], LOW);
  231.     }
  232.    
  233.         if (data[q]  >13) {
  234.      digitalWrite(LEDpins[6], HIGH);
  235.     } else {
  236.       digitalWrite(LEDpins[6], LOW);
  237.     }
  238.     */
  239.  
  240.     if (y <= 8){            
  241.       lcd.setCursor(x,0); // clear first row
  242.       lcd.print(" ");
  243.       lcd.setCursor(x,1); // draw second row
  244.       if (y == 0){
  245.         lcd.print(" "); // save a glyph
  246.       }
  247.       else {
  248.         lcd.write(y);
  249.       }
  250.     }
  251.     else{
  252.       lcd.setCursor(x,0);  // draw first row
  253.       if (y == 9){
  254.        lcd.print(" ");  
  255.       }
  256.       else {
  257.         lcd.write(y-8);  // same chars 1-8 as 9-16
  258.       }
  259.       lcd.setCursor(x,1);
  260.       lcd.write(8);  
  261.     } // end display
  262.   }  // end xres
  263. }
  264. /*
  265. void stereo8(){
  266.   for (x=0; x < 8; x++) {
  267.     y = data_avgs[x];
  268.         z= peaks[x];
  269.     if (y > z){
  270.       peaks[x]=y;
  271.     }
  272.     y= peaks[x];
  273.  
  274.     if (y <= 8){            
  275.       lcd.setCursor(x,0); // clear first row
  276.       lcd.print(" ");
  277.       lcd.setCursor(x,1); // draw second row
  278.       if (y == 0){
  279.         lcd.print(" "); // save a glyph
  280.       }
  281.       else {
  282.         lcd.write(y);
  283.       }
  284.     }
  285.     else{
  286.       lcd.setCursor(x,0);  // draw first row
  287.       if (y == 9){
  288.         lcd.write(" ");  
  289.       }
  290.       else {
  291.         lcd.write(y-8);  // same chars 1-8 as 9-16
  292.       }
  293.       lcd.setCursor(x,1);
  294.       lcd.write(8);  
  295.     }
  296.   }
  297.   for (x=16; x < 32; x++) {
  298.     y = data_avgs[x];
  299.        z= peaks[x];
  300.     if (y > z){
  301.       peaks[x]=y;
  302.     }
  303.     y= peaks[x];
  304.  
  305.     if (y <= 8){            
  306.       lcd.setCursor(x-8,0); // clear first row
  307.       lcd.print(" ");
  308.       lcd.setCursor(x-8,1); // draw second row
  309.       if (y == 0){
  310.         lcd.print(" "); // save a glyph
  311.       }
  312.       else {
  313.         lcd.write(y);
  314.       }
  315.     }
  316.     else{
  317.       lcd.setCursor(x-8,0);  // draw first row
  318.       if (y == 9){
  319.         lcd.write(" ");  
  320.       }
  321.       else {
  322.         lcd.write(y-8);  // same chars 1-8 as 9-16
  323.       }
  324.       lcd.setCursor(x-8,1);
  325.       lcd.write(8);  
  326.     }
  327.   }
  328. }
  329.  
  330. void stereo16(){
  331.   for (x=0; x < 16; x++) {
  332.     y = data_avgs[x];
  333.          z= peaks[x];
  334.     if (y > z){
  335.       peaks[x]=y;
  336.     }
  337.     y= peaks[x];
  338.     if (x < xres){            
  339.       lcd.setCursor(x,0); // draw first row
  340.       if (y == 0){
  341.         lcd.print(" "); // save a glyph
  342.       }
  343.       else {
  344.         lcd.write(y);
  345.       }
  346.     }
  347.   }
  348.  
  349.   for (x=16; x < 32; x++) {
  350.     y = data_avgs[x];
  351.         z= peaks[x];
  352.     if (y > z){
  353.       peaks[x]=y;
  354.     }
  355.     y= peaks[x];
  356.     if (x-16 < xres){            
  357.       lcd.setCursor(x-16,1); // draw second row
  358.       if (y == 0){
  359.         lcd.print(" "); // save a glyph
  360.       }
  361.       else {
  362.         lcd.write(y);
  363.       }
  364.     }
  365.   }
  366. }
  367.  
  368.  
  369. void thirtytwoband(){
  370.   for (x=0; x < 32; x++) {
  371.     y = data_avgs[x];
  372.  
  373.         z= peaks[x];
  374.     if (y > z){
  375.       peaks[x]=y;
  376.     }
  377.     y= peaks[x];
  378.     if (x < 16){            
  379.       lcd.setCursor(x,0); // draw second row
  380.       if (y == 0){
  381.         lcd.print(" "); // save a glyph
  382.       }
  383.       else {
  384.         lcd.write(y);
  385.       }
  386.     }
  387.     else{
  388.       lcd.setCursor(x-16,1);
  389.       if (y == 0){
  390.         lcd.print(" ");
  391.       }
  392.       else {
  393.         lcd.write(y);
  394.       }
  395.     }
  396.   }
  397. }
  398.  
  399.  
  400. void goservo(){
  401.   for (x=0; x < xres; x++) {  // repeat for each column of the display horizontal resolution
  402.     y = data_avgs[x];  // get current column value
  403.     if (calibrate != 1){
  404.       servomap = map(y, 0, yres, 90, 180);
  405.       servomap2 = map(y, 0, yres, 90, 00);
  406.       if ( x== xres/8){
  407.         servo1.write(servomap);    
  408.       }
  409.       if (x == xres/4){
  410.         servo2.write(servomap2);
  411.       }  
  412.       if (x == xres/2){
  413.         servo3.write(servomap);    
  414.       }
  415.       if (x == xres/1){
  416.         servo4.write(servomap2);
  417.       }
  418.     }
  419.   }
  420. } */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement