0

enter image description here If I'm connecting it in this way I have no issue and I can control the speed. here is a video enter image description here

const int speedMotorA = 9;
const int speedMotorB = 3;
const int motorPin1  = 4;  
const int motorPin2  = 7;  
const int motorPin3  = 12; 
const int motorPin4  = 13; 

void setup() {
  Serial.begin(9600);
  pinMode(speedMotorA, OUTPUT);
  pinMode(speedMotorB, OUTPUT);
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
}
void loop(){
  TurnMotorA();
  TurnMotorB();
}


void TurnMotorA() {
  int Speed = analogRead(A2);
  Speed = Speed*0.24926686;
  Serial.println(Speed);
  analogWrite(speedMotorA,Speed);
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
}

void TurnMotorB() {
  int Speed = analogRead(A2);
  Speed = Speed*0.24926686;
  analogWrite(speedMotorB,Speed);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, LOW);
}

now I'm trying to expand my mini-project with servo and ultrasonic sensor with this code and wiring, I have tried many ways to control speed but it's not working properly while increasing speed motor A respond but motor B stay off till I reach maximum speed 255 motor B start working. here is a video enter image description here

#include <NewPing.h>
#include <Servo.h> 

#define TRIG_PIN A4 
#define ECHO_PIN A5 
#define MAX_DISTANCE 200 
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); 
Servo myservo;

const int laser = 8;
int distance = 0;
const int speedMotorA = 9;
const int speedMotorB = 3;
const int motorPin1  = 4;  
const int motorPin2  = 7;  
const int motorPin3  = 12; 
const int motorPin4  = 13;  

int hitlaser(){
  digitalWrite(laser, HIGH);
  delay(200);
  digitalWrite(laser, LOW);
}

void setup() {
  pinMode(laser, OUTPUT);
  pinMode(speedMotorA, OUTPUT);
  pinMode(speedMotorB, OUTPUT);
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
  myservo.attach(5);  
  myservo.write(65);
  delay(2000);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
}

void loop() {
 int distanceR = 0;
 int distanceL =  0;
 delay(40);
 digitalWrite(laser, HIGH);
 if(distance<=50)
 {
  moveStop();
  delay(100);
  moveBackward();
  delay(300);
  moveStop();
  delay(200);
  distanceR = lookRight();
  delay(200);
  distanceL = lookLeft();
  delay(200);

  if(distanceR>=distanceL)
  {
    turnRight();
    moveStop();
  }else
  {
    turnLeft();
    moveStop();
  }
 }else
 {
  moveForward();
 }
 distance = readPing();
}

int lookRight()
{
    myservo.write(5);
    delay(500);
    int distance = readPing();
    delay(100);
    myservo.write(65); 
    return distance;
}

int lookLeft()
{
    myservo.write(150); 
    delay(500);
    int distance = readPing();
    delay(100);
    myservo.write(65); 
    return distance;
    delay(100);
}

int readPing() { 
  delay(70);
  int cm = sonar.ping_cm();
  if(cm==0)
  {
    cm = 250;
  }
  return cm;
}

void moveStop() {
    analogWrite(speedMotorA, 0);
    analogWrite(speedMotorB, 0);
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, LOW);
    digitalWrite(motorPin3, LOW);
    digitalWrite(motorPin4, LOW);
  } 

void moveForward() {
    int Speed = analogRead(A2);
    Speed = Speed*0.24926686;
    Serial.println(Speed);
    analogWrite(speedMotorA,Speed);
    analogWrite(speedMotorB,Speed);
    digitalWrite(motorPin1, HIGH);
    digitalWrite(motorPin2, LOW);
    digitalWrite(motorPin3, HIGH);
    digitalWrite(motorPin4, LOW);
}

void moveBackward() {
    int Speed = analogRead(A2);
    Speed = Speed*0.24926686;
    Serial.println(Speed);
    analogWrite(speedMotorA,Speed);
    analogWrite(speedMotorB,Speed);
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, HIGH);
    digitalWrite(motorPin3, LOW);
    digitalWrite(motorPin4, HIGH);
}  

void turnRight() {
  int Speed = analogRead(A2);
  Speed = Speed*0.24926686;
  Serial.println(Speed);
  analogWrite(speedMotorA,Speed);
  analogWrite(speedMotorB,Speed);
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, HIGH);    
  delay(300);
  moveForward();
} 

void turnLeft() {
  int Speed = analogRead(A2);
  Speed = Speed*0.24926686;
  Serial.println(Speed);
  analogWrite(speedMotorA,Speed);
  analogWrite(speedMotorB,Speed);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, HIGH);   
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, LOW);     
  delay(300);
  moveForward();
}  

update :

after removing servo from the project it seems working but how can i use servo with this project Thanks to Majenko♦ update 2 after attaching servo to pin 9 i can now controle speed but still some issues as when Robot car have obsticles it should make backword >> issue is >> one motor only go backword!.

#include <NewPing.h>
#include <Servo.h> 

#define TRIG_PIN A4 
#define ECHO_PIN A5 
#define MAX_DISTANCE 200 
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); 
Servo myservo;

const int laser = 8;
int distance = 0;
const int speedMotorA = 11;
const int speedMotorB = 3;
const int motorPin1  = 4;  
const int motorPin2  = 7;  
const int motorPin3  = 12; 
const int motorPin4  = 13;  

int hitlaser(){
  digitalWrite(laser, HIGH);
  delay(200);
  digitalWrite(laser, LOW);
}

void setup() {
  pinMode(laser, OUTPUT);
  pinMode(speedMotorA, OUTPUT);
  pinMode(speedMotorB, OUTPUT);
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
  myservo.attach(9);  
  myservo.write(65);
  delay(2000);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
  distance = readPing();
  delay(100);
}

void loop() {
 int distanceR = 0;
 int distanceL =  0;
 delay(40);
 digitalWrite(laser, HIGH);
 if(distance<=50)
 {
  moveStop();
  delay(100);
  moveBackward();
  delay(1000);
  moveStop();
  delay(200);
  distanceR = lookRight();
  delay(200);
  distanceL = lookLeft();
  delay(200);

  if(distanceR>=distanceL)
  {
    turnRight();
    moveStop();
  }else
  {
    turnLeft();
    moveStop();
  }
 }else
 {
  moveForward();
 }
 distance = readPing();
}

int lookRight()
{
    myservo.write(5);
    delay(500);
    int distance = readPing();
    delay(100);
    myservo.write(65); 
    return distance;
}

int lookLeft()
{
    myservo.write(150); 
    delay(500);
    int distance = readPing();
    delay(100);
    myservo.write(65); 
    return distance;
    delay(100);
}

int readPing() { 
  delay(70);
  int cm = sonar.ping_cm();
  if(cm==0)
  {
    cm = 250;
  }
  return cm;
}

void moveStop() {
    analogWrite(speedMotorA, 0);
    analogWrite(speedMotorB, 0);
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, LOW);
    digitalWrite(motorPin3, LOW);
    digitalWrite(motorPin4, LOW);
  } 

void moveForward() {
    int Speed = analogRead(A2);
    Speed = Speed*0.24926686;
    Serial.println(Speed);
    analogWrite(speedMotorA,Speed);
    analogWrite(speedMotorB,Speed);
    digitalWrite(motorPin1, HIGH);
    digitalWrite(motorPin2, LOW);
    digitalWrite(motorPin3, HIGH);
    digitalWrite(motorPin4, LOW);
}

void moveBackward() {
    int Speed = analogRead(A2);
    Speed = Speed*0.24926686;
    Serial.println(Speed);
    analogWrite(speedMotorA,Speed);
    analogWrite(speedMotorB,Speed);
    digitalWrite(motorPin1, LOW);
    digitalWrite(motorPin2, HIGH);
    digitalWrite(motorPin3, LOW);
    digitalWrite(motorPin4, HIGH);
}  

void turnRight() {
  int Speed = analogRead(A2);
  Speed = Speed*0.24926686;
  Serial.println(Speed);
  analogWrite(speedMotorA,Speed);
  analogWrite(speedMotorB,Speed);
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, HIGH);    
  delay(300);
  moveForward();
} 

void turnLeft() {
  int Speed = analogRead(A2);
  Speed = Speed*0.24926686;
  Serial.println(Speed);
  analogWrite(speedMotorA,Speed);
  analogWrite(speedMotorB,Speed);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, HIGH);   
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, LOW);     
  delay(300);
  moveForward();
}  

Appreciate your support

  • Some ground connections are missing. – gre_gor Jun 18 '18 at 15:35
  • yeah but in the video, it's connected, forgot to add this on drawing.( servo ground wire) – Ahmed Younes Jun 19 '18 at 2:05
0

The problem here is a conflict of resources. Both the Servo library and the PWM for pin 9 require the same timer.

The Servo library supports up to 12 motors on most Arduino boards and 48 on the Arduino Mega. On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins.

You should choose a different PWM pin (not pin 10) instead of pin 9.

  • for servo I'm using only one pin myservo.attach(5); – Ahmed Younes Jun 19 '18 at 1:16
  • myservo.attach(8); (I have changed it to pin 8 but still not working). – Ahmed Younes Jun 19 '18 at 2:02
  • Ok i have removed servo from the project and it seems working , so how can i fix this issue and let it work with servo .. Question updated Thanks ! @arduino.stackexchange.com/users/4143/majenko – Ahmed Younes Jun 19 '18 at 2:14
  • Another update2 please check – Ahmed Younes Jun 19 '18 at 2:31
  • @ahmedyounes It doesn't matter where the servo is. It's the pin 9 pwm for the motor you must change - like my answer states quite clearly. – Majenko Jun 19 '18 at 8:15

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.