I have an interesting problem. I want to control ESC and servo with Arduino. I'm using HC-12 to communicate with 2 Arduino and I'm using SoftwareSerial on the nano.
#include <SoftwareSerial.h>
#include <Servo.h>
SoftwareSerial mySerial(2, 3); // RX, TX
Servo esc,servo1;
String okunan;
int escval;
void setup() {
okunan="";
esc.attach(9);
servo1.attach(10);
mySerial.begin(2400);
Serial.begin(9600);
}
void loop() {
while (mySerial.available()) {
delay(3); //delay to allow buffer to fill
char c = mySerial.read(); //gets one byte from serial buffer
okunan += c; //makes the string readString
}
if(okunan.length()>0)
{
Serial.println(okunan);
//escval= map(okunan.substring(0,okunan.indexOf(',')).toInt(),0,1023,,2000);
servo1.writeMicroseconds(map(okunan.toInt(),0,179,1000,2000));
okunan="";
mySerial.flush();
}
delay(20);
}
When I use a potentiometer, It works successfully. But When I use with serial and if three digit value(between 0-179, for example, 120) comes from serial(HC-12), motor works and stops continuous and irregular. If the value is under three digit there is no problem.
Also, if I delete the ESC variable(servo) or add a new servo to code, it always starts and stops interestingly(Using or not it doesn't matter. If I add Servo servo2 etc., it is going crazy.
I've tried on Uno. That is the same.
Thanks in advance...