Go Down

Topic: motor not working (Read 104 times) previous topic - next topic

lucu007

So i making a selfdriving robot but i my code is not working for somme reason my left motor is giving rapid pulses instead of giving a single puls  so ther for my robot left weel is not spining .



Code: [Select]

#include <AFMotor.h>

#include <AFMotor.h> //import your motor shield library
#define trigPin 12 // define the pins of your sensor
#define echoPin 13
#define trigPin1 8 // define the pins of your sensor
#define echoPin1 9
#define trigPin2 10 // define the pins of your sensor
#define echoPin2 11

AF_DCMotor motor1(1,MOTOR12_8KHZ); // set up motors.
AF_DCMotor motor2(2,MOTOR12_8KHZ);
 
void setup() {
  Serial.begin(9600); // begin serial communitication 
  Serial.println("Motor test!");
  pinMode(trigPin, OUTPUT);// set the trig pin to output (Send sound waves)
  pinMode(echoPin, INPUT);// set the echo pin to input (recieve sound waves)
  pinMode(trigPin1, OUTPUT);// set the trig pin to output (Send sound waves)
  pinMode(echoPin1, INPUT);// set the echo pin to input (recieve sound waves)
  pinMode(trigPin2, OUTPUT);// set the trig pin to output (Send sound waves)
  pinMode(echoPin2, INPUT);// set the echo pin to input (recieve sound waves)
 
motor1.setSpeed(250); //set the speed of the motors, between 0-255

motor2.setSpeed (250); 
}
 
void loop() {

   long duration, distance, duration1, distance1, duration2, distance2; // start the scan
 
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2); // delays are required for a succesful sensor operation.
  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10); //this delay is required as well!
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;// convert the distance to centimeters.
  if (distance < 35)/*if there's an obstacle 25 centimers, ahead, do the following: */ {   
   Serial.println ("Close Obstacle detected!" );
Serial.println ("Obstacle Details:");
Serial.print ("Distance From Robot is " );
Serial.print ( distance);
Serial.print ( " CM!");// print out the distance in centimeters.

Serial.println (" The obstacle is declared a threat due to close distance. ");
Serial.println (" Turning !");

   ;motor1.run(BACKWARD);  // Turn as long as there's an obstacle ahead.
    motor2.run (FORWARD);

}
  else {
   Serial.println ("No obstacle detected. going forward");
   delay (15);
   motor2.run(BACKWARD); //if there's no obstacle ahead, Go Forward!
    motor1.run(BACKWARD); 

  } 
 
  digitalWrite(trigPin1, LOW); 
  delayMicroseconds(2); // delays are required for a succesful sensor operation.
  digitalWrite(trigPin1, HIGH);

  delayMicroseconds(10); //this delay is required as well!
  digitalWrite(trigPin1, LOW);
  duration1 = pulseIn(echoPin1, HIGH);
  distance1 = (duration1/2) / 29.1;// convert the distance to centimeters.
  if (distance1 < 30)/*if there's an obstacle 25 centimers, ahead, do the following: */ {   
   Serial.println ("Close Obstacle detected!" );
Serial.println ("Obstacle Details:");
Serial.print ("Distance111 From Robot is " );
Serial.print ( distance1);
Serial.print ( " CM!");// print out the distance in centimeters.

Serial.println (" The obstacle is declared a threat due to close distance. ");
Serial.println (" Turning !");
   
    ;motor1.run(BACKWARD);  // Turn as long as there's an obstacle ahead.
    motor2.run (FORWARD);

}
  else {
   Serial.println ("No obstacle detected. going forward");
   delay (15);
   motor2.run(BACKWARD); //if there's no obstacle ahead, Go Forward!
    motor1.run(BACKWARD); 

 } 

  digitalWrite(trigPin2, LOW); 
  delayMicroseconds(2); // delays are required for a succesful sensor operation.
  digitalWrite(trigPin2, HIGH);

  delayMicroseconds(10); //this delay is required as well!
  digitalWrite(trigPin2, LOW);
  duration2 = pulseIn(echoPin2, HIGH);
  distance2 = (duration2/2) / 29.1;// convert the distance to centimeters.
  if (distance2 < 30)/*if there's an obstacle 25 centimers, ahead, do the following: */ {   
   Serial.println ("Close Obstacle detected!" );
Serial.println ("Obstacle Details:");
Serial.print ("Distance2222 From Robot is " );
Serial.print ( distance2);
Serial.print ( " CM!");// print out the distance in centimeters.

Serial.println (" The obstacle is declared a threat due to close distance. ");
Serial.println (" Turning !");
   
    ;motor1.run(FORWARD);  // Turn as long as there's an obstacle ahead.
    motor2.run (BACKWARD);

}
  else {
   Serial.println ("No obstacle detected. going forward");
   delay (15);
   motor2.run(BACKWARD); //if there's no obstacle ahead, Go Forward!
    motor1.run(BACKWARD);
 
  ;} 







 
 
 

 
 
 
 
}

MorganS

We can't see your robot. Your code only has motor 1 and motor 2. We can't see if you've got motor 1 on the left side or motor 2. Don't you think that would be useful information to diagnose the problem?

You have a basic problem in the logic. You read each sensor individually and each sensor sends drive commands to the motors. What if the left sensor and the right sensor both detect something? It will try to turn both ways and fail.

Try to write your main loop like this:
Code: [Select]
void loop() {
  readSensors();
  doCalculations();
  driveMotors();
}

Then take all the code you have and put it into those 3 functions as appropriate.
"The problem is in the code you didn't post."

lucu007

the motor 1 is the left one

wvmarle

Skimming your code I noticed that the layout is terribly messed up: do a ctrl-T in the IDE and the indentation is fixed and everything becomes a lot more readable.

You appear to have three ultrasound sensors, and three blocks of identical code. Put that in a separate function, create arrays for the pin numbers, and you have another way to simplify your code (and make it more readable to boot). Then you can just do something like

Code: [Select]

byte trigPin[3] = {12, 8, 10}
byte echoPin[3] = {13, 9, 11}

void loop() {
  float distance = readSensor(0);
  // handle sensor 0 distance
  float distance = readSensor(1);
  // handle sensor 1 distance
  float distance = readSensor(2);
  // handle sensor 1 distance
}

float readSensor(byte sensor) {
  // read the sensor at echoPin[sensor] and trigPin[sensor].
  return distance;
}


Another thing (shouldn't affect operation, but it's still wrong) is this wayward semicolon:

Code: [Select]

 ;motor1.run(BACKWARD);  // Turn as long as there's an obstacle ahead.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

Go Up