differential motion using stepper motor

Hey Iam trying to create a swarm using tiny micromouse bots. I am using high torque bipolar stepper motors with 1.8 deg stepping angle which is driven using a A3982 driver which has a direction, step input and mode(half and full stepping) select pins. I am testing out the setup using a regulated power supply of 12V(250mA) but will use a LiPo battery which can deliver 720maH. The current problem is that the motors have a jerking motion when i power them up and then resume their motion only when i reset the board. Also sometimes one of the motor does not rotate at all or the motor starts operating after the completion of operation of the other. I have tested these motors with the example codes of accel stepper motors and they appear fine but when i make changes in these codes they exhibit these problems. Kindly help me with the coding part. I want my bot to move forward,left and right directions. I have attached a code that iam using. Kindly point out what I am doin wrong.

//This is an example of how you would control 2 steppers at the same time

#include <AccelStepper.h>

int motorSpeed = 100; //maximum steps per second (about 3rps / at 16 microsteps)
int motorAccel = 8000; //steps/second/second to accelerate

int motor1DirPin = 4; //left wheels digital pin 2
int motor1StepPin = 3; //digital pin 3
int motor1ModePin= 2;

int motor2DirPin = 6; //right wheels digital pin 4
int motor2StepPin = 5; //digital pin 5
int motor2ModePin = 7;

//set up the accelStepper intances
//the “1” tells it we are using a driver
AccelStepper stepper1(1, motor1StepPin, motor1DirPin);
AccelStepper stepper2(1, motor2StepPin, motor2DirPin);

void setup(){
Serial.begin(9600);

digitalWrite(motor1ModePin,HIGH);
digitalWrite(motor2ModePin,HIGH);

stepper1.setMaxSpeed(motorSpeed);
stepper2.setMaxSpeed(motorSpeed);

stepper1.setSpeed(motorSpeed);
stepper2.setSpeed(motorSpeed);

stepper1.setAcceleration(motorAccel);
stepper2.setAcceleration(motorAccel);

}

void loop(){
forward(-100);
delay(20);
// right(50);
// delay(20);
// forward(500);

}

void forward(long dist)
{
stepper1.moveTo(dist); //move 32000 steps (should be 10 rev)
stepper2.moveTo(dist);

while(stepper1.distanceToGo() == 0 && stepper2.distanceToGo() == 0)
{
stepper1.stop();
stepper2.stop();
}

stepper1.run();
stepper2.run();
}

void right(int right)
{
stepper1.moveTo(right); //move 32000 steps (should be 10 rev)
stepper2.moveTo(right);

if (stepper1.distanceToGo() != 0){
stepper1.moveTo(stepper1.currentPosition());

}

if (stepper2.distanceToGo() != 0){
stepper2.moveTo(-stepper2.currentPosition());

}

stepper1.run();
stepper2.run();
}

void left(int left)
{
stepper1.moveTo(left); //move 32000 steps (should be 10 rev)
stepper2.moveTo(left);

if (stepper1.distanceToGo() != 0){
stepper1.moveTo(-stepper1.currentPosition());

}

if (stepper2.distanceToGo() != 0){
stepper2.moveTo(stepper2.currentPosition());

}

stepper1.run();
stepper2.run();
}*/``

... I think you got your code tags backwards... Maybe an edit is in order?

can you please be specific...

anandnr: can you please be specific...

Lok at how your first post is laid out. Then look at how code appears in some other Threads here. Then go back to your first post, click Modify and correct it.

code should look like this

Many people fail to put code tags around their code and, while it is very inconvenient, it is possible to live with it. However the way you have (accidentally, I assume) done things we can't read your question.

...R

here is the new code. It seems to be working fine but i can’t get the bot to stop. I tried stop() but with no success. Can u tell me a better way.

[code][/code]//This is an example of how you would control 2 steppers at the same time

#include <AccelStepper.h>

int motorSpeed = 100; //maximum steps per second (about 3rps / at 16 microsteps)
int motorAccel = 8000; //steps/second/second to accelerate

int motor1DirPin = 4; //left wheels digital pin 2
int motor1StepPin = 3; //digital pin 3
int motor1ModePin= 2;

int motor2DirPin = 6; //right wheels digital pin 4
int motor2StepPin = 5; //digital pin 5
int motor2ModePin = 7;

int xdist=500;
int ydist=100;

//set up the accelStepper intances
//the “1” tells it we are using a driver
AccelStepper stepper1(1, motor1StepPin, motor1DirPin);
AccelStepper stepper2(1, motor2StepPin, motor2DirPin);

void setup(){
Serial.begin(9600);

digitalWrite(motor1ModePin,HIGH);
digitalWrite(motor2ModePin,HIGH);

stepper1.setMaxSpeed(motorSpeed);
stepper2.setMaxSpeed(motorSpeed);

stepper1.setSpeed(motorSpeed);
stepper2.setSpeed(motorSpeed);

stepper1.setAcceleration(motorAccel);
stepper2.setAcceleration(motorAccel);

}

void loop(){
forward(300);
left();

dstop();

}

void forward(int dist)
{
stepper1.setCurrentPosition(100);
stepper2.setCurrentPosition(100);
stepper1.moveTo(dist);
stepper2.moveTo(dist);
while (stepper1.distanceToGo() != 0 && stepper2.distanceToGo() != 0)
{
Serial.println(stepper1.currentPosition());
Serial.println(stepper2.currentPosition());
stepper1.run();
stepper2.run();
}
}

void right()
{
stepper1.setCurrentPosition(100);
stepper2.setCurrentPosition(-100);
stepper1.moveTo(60); //move 32000 steps (should be 10 rev)
stepper2.moveTo(-60);

while (stepper1.distanceToGo() != 0 && stepper2.distanceToGo() != 0)
{
stepper1.run();
stepper2.run();
}
}

void left()
{
stepper1.setCurrentPosition(-100);
stepper2.setCurrentPosition(100);
stepper1.move(-60); //move 32000 steps (should be 10 rev)
stepper2.move(60);

while (stepper1.distanceToGo() != 0 && stepper2.distanceToGo() != 0)
{
Serial.println(stepper1.currentPosition());
Serial.println(stepper2.currentPosition());
stepper1.run();
stepper2.run();
}
}

void dstop()
{
stepper1.setCurrentPosition(100);
stepper2.setCurrentPosition(100);
stepper1.moveTo(100);
stepper2.moveTo(100);
while (stepper1.distanceToGo() != 0 && stepper2.distanceToGo() != 0)
{
Serial.println(stepper1.currentPosition());
Serial.println(stepper2.currentPosition());
stepper1.run();
stepper2.run();
}
}``

Have you taken the trouble to look at your post after you posted it? You still haven't got the code tags right. There needs to be an opening tag [ code] before the code and a closing tag [ /code] after it. I have added a space into these tags so they don't work.

Have you tried controlling one motor just to learn how to use the AccelStepper library. As far as I know the motor just stops when it has moved through the number of steps that you asked it to do. No special commands are needed. If you need fine control you can always tell it to move one step at a time.

...R

Also note there is a modify button above your post so you can change it without posting the whole thing again. Read the how to use the forum sticky post to see how to post code. If you can't post code correctly how on earth are you going to understand any answer you get?

You are getting serial data output. You haven't shared that with us.

You do understand that loop() is called in an infinite loop, don't you?

please bear with my ignorance. I want the bot to move forward,right and left but i want the robot to stop and wait until it recieves further command from the master bot to move. So the robot should stopfor a while until it has recieved a character via a Xbee module and why is the stop() not working? any other way to make the robot halt and wait for the command. Should I not give it within the loop?

#include <AccelStepper.h>

int motorSpeed = 100; //maximum steps per second (about 3rps / at 16 microsteps)
int motorAccel = 8000; //steps/second/second to accelerate

int motor1DirPin = 4; //left wheels digital pin 2
int motor1StepPin = 3; //digital pin 3
int motor1ModePin= 2;

int motor2DirPin = 6; //right wheels digital pin 4
int motor2StepPin = 5; //digital pin 5
int motor2ModePin = 7;

int xdist=500;
int ydist=100;

//set up the accelStepper intances
//the "1" tells it we are using a driver
AccelStepper stepper1(1, motor1StepPin, motor1DirPin);
AccelStepper stepper2(1, motor2StepPin, motor2DirPin);


void setup(){
  Serial.begin(9600);
  
  digitalWrite(motor1ModePin,HIGH);
  digitalWrite(motor2ModePin,HIGH);
  
  stepper1.setMaxSpeed(motorSpeed);
  stepper2.setMaxSpeed(motorSpeed);

  stepper1.setSpeed(motorSpeed);
  stepper2.setSpeed(motorSpeed);

  stepper1.setAcceleration(motorAccel);
  stepper2.setAcceleration(motorAccel);
  
  
}

void loop(){
  forward(300);
  left();
  
  dstop();
  
  
}

void forward(int dist) 
{
  stepper1.setCurrentPosition(100);
  stepper2.setCurrentPosition(100);
  stepper1.moveTo(dist);
  stepper2.moveTo(dist);
  while (stepper1.distanceToGo() != 0 && stepper2.distanceToGo() != 0) 
  {
      Serial.println(stepper1.currentPosition());
  Serial.println(stepper2.currentPosition());
    stepper1.run();
    stepper2.run();
  }
}
 
 
 
void right()
{
  stepper1.setCurrentPosition(100);
  stepper2.setCurrentPosition(-100);
  stepper1.moveTo(60); //move 32000 steps (should be 10 rev)
  stepper2.moveTo(-60);
  
 while (stepper1.distanceToGo() != 0 && stepper2.distanceToGo() != 0) 
  {
    stepper1.run();
    stepper2.run();
  }
}


void left()
{
 stepper1.setCurrentPosition(-100);
  stepper2.setCurrentPosition(100);
  stepper1.move(-60); //move 32000 steps (should be 10 rev)
  stepper2.move(60);
  
 while (stepper1.distanceToGo() != 0 && stepper2.distanceToGo() != 0) 
  {
  Serial.println(stepper1.currentPosition());
  Serial.println(stepper2.currentPosition());
    stepper1.run();
    stepper2.run();
  }
}

 void dstop()
 {
  stepper1.setCurrentPosition(100);
  stepper2.setCurrentPosition(100);
  stepper1.moveTo(100);
  stepper2.moveTo(100);
  while (stepper1.distanceToGo() != 0 && stepper2.distanceToGo() != 0) 
  {
      Serial.println(stepper1.currentPosition());
      Serial.println(stepper2.currentPosition());
    stepper1.run();
    stepper2.run();
  }
 }

You have been asked to post your code. Orrery twice and you have not. You have been asked about the serial output but you say nothing.

If you are not going to cooperate how can you expect people to help you?