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.
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();
}
}
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.
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