Confusion about wheel movement.

Greetings. I had a quick question about trying to get an Arduino car to move. I am running this code

// Turn on the Sensor to begin finding my location then turn
// on the servo motor to determine position
// Then turn on stepper motors to begin having the car move

#include <Servo.h>;
#include <Stepper.h>;
const int PingPin = 12;
const int ledPin = 13;

Servo myservo;    //create servo object to control a servo

//int angle = 0;   //variable to store the servo position

const int stepsPerRevolution = 200; //change this to fit the number of steps per revolution
int cm;
Stepper myStepper(stepsPerRevolution, 3, 4);
Stepper myStepper2(stepsPerRevolution, 5, 6);
void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  myservo.attach(7);
  myStepper.setSpeed(40);
}

void loop()
{
  //Declare this as the main loop of the entire program
  //Turn on the servo motor to determine location
  //Servo myservo;  //create servo object to control a servo
  int angle = 0;
  //myservo.attach(7);
  //Serial.begin(9600);
  Serial.println("SendA");

  for (angle = 0; angle < 180; angle += 1);
  {
    myservo.write(angle);
    delay(10);
  }
  for (angle = 180; angle >= 1; angle -= 1)
  {
    myservo.write(angle);
    delay(10);
  }
  
  Serial.println("You have succeeded up to this point");


  Serial.println("Now on to the Sensor");


  int cm = ping(12);
  Serial.println(cm);
  digitalWrite(13, HIGH);
  delay(cm * 10 ); //each centimeter adds 10 milliseconds delay
  digitalWrite(ledPin, LOW);
  delay( cm * 10);
  //return cm;
  
// Activate stepper motors to move foward
// then begin looping if else statements
 if (cm <= 50) { 
   Serial.println("counterclockwise");
 myStepper.step(-stepsPerRevolution);
 myStepper2.step(-stepsPerRevolution);
 delay(3000);
 }
 else
 {
 Serial.println("clockwise");
 myStepper.step(stepsPerRevolution);
 myStepper2.step(stepsPerRevolution);
 delay(3000);
 }
  
}

int ping(int PingPin)
{
  long duration, cm;

  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  delayMicroseconds(2);
  digitalWrite(ledPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(ledPin, LOW);

  pinMode(PingPin, INPUT);
  duration = pulseIn(PingPin, HIGH);

  //convert the time into a distance
  cm = microsecondsToCentimeters(duration);
  return cm;
}
long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

And when I plug my battery pack into it, the wheels will only move in one direction. But the stranger thing is that one wheel will constantly be powered, while one will only be powered sporadically. Is it something to do with my code or my wiring. I have the two stepper motors connected up to a L298N Dual H-bridge for anyone that is curious. Thank you for the help.

Have a look at stepper motor basics. The L298 is a poor choice for driving stepper motors.

Post a link to the datasheet for the steppers you are using.
What sort of battery are you using?
Have you separate power supplies for the Arduino and the motors?

...R

And you can't drive two steppers from a dual H-bridge since each stepper uses
2 H-bridges.

If the stepper is low-impedance then you need a current driver not a L298

I am running both motors on a 5v battery pack connected to a breadboard. The wires are indicated by the code. Thank you for the help.

But you expect us to guess without vital information such as the details of the motor, or the
specs of the battery pack (never seen a 5V battery - 3.7V, 4.8V, 6.0V, 7.2V yes) What
capacity / chemistry?

The motors are just steppers that were brought from Radioshack. The battery pack is 5v holding 6 AA batteries. The wires are soldered to the motor and the motors are connected to one L298N H-bridge. The wiring for the VCC slot is connected to IOREF on the Arduino. The GND is wired to The breadboard with a wire connecting to the GND on the Arduino Uno. The same principal applies for the 5v pin. The servo motor and the sensor are also both connected to the Arduino Uno. When I turn on the car using the battery pack, the program will run as indicated by the code, but when it comes time for the steppers to move, only one of them will consistently move, the other one stops every now and again. Im not sure if it has something to do with the code or the wiring. Bascially where I am confused is that if I have both motors running on one h-bridge and they both are recieving enough power to move, why does one periodically stop?

You need to find out the details of the steppers - winding resistance, motor size, number of
wires...

Geeetech Smart Car Tire/Wheel with Gear Motor for arduino/LPC/AVR/ARM car project

http://www.amazon.com/Geeetech-Smart-Wheel-arduino-project/dp/B0081C5B80

The motors have two points for wiring.

TheOneReborn:
Geeetech Smart Car Tire/Wheel with Gear Motor for arduino/LPC/AVR/ARM car project

http://www.amazon.com/Geeetech-Smart-Wheel-arduino-project/dp/B0081C5B80

That is a link to a DC motor with a gearbox. I have a few of them myself.
(perhaps now you understand why you needed to post a link)

It would be quite appropriate to control those with an L298.

Where did you get the idea they are stepper motors?

...R

I assumed since they were both being powered by the Arduino that they were steppers.
Update:
With this code:

// Turn on the Sensor to begin finding my location then turn
// on the servo motor to determine position
// Then turn on stepper motors to begin having the car move

#include <Servo.h>;
#include <Stepper.h>;
const int PingPin = 12;
const int ledPin = 13;

Servo myservo;    //create servo object to control a servo

//int angle = 0;   //variable to store the servo position

const int stepsPerRevolution = 200; //change this to fit the number of steps per revolution
int cm;
Stepper myStepper(stepsPerRevolution, 3, 4, 5, 6);
//Stepper myStepper2(stepsPerRevolution, 5, 6);
void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  myservo.attach(7);
  myStepper.setSpeed(40);
}

void loop()
{
  //Declare this as the main loop of the entire program
  //Turn on the servo motor to determine location
  //Servo myservo;  //create servo object to control a servo
  int angle = 0;
  //myservo.attach(7);
  //Serial.begin(9600);
  Serial.println("SendA");

  for (angle = 0; angle < 180; angle += 1);
  {
    myservo.write(angle);
    delay(10);
  }
  for (angle = 180; angle >= 1; angle -= 1)
  {
    myservo.write(angle);
    delay(10);
  }
  
  Serial.println("You have succeeded up to this point");


  Serial.println("Now on to the Sensor");


  int cm = ping(12);
  Serial.println(cm);
  digitalWrite(13, HIGH);
  delay(cm * 10 ); //each centimeter adds 10 milliseconds delay
  digitalWrite(ledPin, LOW);
  delay( cm * 10);
  //return cm;
  
// Activate stepper motors to move foward
// then begin looping if else statements
 if (cm <= 50) { 
   Serial.println("counterclockwise");
   //digitalWrite(3, HIGH);
   //digitalWrite(4, LOW);
   //digitalWrite(5, LOW);
   //digitalWrite(6, LOW);
   //Serial.println("counterclockwise");
 myStepper.step(-stepsPerRevolution);
 //myStepper2.step(-stepsPerRevolution);
 //digitalWrite(3, LOW);
 //digitalWrite(4, HIGH);
 //digitalWrite(5, LOW);
 //digitalWrite(6, LOW);
 delay(1000);
 }
 else
 {
   Serial.println("clockwise");
   //digitalWrite(3, LOW);
   //digitalWrite(4, LOW);
   //digitalWrite(5, HIGH);
   //digitalWrite(6, LOW);
 //Serial.println("clockwise");
 myStepper.step(stepsPerRevolution);
 //myStepper2.step(stepsPerRevolution);
 //digitalWrite(3, LOW);
 //digitalWrite(4, LOW);
 //digitalWrite(5, LOW);
 //digitalWrite(6, HIGH); 
 delay(1000);
 }
  
}

int ping(int PingPin)
{
  long duration, cm;

  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  delayMicroseconds(2);
  digitalWrite(ledPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(ledPin, LOW);

  pinMode(PingPin, INPUT);
  duration = pulseIn(PingPin, HIGH);

  //convert the time into a distance
  cm = microsecondsToCentimeters(duration);
  return cm;
}
long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

I get both wheels to move consistently, but only in one direction. No matter how far away the sensor percieves an object it will still only move the wheels counterclockwise. At this point I do not think that it is anything dealing with the wiring, rather it is a problem with the code. Does this look right to you?

It is hardly surprising that the motors do not work properly when you are trying to control DC motors with the stepper library.

The most surprising thing is that they work at all.

Take ALL references to stepper motors out of the code and start again. Look for examples for driving DC motors using PWM.

...R