My robot don't move !
Hello here !
I have a problem on the robot that I just built. The robot moves with the help of two stepper motor and detect obstacles using two sensors Ultrasonic.
For now, the robot should just be able to avoid obstacles. Then I could possibly add floors to add other functions as the return has a base for example.
So I try the robot and... nothing. The robot does not move exept one impulses (step ?) per second and per motor and theserial monitor (which gives me the distance of the two sensors) displays only the value of the left distance, the right distance is always equal to 0. I tested all the components separately with test codes and it all works correctly so no worries on that side.
I wonder if the problem is the robot itself (power, connection, ...) or his code.
So if you have an idea of the source of the probleme, please write it in the comment ;)
Here is the code :
#define trigPinD 2
#define echoPinD 3
#define trigPinG 4
#define echoPinG 5
//declare variables for the motor pins
int motorPin1D = 6; // Blue - 28BYJ48 pin 1
int motorPin2D = 7; // Pink - 28BYJ48 pin 2
int motorPin3D = 8; // Yellow - 28BYJ48 pin 3
int motorPin4D = 9; // Orange - 28BYJ48 pin 4
int motorPin1G = 10; // Blue - 28BYJ48 pin 1
int motorPin2G = 11; // Pink - 28BYJ48 pin 2
int motorPin3G = 12; // Yellow - 28BYJ48 pin 3
int motorPin4G = 13; // Orange - 28BYJ48 pin 4
// Red - 28BYJ48 pin 5 (VCC)
int motorSpeed = 1200; //variable to set stepper speed
int lookupD[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};
int lookupG[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};
//////////////////////////////////////////////////////////////////////////////
void setup() {
//declare the motor pins as outputs
pinMode(trigPinD, OUTPUT);
pinMode(echoPinD, INPUT);
pinMode(trigPinG, OUTPUT);
pinMode(echoPinG, INPUT);
pinMode(motorPin1D, OUTPUT);
pinMode(motorPin2D, OUTPUT);
pinMode(motorPin3D, OUTPUT);
pinMode(motorPin4D, OUTPUT);
pinMode(motorPin1G, OUTPUT);
pinMode(motorPin2G, OUTPUT);
pinMode(motorPin3G, OUTPUT);
pinMode(motorPin4G, OUTPUT);
Serial.begin(9600);
}
//////////////////////////////////////////////////////////////////////////////
void loop(){
int durationD, distanceD;
digitalWrite(trigPinD, LOW);
delayMicroseconds(2);
digitalWrite(trigPinD, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinD, LOW);
durationD = pulseIn(echoPinD, HIGH);
distanceD = (durationD/2) / 29.1;
Serial.print(distanceD);
Serial.println(" cm droit");
int durationG, distanceG;
digitalWrite(trigPinG, LOW);
delayMicroseconds(2);
digitalWrite(trigPinG, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinG, LOW);
durationG = pulseIn(echoPinG, HIGH);
distanceG = (durationG/2) / 29.1;
Serial.print(distanceG);
Serial.println(" cm gauch e");
if (distanceG < 10 ){
clockwiseG();
anticlockwiseD();
}
else if ( distanceD < 10 ){
anticlockwiseG();
clockwiseD();
}
else {
clockwiseD();
clockwiseG();
}
}
//////////////////////////////////////////////////////////////////////////////
//set pins to ULN2003 high in sequence from 1 to 4
//delay "motorSpeed" between each pin setting (to determine speed)
void anticlockwiseD(){
for(int iD = 0; iD < 8; iD++)
{
setOutputD(iD);
delayMicroseconds(motorSpeed);
}
}
void clockwiseD(){
for(int iD = 7; iD >= 0; iD--)
{
setOutputD(iD);
delayMicroseconds(motorSpeed);
}
}
void anticlockwiseG(){
for(int iG = 0; iG < 8; iG++)
{
setOutputG(iG);
delayMicroseconds(motorSpeed);
}
}
void clockwiseG(){
for(int iG = 7; iG >= 0; iG--)
{
setOutputG(iG);
delayMicroseconds(motorSpeed);
}
}
void setOutputD(int out){
digitalWrite(motorPin1D, bitRead(lookupD[out], 0));
digitalWrite(motorPin2D, bitRead(lookupD[out], 1));
digitalWrite(motorPin3D, bitRead(lookupD[out], 2));
digitalWrite(motorPin4D, bitRead(lookupD[out], 3));
}
void setOutputG(int out){
digitalWrite(motorPin1G, bitRead(lookupG[out], 0));
digitalWrite(motorPin2G, bitRead(lookupG[out], 1));
digitalWrite(motorPin3G, bitRead(lookupG[out], 2));
digitalWrite(motorPin4G, bitRead(lookupG[out], 3));
}
I try this code with only one motor and one sensor. The result was a bit better because it turned (very slowly and stopping random ways) and he has a better react to the detection of obstacles.
Sorry for spelling, i'm french ;)
thank, Théo.
avoid obstacle
- CPU: arduino uno
- Power source: USB for the moment
- Sensors / input devices: ultrasonic HC-SR04 sensors