Hello, I’m trying to make a RC car whose controller is a smartphone with blynk. I have a H bridge to control direction of the motor and speed and a servo motor for the steering mechanism. I’m using a hm-10 BLE module to connect to the smartphone. On the app I have a joystick (to select direction - forward, backwards - and to control the servo) and a slider to control the speed.
The problem is that the motor works either full speed or not at all. If I don’t include the servo library I’m able to smoothly variate the motor speed, while if I include the library it is either completely on or off.
This is my code, could you lend me a hand? Thank you very much for your time and patience.
#define BLYNK_PRINT Serial
#define TXD 11
#define RXD 10
#include <SoftwareSerial.h>
SoftwareSerial SwSerial(TXD, RXD); // TXD, RXD
#include <BlynkSimpleSerialBLE.h>
#include <SoftwareSerial.h>
#include <Servo.h>
Servo myServo;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "asdasdasd";
SoftwareSerial SerialBLE(TXD, RXD); // TXD, RXD
const int activationPin = 9; //turn motor on off -> H bridge
const int controlPin1 = 3; //set direction -> H bridge
const int controlPin2 = 4; //set direction -> H bridge
const int servoPin = 6;
int speed = 0;
BLYNK_WRITE(V1)
{
speed = param.asInt();
Serial.println(speed);
analogWrite(activationPin, speed);
}
BLYNK_WRITE(V3)
{
if (param[1].asInt() >= 0)
{
digitalWrite(controlPin1, HIGH);
digitalWrite(controlPin2, LOW);
}
else
{
digitalWrite(controlPin2, HIGH);
digitalWrite(controlPin1, LOW);
}
int angle = param[0].asInt();
if (angle >= 168)
angle = 168;
else if (angle <= 16)
angle = 16;
myServo.write(angle);
//Blynk.virtualWrite(V2, angle);
}
void setup()
{
//pin mode
pinMode(controlPin1, OUTPUT);
pinMode(controlPin2, OUTPUT);
pinMode(activationPin, OUTPUT);
//digitalWrite(controlPin1,LOW);
//digitalWrite(controlPin2,LOW);
// Debug console
Serial.begin(9600);
SerialBLE.begin(9600);
Blynk.begin(SerialBLE, auth);
Serial.println("Waiting for connections...");
myServo.attach(servoPin);
}
void loop()
{
Blynk.run();
}