hi i am going around and around in circles and not getting very far
this is my first project i will outline what i am trying to do
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 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
please please can some one help me
Al
1: Stop for a sec
2: Take a deep breath
3: Post the individual codes and your fully assenbled code(how do you want us to help if there is no code)
is the left ldr and the right is connected to analog input 1
What is a idr?
ok sorry just been at it all day and getting nowhere i will post the best code i have so far haven got to the drive motor part yet still trying to get it to turn left and right.
i bet it a simple mistake but i have looked and looked and can not find it.
hope i get it sorted soon then i can work things out myself
new_servo_steering_requires_tuneing.ino (1.13 KB)
forgot not sure if i posted code right so will paste it here as well
#include <Servo.h>
#include <AFMotor.h>
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.
void setup()
{
myservo.attach(9); // Attach servo to pin 9.
}
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 < 179)
pos++;
myservo.write(pos);
}
// 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 > 1)
pos -= 1;
myservo.write(pos);
}
// Added some delay, increase or decrease if you want less or more speed.
delay(10);
}
the left and right ldrs are connected to separate analog inputs 0,1 i have monitored them with the serial monitor and both working ok
the ldr is a light dependent resistor
just to be sure. have you checked, if the servo control works?
btw. to post code you start with [ code] and end with [ /code].
does this code, what it should do?
#include <Servo.h>
#include <AFMotor.h>
Servo myservo;
void setup()
{
myservo.attach(9); // Attach servo to pin 9.
myservo.write(0);
delay(500);
myservo.write(90);
delay(500);
myservo.write(180);
delay(500);
myservo.write(90);
delay(500);
}
Kevin
Yes, first testing out the servo is a good idea.
int inputPhotoLeft = 1; // Easier to read, instead of just 1 or 0.
int inputPhotoRight = 0;
Left = analogRead(inputPhotoLeft);
Right = analogRead(inputPhotoRight);
Are your ldr connected to pins 0 and 1 or are they connected to ANALOG 0 and 1? Second, your InputServoLeft and InputServoLeft are only used once and for me because it is a simple code, to make it easier to read just put:
Left = analogRead(A1);
Right = analogRead(A0);
Besides that, your code seems fine to me...
Wait a second. I think I just found why It doesnt work. Suppose Right is 70 and Left is 80.
if (Left > (Right +20))
Returns false
if (Right > (Left +20))
Returns False
Yes the servo works ok got it to sweep left to right ok just wher I run the program above it keeps jumping left whichever ldr is covered
Tried a secktch that wrote to the serial Moniter and both ldrs work great and read low when covered and high when light is on them
@Theelectronicguy: Than the motor would not move, but that is intended. (deadzone)
Use your Serial for debugging
Serial.write ("L:");
Serial.write (Left);
Serial.write (" R:");
Serial.write (Right);
Serial.write (" - ");
Serial.writeln (pos);
delay(500); // for easier reading