Motor control and HCSR04 radar

Hello
I have a question please. (robot control)

I can’t have my motor to run properly in a programmed sequence :

I mean :
I’ve done a sketch which should control a DC motor :

if distance< “…” motor should stop, then go backward, then wait, then go forward …

I have a motor shield.

Everything is fine for the first action :

if distance< “…” motor stops

but I can’t manage the motor to do the 3 following actions (ie go backward, stop, and go forward)

I’m writing the instructions right after “motor stops” but nothing happens …

No code error by the way.

Any idea ?
Philippe

No code error by the way.

and no code, it seems

Back Home, sorry, yes it’s easier with the code !

/*
HC-SR04 Ping distance sensor]
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 13 Trig to Arduino pin 12
Red POS to Arduino pin 11
Green POS to Arduino pin 10
560 ohm resistor to both LED NEG and GRD power rail
More info at: http://goo.gl/kJ8Gl
Original code improvements to the Ping sketch sourced from Trollmaker.com
Some code and wiring inspired by http://en.wikiversity.org/wiki/User:Dstaub/robotcar
*/
#include <AFMotor.h>
#define trigPin 13
#define echoPin 12

AF_DCMotor motor1(1, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor2(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Motor test!");
  
  motor1.setSpeed(150);     // set the speed to 200/255
  motor2.setSpeed(150);     // set the speed to 200/255
  
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2);        // Added this line
  digitalWrite(trigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10);       // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;

  
  if (distance <10) {          // distance de détection
  
  motor1.run(RELEASE);
  motor2.run(RELEASE);         // stop
  delay(1000);
  motor1.run(BACKWARD);        // arrière
  delay(1000); 
  motor1.run(FORWARD);       // avant
  motor2.run(FORWARD);       // avant   
}
  else {
    motor1.run(FORWARD);       // avant
    motor2.run(FORWARD);       // avant
  } 

if (distance >10) {  
    
    motor1.run(FORWARD);       // avant
    motor2.run(FORWARD);       // avant

}  
}

but I can’t manage the motor to do the 3 following actions (ie go backward, stop, and go forward)

Do you want to do that anyway, because if you do the bot will be back in the same place,

Your code says this

 if (distance < 10)           // distance de détection
  {

    motor1.run(RELEASE);
    motor2.run(RELEASE);         // stop
    delay(1000);
    motor1.run(BACKWARD);        // arrière
    delay(1000);
    motor1.run(FORWARD);       // avant
    motor2.run(FORWARD);       // avant
  }

Which, if the comments are to be believed, does stop, turn, forward

Is that what it does ?

Dear Bob

sorry to be inaccurate ...
Yes I want the bot to go back and turn (only one motor will do) and then go away ....

but this code won't work ....

the motors stop and then start again almost immediately (?)

To me your current code says this

    motor1.run(RELEASE);         // stop both motors
    motor2.run(RELEASE);         
    delay(1000);                      //wait 1 second
    motor1.run(BACKWARD);     //run 1 motor in reverse (ie turn the bot) 
    delay(1000);                      //wait 1 second
    motor1.run(FORWARD);       //run both motors forward 
    motor2.run(FORWARD);

I am assuming that your original comments cause the bot to do what they say. Have you tested the commands individually.

ie, does

   motor1.run(FORWARD);       //run both motors forward 
    motor2.run(FORWARD);

actually make both motors run forward

   motor1.run(BACKWARD);     //run 1 motor in reverse (ie turn the bot)

make motor 1 run in reverse ?

and

    motor1.run(RELEASE);         // stop both motors
    motor2.run(RELEASE);

stop both motors if they were previously running

If so, then try this

    motor1.run(RELEASE);         // stop both motors
    motor2.run(RELEASE);         
    delay(1000);                      //wait 1 second
    motor1.run(BACKWARD);     //run both motors in reverse 
    motor2.run(BACKWARD);    
    delay(5000);                      //backwards for 5 seconds
    motor1.run(RELEASE);         //stop motor 1
    motor2.run(BACKWARD);     //turn the bot
    delay(5000);                      //turn for 5 seconds
    motor1.run(FORWARD);       //run both motors forward 
    motor2.run(FORWARD);

Hello
Still have troubles with this codes, motors won’t run properly, (They don’t stop, but run fine forward …)
I guess I should work on a subroutine ?
Any idea
Thanks
Philippe

Still have troubles with this codes

Changes have been suggested. You need to show that you have implemented them (by posting your code) or explain why you haven't implemented them.

Here’s the code ?
Philippe

#include <AFMotor.h>
AF_DCMotor motor(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motorb(3, MOTOR12_64KHZ); // create motor #3, 64KHz pwm

// trigger > digital8 : echo > digital7 : plus > +5V : moins > GND
//led : plus > digital13 : moins > digitalgnd

#define trigPin 13
#define echoPin 12
#define led 13

void setup() {
  Serial.begin (9600);  
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
  Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Motor test!");
motor.setSpeed(200); // set the speed to 200/255
motorb.setSpeed(200); // set the speed to 200/255
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
 

  if (distance <= 5) {
motor.run(RELEASE); // stopped
delay(2000);
motor.run(BACKWARD); // stopped
delay(2000);

}
  
  else {
Serial.print("tick");
motor.run(FORWARD); // turn it on going forward
Serial.print("tick");
motorb.run(FORWARD); // turn it on going forward
  }
}

They don’t stop, but run fine forward …

This is the code block that makes the two motors run forward

    Serial.print("tick");
    motor.run(FORWARD); // turn it on going forward
    Serial.print("tick");
    motorb.run(FORWARD); // turn it on going forward

This is the code block that tries to make the two motors stop and run in reverse (despite the comments otherwise)

    motor.run(RELEASE); // stopped
    delay(2000);
    motor.run(BACKWARD); // stopped
    delay(2000);

but where is the code for motorb to run backwards ?

Why are the names of the motors not motor1 and motor2 or maybe leftMotor and rightMotor instead of motor and motorb ?

Here's the code ?

Is that a question? Or a statement?

AF_DCMotor motor(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motorb(3, MOTOR12_64KHZ); // create motor #3, 64KHz pwm

When you feel the need to add a suffix to one item in a set, give in completely and suffix ALL of the items in the set.

Do you KNOW which pins on the Arduino make motor #2 move? Do you KNOW which pins on the Arduino make motor #3 move?

#define trigPin 13
#define echoPin 12
#define led 13

So the LED and the ping sensor are on the same pin. Really?

What do your Serial.print() statements tell you is happening? What distance are you really measuring?

Dear Paul and Bob

I’ve reworked a bit a code … well, that works but :…

both motors stop, but the motor1 won’t go backward … ?
And I’d like to run the sequence only one time …

#include <AFMotor.h>
AF_DCMotor motor2(2, MOTOR12_8KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor1(1, MOTOR12_8KHZ); // create motor #1, 64KHz pwm

// trigger > digital2 : echo > digital3 : plus > +5V : moins > GND

#define trigPin 12
#define echoPin 11

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600); // set up Serial library at 9600 bps
  Serial.println("Motor test!");
  motor1.setSpeed(200); // set the speed to 200/255
  motor2.setSpeed(200); // set the speed to 200/255
 
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
 

  if (distance <= 10) {
  
  motor1.run(RELEASE); // stopped
  motor2.run(RELEASE); // stopped
  delay(500);
  
  motor1.run(BACKWARD); // the other way
  motor2.run(RELEASE); // stopped
  delay(1000);
  
  motor1.run(RELEASE); // stopped
    
   
}
  else {
   motor1.run(FORWARD); // turn it on going forward 
   motor2.run(FORWARD); // turn it on going forward 
  }
  if (distance >= 15) {
  motor1.run(FORWARD); // turn it on going forward
  motor2.run(FORWARD); // turn it on going forward   
  }
 

}

If you put EVERY { on a line BY ITSELF, and used Tools + Auto format, your code would be far easier to follow.

In fact, you’d see that you have this mess:

  if (distance <= 10)
  {
    motor1.run(RELEASE); // stopped
    motor2.run(RELEASE); // stopped
    delay(500);

    motor1.run(BACKWARD); // the other way
    motor2.run(RELEASE); // stopped
    delay(1000);

    motor1.run(RELEASE); // stopped
  }
  else
  {
    motor1.run(FORWARD); // turn it on going forward
    motor2.run(FORWARD); // turn it on going forward
  }
  if (distance >= 15)
  {
    motor1.run(FORWARD); // turn it on going forward
    motor2.run(FORWARD); // turn it on going forward   
  }

If the distance is less than 10, do one thing. Otherwise, do something else.

Then, if the same distance is greater than 15, do the same thing you were doing. Otherwise, do the same thing you were doing.

You REALLY need to explain THAT “logic”.