SHARE
TWEET

Stigern

a guest May 6th, 2009 488 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Servo.h>
  2.  
  3. Servo myservo;
  4.  
  5. int pos = 0;  // Variable to store the servo position.
  6. int inputPhotoLeft = 1; // Easier to read, instead of just 1 or 0.
  7. int inputPhotoRight = 0;
  8.  
  9. int Left = 0; // Store readings from the photoresistors.
  10. int Right = 0; // Store readings from the photoresistors.
  11.  
  12. void setup()
  13. {
  14.   myservo.attach(9); // Attach servo to pin 9.
  15. }
  16.  
  17. void loop()
  18. {
  19.   // Reads the values from the photoresistors to the Left and Right variables.
  20.   Left = analogRead(inputPhotoLeft);
  21.   Right = analogRead(inputPhotoRight);
  22.  
  23.   // Checks if right is greater than left, if so move to right.
  24.   if (Left > (Right +20))
  25.     // +20 is the deadzone, so it wont jiggle back and forth.
  26.   {
  27.     if (pos < 179)
  28.       pos++;
  29.     myservo.write(pos);
  30.   }
  31.  
  32.   // Checks if left is greater than right, if so move to left.
  33.   if (Right > (Left +20))
  34.     // +20 is the deadzone, so it wont jiggle back and forth.
  35.   {
  36.     if (pos > 1)
  37.       pos--;
  38.     myservo.write(pos);
  39.   }
  40.  
  41.   // Added some delay, increase or decrease if you want less or more speed.
  42.   delay(10);
  43. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top