Servo attach alternative way?

Good day!

I've been doing a project on RC vehicle using LN298N Dual H-Bridge DC and a servo that can rotate approximately 180 degrees. When using servo library such as <Servo.h> and <VarSpeedServo.h>, the RC vehicle's PWM malfunction. Now, I did some research and found out that Pin 9 and 10 on arduino UNO's PWM capabilites are disabled using those libraries.

I tried switching the pins since there are other pins that are PWM in Arduino UNO. It seems that PIN 10 is still working properly for it still performs PWM unlike the other pins. Now, I would like to ask for your advice on the code on what are the alternative ways to deal with this problem or solutions you may know.

#include <SoftwareSerial.h>
#define DEBUG true
//#include <Servo.h>
#include <VarSpeedServo.h> 

   
SoftwareSerial esp8266(2,3); // make RX Arduino line is pin 2, make TX Arduino line is pin 3.
VarSpeedServo myservo;

int L298N_IN1 = 5;
int L298N_IN2 = 6;
int speedPinA = 9;

int L298N_IN3 = 7;
int L298N_IN4 = 8;
int speedPinB = 10;

char letter;
int PWMvalue;
int PWMleftright;
unsigned long current;
unsigned long previous;
unsigned long current1;
unsigned long previous1;
boolean PWMmode = false;

//Servo myservo;
//int pos = 0;

void setup() {

pinMode(L298N_IN1,OUTPUT);
pinMode(L298N_IN2,OUTPUT);
pinMode(speedPinA,OUTPUT);
pinMode(L298N_IN3,OUTPUT);
pinMode(L298N_IN4,OUTPUT);
pinMode(speedPinB,OUTPUT);

  letter = 'A';
  myservo.attach(11);
  myservo.write(45);
  PWMvalue = 30;
  Serial.begin(9600); //for monitoring purposes
  esp8266.begin(9600); // your esp's baud rate might be different

  sendCommand("AT+RST\r\n",2000,DEBUG); // reset module
  sendCommand("AT+CWMODE=1\r\n",1000,DEBUG); // configure as access point
  sendCommand("AT+CWJAP=\"ABCDEFG\",\"12345678\"\r\n",3000,DEBUG);
  delay(1000); 
  sendCommand("AT+CIFSR\r\n",1000,DEBUG); // get ip address
  sendCommand("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections
  sendCommand("AT+CIPSERVER=1,6625\r\n",1000,DEBUG); // turn on server on port 80
  
  Serial.println("Server Ready");
}

void loop() {

   
   if(esp8266.available()) {
      if(esp8266.find("+IPD,0,")){
          delay(10);
          esp8266.find(":");                    
          delay(10);
    
          letter = esp8266.read();
          Serial.print(letter);//for monitoring purposes
          
        
    }

}

control(letter);
if(PWMmode == true){
  
   PWMincrease();
   
   Serial.println("PWMmode is ON");
   Serial.println(PWMvalue);
   
   
  }
}///////end of LOOP


void PWMincrease(){
  
    current = millis();
    if (current - previous >= 500) {
      previous = current;

      PWMvalue = PWMvalue + 20;
      
      if(PWMvalue >= 255){
        PWMvalue = 255;
      }
      control(letter);
    }
}

void startCar(){
  
      digitalWrite(L298N_IN1, HIGH);
      digitalWrite(L298N_IN2, LOW);
      digitalWrite(L298N_IN3, LOW);
      digitalWrite(L298N_IN4, HIGH);
      analogWrite(speedPinA, PWMvalue);
      analogWrite(speedPinB, PWMvalue);
      
      Serial.println(PWMvalue);


}

void stopCar(){
  digitalWrite(L298N_IN1, LOW);
  digitalWrite(L298N_IN2, LOW);
  digitalWrite(L298N_IN3, LOW);
  digitalWrite(L298N_IN4, LOW);
  analogWrite(speedPinA, 0);
  analogWrite(speedPinB, 0); 
  Serial.println(PWMvalue);

  
}
void leftTurn(){
  
digitalWrite(L298N_IN1, HIGH);
digitalWrite(L298N_IN2, HIGH);
digitalWrite(L298N_IN3, LOW);
digitalWrite(L298N_IN4, LOW);
analogWrite(speedPinA, 200);
analogWrite(speedPinB, 110); //LEFT 
}
void rightTurn(){
  
digitalWrite(L298N_IN1, LOW);
digitalWrite(L298N_IN2, LOW);
digitalWrite(L298N_IN3, HIGH);
digitalWrite(L298N_IN4, HIGH);
analogWrite(speedPinA, 110);
analogWrite(speedPinB, 200); //RIGHT
}
void reverse(){
  
digitalWrite(L298N_IN1, LOW);
digitalWrite(L298N_IN2, HIGH);
digitalWrite(L298N_IN3, HIGH);
digitalWrite(L298N_IN4, LOW);
analogWrite(speedPinA, PWMvalue);
analogWrite(speedPinB, PWMvalue); 
Serial.println(PWMvalue);

}

void positivetiltcamera(){
  myservo.write(45, 15, true);
}
void negativetiltcamera(){
  myservo.write(0, 15, true);
}

void control(char cmd) {

  switch (cmd) {
    case 'Q':
      PWMmode = true;
      startCar();
      break;

    case 'W':
      PWMmode = false;
      stopCar();
      PWMvalue = 30;
      break;
    
    case 'A':
      PWMmode = false;
      PWMvalue = 30;
      break;
      
    case 'P':
      PWMmode = false;
      stopCar();
      PWMvalue = 30;
      break;            

    case 'J':
      PWMmode = false;
      leftTurn();
      PWMvalue = 30;
      break;

    case 'H':
      PWMmode = false;
      rightTurn();
      PWMvalue = 30;
      break;

    case 'Y':
      PWMmode = true;
      reverse();
      break; 
      
    case 'L':
      positivetiltcamera();
      break;  
            
    case 'R':
      negativetiltcamera();
      break; 
  }
}


String sendCommand(String command, const int timeout, boolean debug)
{
    String response = "";
           
    esp8266.print(command); // send the read character to the esp8266
    
    long int time = millis();
    
    while( (time+timeout) > millis())
    {
      while(esp8266.available())
      {
        
        // The esp has data so display its output to the serial window 
        char c = esp8266.read(); // read the next character.
        response+=c;
      }  
    }
    
    if(debug)
    {
      Serial.print(response);
    }
    
    return response;
}

Simple solution - don't use either pins 9 or 10 for PWM. And any pin can be used for controlling the servo.

More complicated solution, if, for some reason it is essential to use pin 9 or pin 10, is to use the ServoTimer2 library which disables the PWM pins used by Timer2 rather than Timer1. Note that this library only works with writeMicroseconds()

...R

ok I'll do what you tell me, and then I'll tell you how I was in the modifications, thanks