mpmcws

Inseguitore solare # 1 - test2 -

Jun 15th, 2018
87
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Servo.h>
  2. Servo myservo;
  3. int pos = 90;   // initial position
  4. int sens1 = A0; // LRD 1 pin
  5. int sens2 = A1; //LDR 2 pin
  6. int tolerance = 2;
  7. void setup()
  8. {
  9.   myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  10.   pinMode(sens1, INPUT);
  11.   pinMode(sens2, INPUT);
  12.   myservo.write(pos);
  13.   delay(2000); // a 2 seconds delay while we position the solar panel
  14. }
  15. void loop()
  16. {
  17.   int val1 = analogRead(sens1); // read the value of sensor 1
  18.   int val2 = analogRead(sens2); // read the value of sensor 2
  19.   if ((abs(val1 - val2) <= tolerance) || (abs(val2 - val1) <= tolerance)) {
  20.     //do nothing if the difference between values is within the tolerance limit
  21.   } else {
  22.     if (val1 > val2)
  23.     {
  24.       pos = --pos;
  25.     }
  26.     if (val1 < val2)
  27.     {
  28.       pos = ++pos;
  29.     }
  30.   }
  31.   if (pos > 180) {
  32.     pos = 180;  // reset to 180 if it goes higher
  33.   }
  34.   if (pos < 0) {
  35.     pos = 0;  // reset to 0 if it goes lower
  36.   }
  37.   myservo.write(pos); // write the position to servo
  38.   delay(20);
  39. }
RAW Paste Data