Advertisement
Guest User

DigiSparkProTest

a guest
Jun 25th, 2014
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.53 KB | None | 0 0
  1. #include "LedControl.h"
  2.  
  3. int potX = A5;
  4. int potY = A8;
  5. int ldr = A9;
  6. const int buttonPin = 1;
  7.  
  8. int joyX = 0;
  9. int joyY = 0;
  10. int ldr_val = 0;
  11.  
  12. LedControl lc=LedControl(10,11,12,1);
  13.  
  14. void SetAllOn()
  15. {
  16.   for(int row=0;row<8;row++)
  17.   {
  18.     for(int col=0;col<8;col++)
  19.     {
  20.       lc.setLed(0,row,col,true);
  21.     }
  22.   }
  23. }
  24.  
  25. void SetAllOff()
  26. {
  27.   for(int row=0;row<8;row++)
  28.   {
  29.     for(int col=0;col<8;col++)
  30.     {
  31.       lc.setLed(0,row,col,false);
  32.     }
  33.   }
  34. }
  35.  
  36. void SetSingleOn(int xpos, int ypos)
  37. {
  38.   for(int row=0;row<8;row++)
  39.   {
  40.     for(int col=0;col<8;col++)
  41.     {
  42.       if(row == xpos && col == ypos)
  43.         lc.setLed(0,row,col,true);
  44.       else
  45.         lc.setLed(0,row,col,false);
  46.     }
  47.   }
  48. }
  49.  
  50. void setup()
  51. {
  52.   pinMode(buttonPin, INPUT);
  53.  
  54.   lc.shutdown(0,false);
  55.  
  56.   lc.setIntensity(0,8);
  57.  
  58.   lc.clearDisplay(0);
  59.  
  60.   SetAllOff();
  61. }
  62.  
  63. void RandomFill()
  64. {
  65.   lc.setLed(0, random(0,8), random(0,8), random(0,2));
  66.   delay(3);
  67. }
  68.  
  69. int AvgRead()
  70. {
  71.   unsigned int sum = 0;
  72.   for(int i = 0; i < 50; i++)
  73.   {
  74.     sum += analogRead(ldr);
  75.   }
  76.   return sum / 50;
  77. }
  78.  
  79. void loop()
  80. {
  81.   int xpos = 0;
  82.   int ypos = 0;
  83.   joyX = analogRead(potX);
  84.   joyY = analogRead(potY);
  85.   ldr_val = AvgRead();
  86.   lc.setIntensity(0, map(ldr_val, 0, 1023, 0, 15));
  87.  
  88.   xpos = map(joyX, 0, 1023, 7, 0);
  89.   ypos = map(joyY, 0, 1023, 7, 0);
  90.  
  91.   SetSingleOn(xpos, ypos);
  92.  
  93.   while(digitalRead(buttonPin) == 0)
  94.   {
  95.     RandomFill();
  96.     ldr_val = AvgRead();
  97.     lc.setIntensity(0, map(ldr_val, 32, 1023, 0, 15));
  98.   }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement