Pages: [1]   Go Down
Author Topic: total lost on first project need help please  (Read 458 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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   
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if (Right = (Left))

This is an assignment, not a comparison. As it always evaluates to TRUE, you are moving the servo to the centre position every time through loop().
It should say:
Code:
if (Right == Left)

Code:
if (pos < 100)
    pos++;

Which pos do you mean?

This global one:
Code:
int pos = 0;    // variable to store the servo position
or this one, local to loop():
Code:
unsigned char pos;
which is defined after pos is first used.

I don't understand what pos is used for.
« Last Edit: June 24, 2012, 09:14:50 am by dxw00d » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i am confused the first bit suggested i have changed so it now reads if (Right == Left) that has made it a bit better but now servo just keeps trying to go left.
as to the "  if (pos < 100) pos++;  " bit i am not sure about it at all as i have put several sketches together to get this far so any thing any one can suggest would be of great help.
Al
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6146
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

as to the "  if (pos < 100) pos++;  " bit i am not sure about it at all as i have put several sketches together to get this far so any thing any one can suggest would be of great help.
Delete the lines:
unsigned char pos;
char i:
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thank you its getting better all the time but when right ldr is covered it turns left as i wanted it to.
but it will not turn the wheels right when cover the left ldr also the servo keeps buzzing not stoping when it reaches its position.
but there is light at the end of the tunnel now please advise
Al
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

sorry made a mistake it keeps wanting to turn right but only turns left when right ldr covered
what i am trying for is to go straight with no ldrs covered and right if left covered and left if right covered hope that makes sense
Al
Logged

Pages: [1]   Go Up
Jump to: