Skip to main content

I have aan interesting problem. I want to control escESC and servo with arduinoArduino. I'm using hcHC-12 to communicate betweenwith 2 arduinosArduino and I'm using softwareserialSoftwareSerial 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);
  }
#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(hcHC-12), motor works and stops continuous and irregular. If the value is under three digit there is no problem.

Also, if I delete the escESC variable(servo) or add a new servo to code, it always startstarts and stopstops interestingly(Using or not it doesn't matter. If I add Servo servo2 etc., it is going crazy.

I have a interesting problem. I want to control esc and servo with arduino. I'm using hc-12 to communicate between 2 arduinos 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 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 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 start and stop interestingly(Using or not it doesn't matter. If I add Servo servo2 etc., it is going crazy.

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.

Source Link

Arduino Servo with Serial

I have a interesting problem. I want to control esc and servo with arduino. I'm using hc-12 to communicate between 2 arduinos 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 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 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 start and stop 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...