James robot driving me crazy

Screenshot_2015-04-26-18-50-51.jpg (194165Bytes)

hey guys i am seriously losing my mind. I am very eager to leanr and improve. I seen this project and i really wanted to build it. Its been like two months and i still am.having issues.

i am using his code and i believe i have it connected proper. issue is left dc motor just spins as soon as power is applied. servo doesnt work... sometimes it pulses. and ping who knows. i am at the end of.my rope. i am so stressed out. i am.hoping i could find guidance here. i will.post sketch and the pic of the setup. please work.with me to get this going. im really interested in making it work. ill have to use my laptop to.add code im on my phone right now. but if your familiar with it its the james robot

thank you guys i look forward to.learning

code


Step 9: Connecting the Ultrasonic Sensor


Next connect some female Breadboard cables up to the Ultrasonic sensor.
Then using some male cables connect the Trigger pin of the sensor up to Arduino pin 3 and the Echo to pin 2. You will also need to connect the servo to the Arduino, do this by connecting the voltage in of the Servo up to the 5V rail, the ground to the ground rail and the signal wire to Arduino pin 4.

Step 10: Power


To power my DC Motors I am using a 4.8V Ni-Cd rechargeable battery pack. The V gets connected to the Motor V on the L293D chip and the ground up to the Arduino ground rail.
Next connect a 9V battery up to the Arduino to power it.
 
 

Step 11: Code


Here is some basic code to get you started. I strongly encourage you to edit it and make it better once you have learned how it works.
To make the use of the ultrasonic sensor easier I have used the new ping library.
Download link for the sketch can be found at the bottom.

//Since we are using servos and ultrasonic sensors in the robot we will include some libraries written to make their use easier
#include <Servo.h>
#include <NewPing.h>

//Below are the symbolic constants. Instead of having to type in a non-sensical pin number each time we want to do something we can write an easy to understand name which represents the pin, the compiler will then replace the names with the numbers
#define LeftMotorForward 2
#define LeftMotorBackward 3
#define RightMotorForward 5
#define RightMotorBackward 4
#define USTrigger 8
#define USEcho 9
#define MaxDistance 100
#define LED 13

//Here we have created two ‘objects’, one for the servo and one for the ultrasonic sensorServo servo;
NewPing sonar(USTrigger, USEcho, MaxDistance);

//Below we are creating unsigned integer variables which we will use later on in the code. They are unsigned as they will only have postive valuesunsignedint duration;
unsignedint distance;
unsignedint FrontDistance;
unsignedint LeftDistance;
unsignedint RightDistance;
unsignedint Time;
unsignedint CollisionCounter;

voidsetup() //This block happens once on startup
{
  Serial.begin(9600); //I have included the serial initialize but commented it out, if you want to debug and print information to the serial monitor just uncomment//Here we are setting the pin modes. As we will sending out signals from the pins we set them as outputspinMode(LeftMotorForward, OUTPUT);
  pinMode(LeftMotorBackward, OUTPUT);
  pinMode(RightMotorForward, OUTPUT);
  pinMode(RightMotorBackward, OUTPUT);
  pinMode(LED, OUTPUT);
  servo.attach(6); //The servo is attached to pin 4
}

voidloop() //This block repeats itself while the Arduino is turned on
{
  servo.write(90); //Rotate the servo to face the front
  scan();                                             //Go to the scan function
  FrontDistance = distance;                           //Set the variable FrontDistance to the value of the distance returned from the scan functionSerial.println("Front distance = ");
  Serial.print(distance);
  if(FrontDistance > 40 || FrontDistance == 0) //If there is nothing infront of the robot within 40cm or the distance value is 0 (which for the newping libary means no ping was returned) then…
  {
   moveForward();                                     //Go to the moveForward function
  } 
  else//Else (if there is something infront of the robot within 40cm) then…
  {
    CollisionCounter = CollisionCounter + 1;
    moveStop();                                       //Go to the moveStop function
    navigate();
  }
}

void moveForward() //This function tells the robot to go forward
{
  Serial.println("");
  Serial.println(“Moving forward”);
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(RightMotorForward, HIGH);
}

void moveBackward() //This function tells the robot to move backward
{
  Serial.println("");
  Serial.println(“Moving backward”);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(RightMotorBackward, HIGH);
}

void moveLeft() //This function tells the robot to turn left
{
  Serial.println("");
  Serial.println(“Moving left”);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(LeftMotorBackward, HIGH);
  digitalWrite(RightMotorBackward, LOW);
  digitalWrite(RightMotorForward, HIGH);
  
}

void moveRight() //This function tells the robot to turn right
{
  Serial.println("");
  Serial.println(“Moving right”);
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(LeftMotorForward, HIGH);
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(RightMotorBackward, HIGH);
}

void moveStop() //This function tells the robot to stop moving
{
  Serial.println("");
  Serial.println(“Stopping”);
  digitalWrite(LeftMotorBackward, LOW);
  digitalWrite(LeftMotorForward, LOW);
  digitalWrite(RightMotorForward, LOW);
  digitalWrite(RightMotorBackward, LOW);
}
void scan() //This function determines the distance things are away from the ultrasonic sensor
{
  Serial.println("");
  Serial.println(“Scanning”);
  Time = sonar.ping();
  distance = Time / US_ROUNDTRIP_CM;
  delay(500);
}
void navigate()
{
    Serial.println(“There’s an obstacle!”);
    servo.write(167); //Move the servo to the left (my little servos didn’t like going to 180 so I played around with the value until it worked nicely)delay(1000); //Wait half a second for the servo to get there
    scan();                                           //Go to the scan function
    LeftDistance = distance;                          //Set the variable LeftDistance to the distance on the leftSerial.println("Left distance = ");
    Serial.print(distance);
    servo.write(0); //Move the servo to the rightdelay(1000); //Wait half a second for the servo to get there
    scan();                                           //Go to the scan function
    RightDistance = distance;                         //Set the variable RightDistance to the distance on the rightSerial.println("Right distance = ");
    Serial.print(distance);
    if(abs(RightDistance - LeftDistance) < 5)
    {
      moveBackward();                                  //Go to the moveBackward functiondelay(200); //Pause the program for 200 milliseconds to let the robot reverse
      moveRight();                                     //Go to the moveRight functiondelay(100); //Pause the program for 200 milliseconds to let the robot turn right
    }
    elseif(RightDistance < LeftDistance) //If the distance on the right is less than that on the left then…
    {
     moveLeft();                                      //Go to the moveLeft functiondelay(100); //Pause the program for half a second to let the robot turn
    }
    elseif(LeftDistance < RightDistance) //Else if the distance on the left is less than that on the right then…
    {
     moveRight();                                     //Go to the moveRight functiondelay(100); //Pause the program for half a second to let the robot turn
    }
}

so my issues are my servo is not working proper my ping sensor isnt either and only the left motor spins.constantly. any thoughts or ideas?

Robot problems
1. I suspect your left motor problem is because LeftMotorForward is not setup as an OUTPUT.
2. The cheap ($3.00US) sonar modules are completely useless.
3. The ping and servo libraries both use hardware time. I do not know which or if there is a conflict.

I would suggest that you separate the program into 3 individual programs (motor, servo, & sonar) and debug each module independently. I would also suggest you draw the entire electronics using a schematic capture program. There are several free tool sets available.

Once you get the motor rotation working you will probably need to learn about PWM to control the speed to something other than full on/ff.

Better to power the servo

Better to power the servo from the 4.8v battery, rather than arduino. The 9v battery goes flat in 10min if the servo stalls. I’ve been fine with HC-SR04, as cheap ultrasonic sensors.

robot james

ok so i will fix the code with the left motor forward output.

my setup is in the screen shot attachment

Does my wiring look right?

I will try powering servo from battery pack. also i tried other sketchs and the ping sensor was working.

One thing about your code:
One thing about your code: please keep lines to about 80 characters.

On my screen (Safari, iPad) your code takes the full width of the screen but everything is so small I can’t read it. I don’t know if others have this problem.

WAY OT :smiley:

If you are paying $3 for an ultrasonic sensor, you are paying too much . :stuck_out_tongue:

Stoopid hooman question.

Can you really get away with “voidsetup()” and “voidloop()”, or, are the spaces just missing from this copy of the code?

Are you sure of two things, sort of already mentioned, do you have the motor driver chip oriented correctly and do you have the connections then made properly?

The way your code reads it would seem the Enable pins are both tied high. You might do better with a couple of NOT gates feeding the two inputs for each motor and then as was pointed out PWM on the enable while using a single digital pin for each motor to tell it forward or reverse. Your code will change a fair bit, but, you will have more control over your platform. You will even be able to almost get it to drive straight a majority of the time. Motors are most of the reason for the arc, and the flatter the batteries get the more the arc will return.

hey man thank you so

hey man thank you so much. 

 

i am really new and learning. can you help me setup step by step

ill take everything apart. and first do motors and could you help

with code for that.

im really frusterated because i am not 

making progress so i would like

to start small and learn.

thank you

solved

I am almost too embarassed to disclose what was wrong.

the protoboard pins 78910111213 were bent when i attached so it looked like the were connected…

thanks guys now i can progress lol