hi i have asked a couple of times fro help but just getting more confused. i will explain in the hope some one can help.
i have an old r/c car the motor is connected to motor 1 on an adfruit motor shield
the steering is a servo connected to servo2 on the motor shield
connected to analog input 0 is the left ldr and the right ldr is connected to analog input 1
all the bits work individually i have used a sketch that showed me the ldrs go from 200 when blocked to around 800 in light.
i just can not get it all to work together i have tried lots of ideas from the net but i get all sorts of problems.
like servo keeps pulsing left or right or ldrs get ignored.
the servo needs to move between 70 and 120 degrees to steer OK. what i am hoping to do is the car will go forward until the light on one side is brighter the car will then turn towards the light.please remember this is my first project and i am very confused.so please keep answers very simple.the latest code i am using just bounces the servos left to right all the time and takes no notice of sensors.
please please can some one help me here is the code i am using now i dont know how to put it in a window so just cut and pasted it here
#include <servo.h>
// create servo object to control a servo
// a maximum of eight servo objects can be created
#include <AFMotor.h>
#include <Servo.h>
AF_DCMotor motor(1);
Servo myservo;
int pos = 0; // variable to store the servo position
int inputPhotoLeft = 1; // Easier to read, instead of just 1 or 0.
int inputPhotoRight = 0;
int Left = 0; // Store readings from the photoresistors.
int Right = 0; // Store readings from the photoresistors.
#define CENTER 95 // servo at center
#define LEFT 70 // servo turn left
#define RIGHT 120 // servo turn right
void setup()
{
myservo.attach(9);
myservo.write(CENTER);
}
void loop()
{
// Reads the values from the photoresistors to the Left and Right variables.
Left = analogRead(inputPhotoLeft);
Right = analogRead(inputPhotoRight);
// Checks if right is greater than left, if so move to right.
if (Left > (Right +20))
// +20 is the deadzone, so it wont jiggle back and forth.
{
if (pos < 100)
pos++;
myservo.write(LEFT);
}
// Checks if left is greater than right, if so move to left.
if (Right > (Left +20))
// +20 is the deadzone, so it wont jiggle back and forth.
{
if (pos > 80)
pos -= 1;
myservo.write(RIGHT);
}
// Checks if left and right the same go straight.
if (Right = (Left))
{
pos -= 1;
myservo.write(CENTER);
// Added some delay, increase or decrease if you want less or more speed.
delay(10);
}
unsigned char pos;
char i;
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
thank you in advance
Al