Advertisement
Guest User

LightFollowerArduino

a guest
Feb 16th, 2011
710
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1.     #include <Servo.h>
  2.  
  3.     Servo myservo;
  4.     Servo myservo2;
  5.  
  6.     int pos = 0;  // Variable to store the servo position.
  7.     int pos2 = 0;  // Variable to store the servo position.
  8.     int inputPhotoLeft = 1; // Easier to read, instead of just 1 or 0.
  9.     int inputPhotoRight = 0;
  10.     int inputPhotoUP = 3; // Easier to read, instead of just 1 or 0.
  11.     int inputPhotoDOWN = 2;
  12.  
  13.     int Left = 0; // Store readings from the photoresistors.
  14.     int Right = 0; // Store readings from the photoresistors.
  15.     int UP = 0; // Store readings from the photoresistors.
  16.     int DOWN = 0; // Store readings from the photoresistors.
  17.  
  18.  
  19.     void setup()
  20.     {
  21.     myservo.attach(9); // Attach servo to pin 9.
  22.     myservo2.attach(10); // Attach servo to pin 10.
  23.     Serial.begin(9600); // Begin serial communcation
  24.     }
  25.  
  26.     void loop()
  27.     {
  28.        Serial.println(analogRead(inputPhotoRight));
  29.     // Reads the values from the photoresistors to the Left and Right variables.
  30.     Left = analogRead(inputPhotoLeft);
  31.     Right = analogRead(inputPhotoRight);
  32.  
  33.     // Checks if right is greater than left, if so move to right.
  34.     if (Left > (Right +10))
  35.     // +20 is the deadzone, so it wont jiggle back and forth.
  36.     {
  37.     if (pos < 139)
  38.     pos++;
  39.     myservo.write(pos);
  40.     }
  41.     // Checks if left is greater than right, if so move to left.
  42.     if (Right > (Left +10))
  43.     // +20 is the deadzone, so it wont jiggle back and forth.
  44.     {
  45.     if (pos > 61)
  46.     pos -= 1;
  47.     myservo.write(pos);
  48.     }
  49.    
  50.    
  51.     //#################
  52.         // Reads the values from the photoresistors to the Left and Right variables.
  53.     UP = analogRead(inputPhotoUP);
  54.     DOWN = analogRead(inputPhotoDOWN);
  55.  
  56.     // Checks if right is greater than left, if so move to right.
  57.     if (UP > (DOWN +10))
  58.     // +20 is the deadzone, so it wont jiggle back and forth.
  59.     {
  60.     if (pos2 < 109)
  61.     pos2++;
  62.     myservo2.write(pos2);
  63.     }
  64.     // Checks if left is greater than right, if so move to left.
  65.     if (DOWN > (UP +10))
  66.     // +20 is the deadzone, so it wont jiggle back and forth.
  67.     {
  68.     if (pos2 > 51)
  69.     pos2 -= 1;
  70.     myservo2.write(pos2);
  71.     }
  72.     // Added some delay, increase or decrease if you want less or more speed.
  73.     delay(10);
  74.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement