Advertisement
djfoo000

Arduino Simple Solar Tracker

Aug 18th, 2011
9,485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. #include <Servo.h>
  2.  
  3. Servo servo1;          //declare servos
  4. Servo servo2;
  5. int pos1 = 0;          // pos1 is horizontal position
  6. int pos2 = 0;          // pos2 is vertical position
  7. int right = 0;         // right ldr
  8. int left = 0;          // left ldr
  9. int centre = 0;        // centre ldr
  10. int up = 0;            // top ldr
  11. int down = 0;          // bottom ldr
  12.  
  13. int ldr1 = 0;          // map LDR's to pins
  14. int ldr2 = 1;
  15. int ldr3 = 2;
  16. int ldr4 = 3;
  17. int ldr5 = 4;
  18.  
  19. void setup()
  20. {
  21.   servo1.attach(10);      // attach servo1 to digital pin 10
  22.   servo1.write(90);       // set initial position as 90 deg
  23.  
  24.   servo2.attach(9);        // attach servo1 to digital pin 9
  25.   servo2.write(90);        // set initial position as 90 deg
  26.  
  27.   pinMode(ldr1, INPUT);      // declare LDRs as input
  28.   pinMode(ldr2, INPUT);
  29.   pinMode(ldr3, INPUT);
  30.   pinMode(ldr4, INPUT);
  31.   pinMode(ldr5, INPUT);
  32. }
  33.  
  34. void loop()
  35. {
  36.  
  37.   pos1 = servo1.read();            // pos1 takes reading from current servo1 position
  38.   pos2 = servo2.read();            // pos2 takes reading from current servo2 position
  39.   int right = analogRead(ldr1);    // records reading from each LDRs
  40.   int centre = analogRead(ldr2);
  41.   int left = analogRead(ldr3);
  42.   int up = analogRead(ldr4);
  43.   int down = analogRead(ldr5);
  44.  
  45.                   // this portion is to control horizontal position
  46.   if(right > centre && left < centre)          // if right LDR has more light than centre LDR, and centre LDR has more light than left (means light is towards right)
  47.   {
  48.     servo1.write(pos1 +1);                    //increase position of servo1 by 1 (0 is left, 90 is centre, 180 is right)
  49.     delay(10);                                // this delay is needed to prevent servo from going 0-180 without stopping
  50.   }
  51.  
  52.   else if(left > centre && right < centre)      // if light is towards centre
  53.   {
  54.     servo1.write(pos1 -1);                    // decrease position of servo1 by 1 (0 is left, 90 is centre, 180 is right)
  55.     delay(10);
  56.   }
  57.  
  58.   else
  59.   {
  60.     servo1.write(pos1);                    // if neither condition is met, this means the panel is centre. Do not move panel
  61.   }
  62.                  
  63.                  // this portion is to control vertical position
  64.   if(up > centre && down < centre)
  65.   {
  66.     servo2.write(pos2 +1);
  67.     delay(10);
  68.   }
  69.  
  70.   else if(down > centre && up < centre)
  71.   {
  72.     servo2.write(pos2 -1);
  73.     delay(10);
  74.   }
  75.  
  76.   else
  77.   {
  78.     servo2.write(pos2);
  79.   }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement