Go Down

Topic: Servo and DC motor with bts7960 (Read 54 times) previous topic - next topic

ahmadmoaz

Hello Everyone. I am having a problem running my servo with a DC motor. They work perfectly fine independently but the moment i combine them under one code, my DC motor only rotates in one direction.
Below you can see the code.

int RPWM=10;
int LPWM=11;
// timer 0
int L_EN=7;
int R_EN=8;
#include <Servo.h>

Servo myservo;


void setup() {

 
   pinMode(RPWM,OUTPUT);
   pinMode(LPWM,OUTPUT);
   pinMode(L_EN,OUTPUT);
   pinMode(R_EN,OUTPUT);
 
  digitalWrite(RPWM,LOW);
  digitalWrite(LPWM,LOW);
  digitalWrite(L_EN,LOW);
  digitalWrite(R_EN,LOW);
 
   delay(1000);
    Serial.begin(9600);
  myservo.attach(9); 
  }



void loop() {
 
  servo();
  Serial.println("EN High");
  digitalWrite(R_EN,HIGH);
  digitalWrite(L_EN,HIGH);
  Serial.println("stop yet\n");
delay(500);
   Serial.println("start CW\n");
   analogWrite(LPWM,50);
   
delay(5000);
   analogWrite(LPWM,0);
    Serial.println("stop\n");
    delay(2000);
analogWrite(RPWM,50);
Serial.println("start CCW\n ");
delay(2000);
analogWrite(RPWM,0);
Serial.println("srop2\n ");
delay(2000);

 
}

void servo(){
  myservo.write(90);                 
  delay(1000);     
  myservo.write(0);
  delay(1000); 
}

groundFungus

#1
Apr 11, 2019, 02:20 pm Last Edit: Apr 11, 2019, 02:23 pm by groundFungus
Code: [Select]
int RPWM=10;
If you had read the Servo library reference you would have seen this:
Quote
use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins.
The solution is to use a different PWM pin for RPWM.

Read the how to use this forum-please read sticky to see how to properly post code.

Go Up