Advertisement
safwan092

Untitled

Mar 29th, 2022
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. //Eliyas Science Info www.youtube.com/c/Eliyasscienceinfo
  2. //Arduino Single Axis Solar Tracker
  3. #include <Servo.h> //including the library of servo motor
  4. Servo myservo;
  5. int initial_position = 90;
  6. int LDR1 = A0; //connect The LDR1 on Pin A0
  7. int LDR2 = A1; //Connect The LDR2 on pin A1
  8. int error = 5;
  9. int servopin=11; //You can change servo just makesure its on arduino's PWM pin
  10. void setup()
  11. {
  12.  
  13.  
  14. myservo.attach(servopin);
  15. pinMode(LDR1, INPUT);
  16. pinMode(LDR2, INPUT);
  17. myservo.write(initial_position); //Move servo at 90 degree
  18. delay(2000);
  19. }
  20.  
  21. void loop()
  22. {
  23. int R1 = analogRead(LDR1); // read LDR 1
  24. int R2 = analogRead(LDR2); // read LDR 2
  25. int diff1= abs(R1 - R2);
  26. int diff2= abs(R2 - R1);
  27.  
  28. if((diff1 <= error) || (diff2 <= error)) {
  29.  
  30. } else {
  31. if(R1 > R2)
  32. {
  33. initial_position = --initial_position;
  34. }
  35. if(R1 < R2)
  36. {
  37. initial_position = ++initial_position;
  38. }
  39. }
  40. myservo.write(initial_position);
  41. delay(100);
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement