Advertisement
Guest User

Sound Reactive LEDs

a guest
Sep 7th, 2012
713
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.08 KB | None | 0 0
  1. int data = 2;
  2. int clock = 3;
  3. int latch = 4;
  4. int v;
  5. int lights = 0;
  6. unsigned long startMillis = 0;
  7. unsigned long currentMillis;
  8. int fallDelay = 75;
  9.  
  10. void setup(){
  11.   Serial.begin(9600);
  12.   pinMode(latch, OUTPUT);
  13.   pinMode(clock, OUTPUT);
  14.   pinMode(data, OUTPUT);
  15. }
  16.  
  17. void loop(){
  18.   v = getVolume();
  19. //  v = getSerial();
  20.   lights = calculate(v);
  21.   Serial.println(v);
  22.   light(lights);
  23.   lights = 0;
  24.   barFall();
  25. }
  26.  
  27. int getSerial(){
  28.   if (Serial.available()) {
  29.     v = int(Serial.read()-'0');
  30.   }
  31.   return v;
  32. }
  33.  
  34. int getVolume(){
  35.   v=analogRead(0);
  36.   v=constrain(v,0,100);
  37.   v=map(v,0,100,1,8);
  38.   return v;
  39. }
  40.  
  41. int calculate(int v){
  42.   lights = (1 << v) -1;
  43.   return(lights);
  44. }
  45.  
  46. void light(int lights){
  47.   digitalWrite(latch, LOW);
  48.   shiftOut(data, clock, MSBFIRST, lights);
  49.   digitalWrite(latch, HIGH);
  50.   delay(10);
  51. }
  52.  
  53. unsigned long barFall(){
  54.   startMillis=millis();
  55.   for (int i = v; i > 0; i--){
  56.     int f = ((1<<i) -1) ;
  57.     if (millis()-startMillis < fallDelay){  
  58.       light(f);
  59.       delay(40);
  60.     }
  61.     else{
  62.       loop();
  63.     }
  64.   }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement