Advertisement
Guest User

Arduino Hand Wave

a guest
Feb 20th, 2012
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.79 KB | None | 0 0
  1. // senses hand wave movement
  2.  
  3. // lights
  4. int l1 = 6;
  5. int l2 = 9;
  6. int l3 = 10;
  7. int l4 = 11;
  8.  
  9. // analog inputs
  10. int i1 = A3;
  11. int i2 = A2;
  12. int i3 = A1;
  13. int i4 = A0;
  14.  
  15. // voltages at the inputs
  16. float v1;
  17. float v2;
  18. float v3;
  19. float v4;
  20.  
  21. // array of the order in which pins should dim
  22. int order[] = {0,0,0,0};
  23. int orderCount = 0;
  24.  
  25. void setup() {
  26.     // run the reset funciton once just to get everything zeroed
  27.     dimSetReset();
  28.     pinMode(l1, OUTPUT);
  29.     pinMode(l2, OUTPUT);
  30.     pinMode(l3, OUTPUT);
  31.     pinMode(l4, OUTPUT);
  32. }
  33.  
  34. void loop() {
  35.     // we only want to loop when we see a "motion"
  36.     if((analogRead(i1) * .004882814 > 0.30) || (analogRead(i2) * .004882814 > 0.30) || (analogRead(i3) * .004882814 > 0.30) || (analogRead(i4) * .004882814 > 0.30)) {
  37.         // run thru the process a bunch to see what happens
  38.         for(int i = 0; i < 100; ++i) {
  39.             v1 = getvoltage(i1);
  40.             checkvoltage(v1,l1);
  41.             v2 = getvoltage(i2);
  42.             checkvoltage(v2,l2);
  43.             v3 = getvoltage(i3);
  44.             checkvoltage(v3,l3);
  45.             v4 = getvoltage(i4);
  46.             checkvoltage(v4,l4);
  47.             delay(5);
  48.         }
  49.  
  50.         // we have gone thru the loop, lets see what happens and reset for next time
  51.         dimEnd();
  52.         dimSetReset();
  53.     }
  54. }
  55.  
  56. // return the voltage that a pin sees using a conversion factor
  57. float getvoltage(int pin) {
  58.     return (analogRead(pin) * 0.004882814);
  59. }
  60.  
  61. // big function that figures out what order the pins are seeing the voltage in
  62. void checkvoltage(float voltage, int pin) {
  63.     int pinFound = 0;
  64.    
  65.     // don't do anything unless we see a voltage above the threshold
  66.     if(voltage >= 0.30) {
  67.         // turn the LED on first
  68.         digitalWrite(pin, HIGH);
  69.  
  70.         // make sure that we havent seen a voltage on the given pin already
  71.         // we don't want to clog up the array
  72.         for(int i = 0; i < 4; ++i) {
  73.             if(order[i] == pin)
  74.                 pinFound = 1;
  75.         }
  76.  
  77.         // if we haven't found a dupicate pin (good) and the order we are at is unfilled (0)
  78.         // then fill the array with the value of the pin that we detected a voltage change at
  79.         if(!pinFound && order[orderCount] == 0) {
  80.             order[orderCount] = pin;
  81.             orderCount++;
  82.         }
  83.     }
  84. }
  85.  
  86. // dim the specified pin using PWM
  87. void dim(int pin) {
  88.     for(int i = 255; i >= 0; --i) {
  89.         analogWrite(pin, i);
  90.         delay(1);
  91.     }
  92. }
  93.  
  94. // set the order in which to dim the LEDs and dim them
  95. void dimEnd() {
  96.  
  97.     // look thru the order array and dim the LEDs in the specified order
  98.     for(int i = 0; i < 4; ++i) {
  99.         switch(order[i]) {
  100.             case 6:
  101.                 dim(l1);
  102.                 break;
  103.             case 9:
  104.                 dim(l2);
  105.                 break;
  106.             case 10:
  107.                 dim(l3);
  108.                 break;
  109.             case 11:
  110.                 dim(l4);
  111.                 break;
  112.             default:
  113.                 break;
  114.         }
  115.     }
  116. }
  117.  
  118. // reset so we can begin afresh
  119. void dimSetReset() {
  120.     // reset order array
  121.     for(int i = 0; i < 4; ++i) {
  122.         order[i] = 0;
  123.     }
  124.  
  125.     // start over with what position we are in the order of LEDs
  126.     orderCount = 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement