0

When myservo_1,myservo_2,myservo_3,myservo_4 is commented, I can't control the speed of my M1 motor. But only when I uncomment myservo_1 and so on, I can control the motor speed again.

Can someone help me? My codes are listed down below:

#include <Servo.h>
Servo myservo_1;
Servo myservo_2;
Servo myservo_3;
Servo myservo_4;

int SensorL = 0;
int SensorR = 0;
int SensorM = 0;

#define E1 10
#define M1 12
#define E2 11
#define M2 13
int pos = 0;
int incomingByte = 0;

void setup()
{
  Serial.begin(9600);
  myservo_1.attach(9);
  myservo_2.attach(8);
  myservo_3.attach(7);
  myservo_4.attach(6);
  pinMode(SensorL, INPUT);
  pinMode(SensorR, INPUT);
  pinMode(SensorM, INPUT);
  pinMode(M1, OUTPUT);
  pinMode(M2, OUTPUT);
//  pinMode(E1, OUTPUT);
//  pinMode(E2, OUTPUT);
}

void loop()
{
  SensorR = digitalRead(2);
  SensorL = digitalRead(3);
  SensorM = digitalRead(5);

  if (SensorL == LOW && SensorR == LOW && SensorM == HIGH)
  {
    digitalWrite(M1, HIGH);
    digitalWrite(M2, HIGH);
    analogWrite(E1, 255);
    analogWrite(E2, 255);
    Serial.println(" FORWARD ");
    //delay(1);
  }
  if (SensorL == HIGH && SensorR == LOW && SensorM == HIGH)
  {
    digitalWrite(M1, HIGH);
    digitalWrite(M2, LOW);
    analogWrite(E1, 255);
    analogWrite(E2, 255);
    Serial.println(" LEFT ");
    //delay(1);
  }
  if (SensorL == LOW && SensorR == HIGH && SensorM == HIGH) //move right
  {
    digitalWrite(M1, LOW);
    digitalWrite(M2, HIGH);
    analogWrite(E1, 255);
    analogWrite(E2, 255);
    Serial.println(" RIGHT ");
    //delay(1);
  }

  if (SensorL == HIGH && SensorR == HIGH && SensorM == HIGH) //stop
  {
    digitalWrite(M1, HIGH);
    digitalWrite(M2, HIGH);
    analogWrite(E1, 0);
    analogWrite(E2, 0);
    Serial.println(" STOP ");
    //delay(1);
  }

  if (SensorL == HIGH && SensorR == LOW && SensorM == LOW) //move left
  {
    digitalWrite(M1, HIGH);
    digitalWrite(M2, LOW);
    analogWrite(E1, 125);
    analogWrite(E2, 125);
    Serial.println(" LEFT ");
    //delay(1);
  }

  if (SensorL == LOW && SensorR == HIGH && SensorM == LOW) //move right
  {
    digitalWrite(M1, LOW);
    digitalWrite(M2, HIGH);
    analogWrite(E1, 125);
    analogWrite(E2, 125);
    Serial.println(" MoVE Right ");
    //delay(1);
  }


  if (Serial.available() > 0)
  {
    incomingByte = Serial.read();
    {
      {
        if (incomingByte == 'J')//ARM OPEN
        {
          myservo_1.write(0);
          Serial.println(" ARM OPEN ");
        }
        if (incomingByte == 'L')//ARM CLOSE
        {
          myservo_1.write(180);
          Serial.println(" ARM CLOSE ");
        }
        if (incomingByte == 'I')//ARM UP
        {
          myservo_2.write(0);
          myservo_3.write(180);
          Serial.println(" ARM UP ");
        }
        if (incomingByte == 'K')//ARM DOWN
        {
          myservo_2.write(180);
          myservo_3.write(0);
          Serial.println(" ARM DOWN ");
        }
        if (incomingByte == 'U')//ROTATE LEFT
        {
          myservo_4.write(0);
          Serial.println(" ROTATE LEFT ");
        }
        if (incomingByte == 'O')//ROTATE RIGHT
        {
          myservo_4.write(180);
          Serial.println(" ROATE RIGHT ");
        }
        if (incomingByte == 'M')
        {
          myservo_1.write(90);
          myservo_2.write(90);
          myservo_3.write(90);
          myservo_4.write(90);
          Serial.println(" ALL STOP ");
        }
        if (incomingByte == 'N')//ARM STOP
        {
          myservo_1.write(90);
          Serial.println(" ARM STOP ");
        }
        if (incomingByte == 'B')//UP DOWN STOP
        {
          myservo_2.write(90);
          myservo_3.write(90);
          Serial.println(" UP DOWN STOP ");
        }
        if (incomingByte == 'V')//ROTATION STOP
        {
          myservo_4.write(90);
          Serial.println(" ROTATION STOP ");
        }
        if (incomingByte == 'W')
        {
          digitalWrite(M1, HIGH);
          digitalWrite(M2, HIGH);
          analogWrite(E1, 200);
          analogWrite(E2, 0);
          Serial.println(" FORWARD ");
        }
        if (incomingByte == 'Z')
        {
          digitalWrite(M1, HIGH);
          digitalWrite(M2, HIGH);
          analogWrite(E1, 255);
          analogWrite(E2, 255);
          Serial.println(" FORWARD ");
          delay(100);
        }
        if (incomingByte == 'S')
        {
          digitalWrite(M1, LOW);
          digitalWrite(M2, LOW);
          analogWrite(E1, 255);
          analogWrite(E2, 255);
          Serial.println(" REVERSE ");
        }
        if (incomingByte == 'A')
        {
          digitalWrite(M1, HIGH);
          digitalWrite(M2, LOW);
          analogWrite(E1, 255);
          analogWrite(E2, 255);
          Serial.println(" LEFT ");
        }
        if (incomingByte == 'D')
        {
          digitalWrite(M1, LOW);
          digitalWrite(M2, HIGH);
          analogWrite(E1, 255);
          analogWrite(E2, 255);
          Serial.println(" TURNING RIGHT ");
        }
        if (incomingByte == 'X')
        {
          digitalWrite(M1, LOW);
          digitalWrite(M2, HIGH);
          analogWrite(E1, 0);
          analogWrite(E2, 0);
          Serial.println(" STOP MANAUAL ");
        }
        if (incomingByte == 'w')
        {
          digitalWrite(M1, HIGH);
          digitalWrite(M2, HIGH);
          analogWrite(E1, 125);
          analogWrite(E2, 125);
          Serial.println(" SLOW FORWARD ");
        }
        if (incomingByte == 's')
        {
          digitalWrite(M1, LOW);
          digitalWrite(M2, LOW);
          analogWrite(E1, 125);
          analogWrite(E2, 125);
          Serial.println(" SLOW REVERSE ");
        }
        if (incomingByte == 'a')
        {
          digitalWrite(M1, HIGH);
          digitalWrite(M2, LOW);
          analogWrite(E1, 125);
          analogWrite(E2, 125);
          Serial.println(" SLOW LEFT ");
        }
        if (incomingByte == 'd')
        {
          digitalWrite(M1, LOW);
          digitalWrite(M2, HIGH);
          analogWrite(E1, 125);
          analogWrite(E2, 125);
          Serial.println(" SLOW RIGHT ");
        }
      }
    }
  }
}
1

The Servo library uses timer 1 to send the signals to the servos. But timer 1 is also used to run PWM on pins 9 and 10. It can't however do both.

So since Motor E1 uses pin 10, and timer 1 is already used by the Servo library, PWM, and by extension, analogWrite doesn't work.

E2 uses pin 11, which uses timer 2 for it's PWM, so is therefor no affected.

So move E1 to one of the PWM pins, other than 9 and 10, and thing should start working again.

I however don't know why M1 doesn't work.

  • I had change the pin for my servo 2 3 5 6 so it will not affect pin 9 and 10 but whoever it still doesnt work properly but my E2 M2 still works fine. Is there any other method to troubleshot it? By the way Thx alot! – Eugene24Tab Sep 25 at 12:32
  • That's not what I said. You can use both the servo library (it doesn't matter which pins you use for servos), and PWM on pins 9 and 10. Since you need the servo library, you can't use pins 9 and 10 for PWM. So you need to change the pin for that motor that's currently connected to pin 10. Changing servo pins does not help here. – Gerben Sep 26 at 15:09
0
  • Please correct your code there are several mistakes, such as
    1. the lines at beginning of loop SensorR = digitalRead(2); SensorL = digitalRead(3); SensorM = digitalRead(5); doesn't make any sense.. as you not defined 2,3,5 pin as input. And for taking analog input please change pins 2,3,5 and analog pins..
    2. Motor Pin M1 and M2 are connected 12,13 which are not pwm pins so you can't use to change the speed of your Motor.. Please make this code sensible....
  • E1 E2 are used by pwm pins to control the speed and the ir sensor is define as input above – Eugene24Tab Sep 25 at 12:30

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.