Help on moving forward, stopping and then moving forward again

Hi everybody,

The main idea for my project is to have my robot move forward 30cm, then stop and perform its desired task (this doesn't involve programming and should take about 15 seconds). Once it has completed this task I need it to move forward once again and then stop after 10cm. After it stops for the second time I need it to move right until it reaches the edge of a table which will be known by using a HC-SR04 ultrasonic sensor. I am using Mecanum wheels so moving right won't require rotation. The main issues I'm facing are I only want certain tasks like moving forward and right to be performed once but I want the sensor to constantly update as fast as possible.

I have included a photo of my schematic which includes one of four of stepper motors and one of four of my sensors.

I am trying to use the Accelstepper library for this project.

Thanks to everybody in advance for the replies.

I hope that you are not actually planning to use a PP3 battery

How will you be measuring the distances moved ?

Hi

No, I am not planning on using that battery that was just the only battery on the software I was told to use.

I am using this to move the required distance I'm not sure if it is the correct way to do it though

stepperMotor1.moveTo(30);
  stepperMotor2.moveTo(30);
  stepperMotor3.moveTo(30);
  stepperMotor4.moveTo(30);
  while ((stepperMotor1.distanceToGo > 0) && (stepperMotor2.distanceToGo > 0) && (stepperMotor3.distanceToGo > 0) && (stepperMotor4.distanceToGo > 0)) {
    stepperMotor1.run();
    stepperMotor2.run();
    stepperMotor3.run();
    stepperMotor4.run();
	}

Hi,
It would be better if you used a pen(cil) and paper and drew your circuit diagram and post an image of it, including pin labels and part names.

The picture you have is not complete nor accurate.

Thanks.. Tom.. :slight_smile:
PS, Karma for reading the howto and using code tags.

paxton1508:
I am using this to move the required distance I'm not sure if it is the correct way to do it though

Please post the complete program.

...R

This is the program so far, I stopped when I realised the issue of the forward movement commands constantly repeating and not being sure how to stop the right movement.

#include <accelstepper.h>

// The top left (back left) stepper motor = 1, top right (front left) = 2, bottom left (back right) = 3, bottom right (front right) = 4

// This area of set up is for the stepper motors
const int motor1Step = 5
const int motor1Dir = 6
const int motor1Ena = 4
const int motor2Step = 2
const int motor2Dir = 3
const int motor2Ena = 1
const int motor3Step = 49
const int motor3Dir = 48
const int motor3Ena = 42
const int motor4Step = 47
const int motor4Dir = 46
const int motor4Ena = 44
  
// This area of set up is for the ultrasonic distance sensors 
// 1 is for the left side sensor, 2 is for the right side sensor and 3 is for the back sensor used to find the 2 ball tube
long duration1; // This is in microseconds 
int distance1; // This is in centremetres
long duration2;
int distance2;
long duration3;
int distance3;
long duration4;
int distance4;
const int triggerPin1 = 40
const int echoPin1 = 38
const int triggerPin2 = 
const int echoPin2 = 
const int triggerPin3 = 
const int echoPin3 = 
const int triggerPin4 = 
const int echoPin4
  
void setup () {
  
// This area of set up is for the stepper motors
digitalWrite (motor1Ena,HIGH) // This is so the motors originally start off
digitalWrite (motor2Ena,HIGH)
digitalWrite (motor3Ena,HIGH)
digitalWrite (motor4Ena,HIGH)
  
// This area of set up is for the ultrasonic distance sensors
  pinMode (triggerPin1, OUTPUT);
  pinMode (echoPin1, INPUT);
  pinMode (triggerPin2, OUTPUT);
  pinMode (echoPin2, INPUT);
  pinMode (triggerPin2, OUTPUT);
  pinMode (echoPin2, INPUT);
  
}

void loop  () {
  
  //This is used to clear the trigger
  digitalWrite (triggerPin1, LOW);
  digitalWrite (triggerPin2, LOW);
  digitalWrite (triggerPin3, LOW);
  delay (2);
  
  digitalWrite (triggerPin1, HIGH);
  digitalWrite (triggerPin2, HIGH);
  digitalWrite (triggerPin3, HIGH);
  delay(10); // This delay is required to send out a signal long enough for it to return to the echo p
  digitalWrite (triggerPin1, LOW);
  digitalWrite (triggerPin2, LOW);
  digitalWrite (triggerPin3, LOW);
  
  duration1 = pulseIn (echoPin1, HIGH);
  distance1 = duration1*0.01723; // Speed = distance/time formula used
  duration2 = pulseIn (echoPin2, HIGH);
  distance2 = duration1*0.01723;
  duration3 = pulseIn (echoPin3, HIGH);
  distance3 = duration1*0.01723;
  duration4 = pulseIn (echoPin4, HIGH);
  distance4 = duration1*0.01723;
  
  //If statement used to check the robot won't move off the edge of the table and isn't near a tube
    if (distance1<5 && distance2<5 && distance3<5 && distance4<5){
      //This is the distance to the first tube (4 balls) the position of the track that schultz designs will affect this value
      stepperMotor1.moveTo(30);
      stepperMotor2.moveTo(30);
      stepperMotor3.moveTo(30);
      stepperMotor4.moveTo(30);
      while ((stepperMotor1.distanceToGo > 0) && (stepperMotor2.distanceToGo > 0) && (stepperMotor3.distanceToGo > 0) && (stepperMotor4.distanceToGo > 0)) {
        stepperMotor1.run();
        stepperMotor2.run();
        stepperMotor3.run();
        stepperMotor4.run();
      }

      //This is the distance to the point at which the robot starts moving right
      stepperMotor1.moveTo(10);
      stepperMotor2.moveTo(10);
      stepperMotor3.moveTo(10);
      stepperMotor4.moveTo(10);
      while ((stepperMotor1.distanceToGo > 0) && (stepperMotor2.distanceToGo > 0) && (stepperMotor3.distanceToGo > 0) && (stepperMotor4.distanceToGo > 0)) {
        stepperMotor1.run();
        stepperMotor2.run();
        stepperMotor3.run();
        stepperMotor4.run();
      }
      //This is the movement of the car in the right direction until it reaches the edge of the table
      stepperMotor2.run();
      stepperMotor4.run();
	}
  
}

Thanks for all the help

Sorry, I can't hand draw my schematic at the moment. Here is the complete version though with a zoomed in version of the Arduino mega board. The DC motors can be ignored in this thread they are going to be used for a later task.

Thanks

Images from Reply #6 so we don't have to download them. See this Simple Image Posting Guide

...R

I suspect this code

while ((stepperMotor1.distanceToGo > 0) && (stepperMotor2.distanceToGo > 0) && (stepperMotor3.distanceToGo > 0) && (stepperMotor4.distanceToGo > 0)) {

should be using OR rather than AND

As it is I expect it will stop as soon as any one of the motors reaches 0.

...R

That is true thanks for pointing that out I have changed it now.

Any advice on how to make sure those sets of movement only happen once?

At the moment since it is in the void loop my understanding is it will continue to happen repeatedly as the loop is repeated.

The only task that I want to be repeated is the sensor detecting the table underneath it.

paxton1508:
Any advice on how to make sure those sets of movement only happen once?

At the moment since it is in the void loop my understanding is it will continue to happen repeatedly as the loop is repeated.

The usual way to do that is to have a variable that counts how many time something happens. Something like

if (numRepeats < maxRepeats) (
   makeMotorMove();
   numRepeats ++;
}

If you put code into a function and call the function from loop() it makes it much easier to manage things. Have a look at Planning and Implementing a Program

...R

Ok sounds good thanks for all your help