safwan092

Untitled

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