Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Servo.h> // include Servo library
- Servo horizontal; // horizontal servo
- int servoh = 90; // stand horizontal servo SET SERVO POSITIONS VARIABLES TO MID-POINT
- Servo vertical; // vertical servo
- int servov = 90; // stand vertical servo
- // LDR pin connections
- // name = analogpin; defining LDR input pin names+attaching to physical pins
- int ldrLT = 0; //LDR top left
- int ldrRT = 1; //LDR top right
- int ldrLD = 2; //LDR down left
- int ldrRD = 3; //ldr down right
- void setup()
- {
- Serial.begin(9600); //opens serial port, sets data rate to 9600 bits/s
- // servo connections
- // name.attach(pin);
- horizontal.attach(9); //attach physical digital pins to servos (PWM~ pins 9 and 10)
- vertical.attach(10);
- }
- void loop()
- {
- int LeftTop = analogRead(ldrLT); // top left
- int RightTop = analogRead(ldrRT); // top right
- int LeftDown = analogRead(ldrLD); // down left
- int RightDown = analogRead(ldrRD); // down right
- int dtime = analogRead(4)/20; // read potentiometers - dtime is for how often to check difference
- int tol = analogRead(5)/4; //read potentiometers - tol is tolerance for how far off the LDRs can be
- int avt = (LeftTop + RightTop) / 2; // average value top
- int avd = (LeftDown + RightDown) / 2; // average value down
- int avl = (LeftTop + LeftDown) / 2; // average value left
- int avr = (RightTop + RightDown) / 2; // average value right
- int dvert = avt - avd; // check the difference of up and down
- int dhoriz = avl - avr;// check the difference of left and right
- if (-1*tol > dvert || dvert > tol) // check if the diffirence is within the tolerance else change vertical angle
- {
- if (avt > avd)
- {
- servov = ++servov;
- if (servov > 180)
- {
- servov = 180;
- }
- }
- else if (avt < avd)
- {
- servov= --servov;
- if (servov < 0)
- {
- servov = 0;
- }
- }
- vertical.write(servov);
- }
- if (-1*tol > dhoriz || dhoriz > tol) // check if the diffirence is within the tolerance else change horizontal angle
- {
- if (avl > avr)
- {
- servoh = --servoh;
- if (servoh < 0)
- {
- servoh = 0;
- }
- }
- else if (avl < avr)
- {
- servoh = ++servoh;
- if (servoh > 180)
- {
- servoh = 180;
- }
- }
- else if (avl == avr)
- {
- // nothing
- }
- horizontal.write(servoh);
- }
- delay(dtime);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement