Advertisement
Guest User

Arduino VU Meter

a guest
Oct 11th, 2011
5,769
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.17 KB | None | 0 0
  1. #define NUMREADINGS 5 // Number of readings (increase for a slow curve)
  2. #define MINSOUND 25   // Min sound intensity
  3. #define MAXSOUND 125  // Max sound intensity
  4.  
  5. const int firstLED = 2;
  6. const int lastLED = 9;
  7. int leds;
  8. int readings[NUMREADINGS];
  9. int index = 0;
  10. int total = 0;
  11. int average = 0;
  12. int sound = 0;
  13.  
  14. int x = 0;
  15. int y = 0;
  16.  
  17. void setup() {
  18.     for (int r = 0; r < NUMREADINGS; r++) {
  19.         readings[r] = 0;
  20.     }
  21.     for (int p = firstLED; p <= lastLED; p++) {
  22.         pinMode(p, OUTPUT);
  23.     }
  24.     leds = (lastLED - firstLED) + 1;
  25. }
  26.  
  27. void loop() {
  28.     total -= readings[index];
  29.     readings[index] = analogRead(0);
  30.     total += readings[index];
  31.     index++;
  32.  
  33.     if (index >= NUMREADINGS) {
  34.        index = 0;
  35.     }
  36.  
  37.     average = abs((total / NUMREADINGS) - 338); // ((1024 * 300) / 1000) = ~338
  38.     sound = map(average, MINSOUND, MAXSOUND, 0, leds);
  39.  
  40.     for (int ledON = firstLED; ledON < (firstLED + sound); ledON++) {
  41.         digitalWrite(ledON, HIGH);
  42.     }
  43.  
  44.     for (int ledOFF = (firstLED + sound); ledOFF <= lastLED; ledOFF++) {
  45.         digitalWrite(ledOFF, LOW);
  46.     }
  47.  
  48.     digitalWrite(10, HIGH);
  49.     digitalWrite(11, HIGH);
  50. }
  51.  
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement