1

Proceeding is the relevant part of the code for the problem I'm asking assistance with. I'm attempting to vary the speed of the robots motors with PWM. The left motor speed can be varied numerical. The right motor fails to move. Any ideas is appreciated. The motor speed code is at the end of the sketch. Any assistance is greatly appreciated.

enter code here

//Below are the symbolic constants. Instead of having to type in a non-sensical pin number each time we want to do something we can write an easy to understand name which represents the pin, the compiler will then replace the names with the numbers
 #define LeftMotorForward 8
 #define LeftMotorBackward 9
 #define LeftMotorSpeed 10
 #define RightMotorForward 6
 #define RightMotorBackward 7
 #define RightMotorSpeed 5
 #define USTrigger 3
 #define USEcho 2
 #define MaxDistance 100
 #define LED 13

//Here we have created two 'objects', one for the servo and one for the ultrasonic sensor Servo servo; NewPing sonar(USTrigger, USEcho, MaxDistance);

//Below we are creating unsigned integer variables which we will use later on in the code. They are unsigned as they will only have postive values unsigned int duration; unsigned int distance; unsigned int FrontDistance; unsigned int LeftDistance; unsigned int RightDistance; unsigned int Time; unsigned int CollisionCounter;

void setup()                                            //This block happens once on startup {  Serial.begin(9600);                              //I have included the serial initialize but commented it out, if you want to debug and print information to the serial monitor just uncomment    //Here we are setting the pin modes. As we will sending out signals from the pins we set them as outputs  pinMode(LeftMotorForward, OUTPUT);  pinMode(LeftMotorBackward, OUTPUT);  pinMode(LeftMotorSpeed, OUTPUT);  pinMode(RightMotorForward, OUTPUT);  pinMode(RightMotorBackward, OUTPUT);  pinMode(RightMotorSpeed, OUTPUT);  pinMode(LED, OUTPUT);  servo.attach(11);                                    //The servo is attached to pin 4 }

void loop()                                           //This block repeats itself while the Arduino is turned on {  servo.write(90);                                    //Rotate the servo to face the front                    scan();                                             //Go to the scan function  FrontDistance = distance;                           //Set the variable FrontDistance to the value of the distance returned from the scan function  Serial.println("Front distance = ");  Serial.print(distance);  if(FrontDistance > 40 || FrontDistance == 0)        //If there is nothing infront of the robot within 40cm or the distance value is 0 (which for the newping libary means no ping was returned) then...  {   moveForward();                                     //Go to the moveForward function  }  else                                                //Else (if there is something infront of the robot within 40cm) then...  {    CollisionCounter = CollisionCounter + 1;    moveStop();                                       //Go to the moveStop function    navigate();  } }

void moveForward()                                    //This function tells the robot to go forward {        Serial.println("");  Serial.println("Moving forward");  //Turn on left motor  digitalWrite (LeftMotorBackward, LOW);  digitalWrite(LeftMotorForward, HIGH);  //set speed  analogWrite(LeftMotorSpeed, 100);

// turn on right motor  digitalWrite(RightMotorBackward, LOW);  digitalWrite(RightMotorForward, HIGH); // set speed   analogWrite (RightMotorSpeed, 75);

2

You are not sending a speed command to the motor, you are sending it an analog voltage with the analogWrite. If that voltage is too low, the motor will stall. A higher value should make it spin faster. If you want the speed to be more tightly controlled, you will need encoders on the motors to sense how fast they are spinning, or use stepper motors.

1

First try commenting out the

 Servo servo;

line, and all other bits using the servo. If it works then there's your problem - use the Mega possibly as this allows more PWM outputs.

If it doesn't work with the Servo bits removed, then report back and include the Arduino model you're using.

Good luck!

  • int value; for(value = 0 ; value <= 75; value+=5)//if value is <=50 left motor does not spin. >75 both motors turn Serial.println(""); Serial.println("Moving forward"); analogWrite(LeftMotorSpeed, value);//int value has no effect on speed analogWrite (RightMotorSpeed, value);// same as LeftMotor) – Tom Evans Feb 27 '16 at 6:29
  • 1
    Thanks Andy. I'm getting a Mega board and using pin 11 on the Uno, to avoid the servo conflict. – Tom Evans Feb 28 '16 at 14:52
0

I commented all references to servo, the same results. Comment out the servo code doesn't seem to make a difference. What's going on is when the speed is numerically controlled the motor speed varies. At 50 the right motors slows down and the left motor HALTS, Not sure what's going on.

//This function tells the robot to go forward 
int value;
for(value = 0 ; value <= 75; value+=5)
    //if value is <=50 left motor does not spin. >75 both motors turn
    Serial.println("");
    Serial.println("Moving forward");
    analogWrite(LeftMotorSpeed, value);//int value has no effect on speed
    analogWrite(RightMotorSpeed, value);// same as LeftMotor)

    digitalWrite(LeftMotorBackward, LOW);
    digitalWrite(LeftMotorForward, HIGH);
    digitalWrite(RightMotorBackward, LOW);
    digitalWrite(RightMotorForward, HIGH);
  • Now you've got both motors running, but they don't run at the same speed? A common problem - possibly not enough current getting through from the battery. See Jim's answer. – Andy Feb 29 '16 at 8:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.