Advertisement
Guest User

Untitled

a guest
Mar 18th, 2012
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SPI.h>
  2. #include <ShiftOutX.h>
  3. #include <ShiftPinNo.h>
  4.  
  5. /*
  6.  * LED driver
  7.  * Use 4 595 SR with ShiftOutX
  8.  * on pin 8, 11, 12
  9.  * plus one 33rd LED driven by
  10.  * Arduino pin 13.
  11.  *
  12.  * */
  13.  
  14. shiftOutX shiftRegisters(8, 11, 12, MSBFIRST, 4);
  15.  
  16. void setLed(int led, boolean state){
  17.   if(led>0 && led<33){
  18.     if(state){
  19.       shiftRegisters.pinOn(led);
  20.     }else{
  21.       shiftRegisters.pinOff(led);
  22.     }
  23.   }
  24.   if(led==33){
  25.     digitalWrite(13, state);
  26.   }
  27. }
  28.  
  29. /*
  30.  * Default Sound reactive Pattern
  31.  *
  32.  * */
  33.  
  34. // 2 level array to store pixels of each level
  35. int srd_levels[][5] = {{ 1,  0,  0,  0,  0}
  36.                     , { 2,  0,  0,  0,  0}
  37.                     , { 3,  6, 14,  0,  0}
  38.                     , { 4,  7, 15,  0,  0}
  39.                     , { 5,  8, 10, 16, 21}
  40.                     , { 9, 11, 17, 22,  0}
  41.                     , {12, 18, 23, 28,  0}
  42.                     , {13, 19, 24, 29,  0}
  43.                     , {20, 25, 30,  0,  0}
  44.                     , {26, 31,  0,  0,  0}
  45.                     , {27, 32,  0,  0,  0}
  46.                     , {33,  0,  0,  0,  0}};
  47.  
  48. void loopSoundReactDiagonal()
  49. {
  50.   // read input fro pin 0
  51.   int sound = analogRead(0);
  52.  
  53.   // read the potentiometer value
  54.   // be sure to have a value between 0 and 2
  55.   float p = analogRead(5)/1024.0F*2;
  56.  
  57.   // by multiplying sound by p
  58.   // the potentiometer can adjust the level
  59.   // of sensibility
  60.   sound = sound * p;
  61.  
  62.  
  63.   int i = 0;
  64.   int j = 1;
  65.  
  66.   // let's scan the 12 levels of sensibility
  67.   while(i<=11)
  68.   {
  69.     int cnt=0;
  70.     // is the sound value higher than the
  71.     // current level (i) sensibility
  72.     boolean isok = sound>256/31*j;
  73.    
  74.     // scan the current level pixels and
  75.     // set them on or off
  76.     while(cnt<=4){
  77.       setLed(srd_levels[i][cnt], isOk)
  78.       cnt++;
  79.     }
  80.     i++;
  81.     j++;
  82.   }
  83.  
  84.   // small delay
  85.   delay(10);
  86. }
  87.  
  88. /*
  89.  * Arduino functions
  90.  *
  91.  * */
  92.  
  93. void setup(){
  94.   pinMode( 8, OUTPUT); // SR latch
  95.   pinMode(11, OUTPUT); // SR data
  96.   pinMode(12, OUTPUT); // SR clock
  97.   pinMode(13, OUTPUT); // pixel #33
  98. }
  99.  
  100. void loop(){
  101.   loopSoundReactDiagonal() ;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement