RC CAR Motor not spinning

Hello everybody, i am stuck, i am making an RC car project where i have a Servo motor for turning and a DC motor to go to front or back. Now i have my program and if i upload it seperately (so only the part of servo program or only of the motor) it works fine but when i join them together my car won't go in reverse. i have unmanteled it and see that stops working when i add this part of my code " myservo.attach(servoPin); " can anybody help me please because i am lost
my full code;

// Pin declaraties 
#define FORWARD_PIN 2    // Pin vooruit
#define REVERSE_PIN 3    // Pin achteruit
#define RPWM 10           // RPWM van BTS7960 (vooruit)
#define LPWM 6          // LPWM van BTS7960 (achteruit)
#define L_EN 13          // Motordriver BTS 7960
#define R_EN 12          // Motordriver BTS 7960

// Handmatige snelheidsinstelling (tussen 0 en 255)
int motorSpeed = 160;  // Pas deze waarde aan om de snelheid van de motor in te stellen

#include <Servo.h>


Servo myservo;
  int servoPin = 9; // Servo
  int drukknopLinks = 4;  // Links
  int drukknopRechts = 5; // Rechts  
  int neutraal = 90; // Neutraal
  int posLinks = 135; // Positie voor links 
  int posRechts = 45; // Positie voor rechts 


void setup() {

    //Servo
  myservo.attach(servoPin); //PWM pin Uitgang
  pinMode(drukknopLinks, INPUT_PULLUP);
  pinMode(drukknopRechts, INPUT_PULLUP);
  myservo.write(neutraal);

  // Zet de pinnen in de juiste modus
  pinMode(FORWARD_PIN, INPUT_PULLUP); // Vooruit pin met pullup
  pinMode(REVERSE_PIN, INPUT_PULLUP); // Achteruit pin met pullup
  pinMode(RPWM, OUTPUT);  // PWM voor vooruit
  pinMode(LPWM, OUTPUT);  // PWM voor achteruit
  pinMode(L_EN, OUTPUT);  // Enable pin voor BTS7960
  pinMode(R_EN, OUTPUT);  // Enable pin voor BTS7960

  // Zet de motor in de uitgeschakelde toestand bij opstarten
  digitalWrite(RPWM, LOW);
  digitalWrite(LPWM, LOW);
  digitalWrite(L_EN, LOW);
  digitalWrite(R_EN, LOW);

  // Debug pinnen
  Serial.begin(9600); // Voor debuggen
}

void loop()
{
  //Servo
  int DKLinks = digitalRead(drukknopLinks);
  int DKRechts = digitalRead(drukknopRechts);

  if (DKLinks == HIGH && DKRechts == LOW) 
  {
    // Linker knop ingedrukt => Servo naar links
    myservo.write(posLinks);
  }
  else if (DKRechts == HIGH && DKLinks == LOW) 
  {
    // Rechter knop ingedrukt => Servo naar rechts
    myservo.write(posRechts);
  }
  else 
  {
    // Geen knop ingedrukt => Servo terug naar neutraal (rechtdoor)
    myservo.write(neutraal);
  }
  
  // Lees de status van de knoppen (Vooruit en Achteruit)
  bool forwardState = digitalRead(FORWARD_PIN);  // Lees vooruit knop
  bool reverseState = digitalRead(REVERSE_PIN);  // Lees achteruit knop

  // Debug: toon de status van de knoppen en snelheid
  Serial.print("Forward: ");
  Serial.print(forwardState);
  Serial.print(" | Reverse: ");
  Serial.print(reverseState);
  Serial.print(" | Speed: ");
  Serial.println(motorSpeed);  // Toon de snelheid in de debug

  // Als de vooruit knop ingedrukt is (LOW), zet de motor vooruit
  if (forwardState == LOW) {
    moveForward(motorSpeed);  // Geef de snelheid door
  }
  // Als de achteruit knop ingedrukt is (LOW), zet de motor achteruit
  else if (reverseState == LOW) {
    moveBackward(motorSpeed);  // Geef de snelheid door
  }
  // Als geen knop ingedrukt is, stop de motor
  else {
    stopMotor();
  }

  delay(50); // Vertraging om overbelasting te voorkomen
}

void moveForward(int speed) {
  // Zet de motor vooruit met de opgegeven snelheid
  analogWrite(RPWM, speed);  // Zet PWM voor vooruit
  analogWrite(LPWM, 0);      // Zet PWM voor achteruit uit
  digitalWrite(L_EN, HIGH);  // Zet de motor driver aan
  digitalWrite(R_EN, HIGH);  // Zet de motor driver aan
  Serial.println("Motor vooruit!");
}

void moveBackward(int speed) {
  // Zet de motor achteruit met de opgegeven snelheid
  analogWrite(RPWM, 0);      // Zet PWM voor vooruit uit
  analogWrite(LPWM, speed);  // Zet PWM voor achteruit
  digitalWrite(L_EN, HIGH);  // Zet de motor driver aan
  digitalWrite(R_EN, HIGH);  // Zet de motor driver aan
  Serial.println("Motor achteruit!");
}

void stopMotor() {
  // Zet de motor uit
  analogWrite(RPWM, 0);   // Zet PWM voor vooruit uit
  analogWrite(LPWM, 0);   // Zet PWM voor achteruit uit
  digitalWrite(L_EN, LOW);  // Zet de motor driver uit
  digitalWrite(R_EN, LOW);  // Zet de motor driver uit
  Serial.println("Motor stopt!");
}

Welcome to the forum

Using the Servo library disables PWM on pins 9 and 10

From https://docs.arduino.cc/libraries/servo/

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.

1 Like

omg thank u so much it works now, though i already tried other pins but apparently not the right ones :slight_smile:

I am glad that you got it working

As to using other pins, only some of them support PWM so you must use the right ones

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.