HC 12 with Servo

I am trying to use 2 hc12 modules to control a 120 ’ servo with arduino and a joystick . The problem is that every 2 or less seconds the servo shakes a little bit .
I hope there is solution to this problem .

Transmitter code

#include <SoftwareSerial.h>

int ROT ;

SoftwareSerial HC12(10, 11); 

void setup() {

  Serial.begin(9600);             

  HC12.begin(9600);               
  
}
void loop() {

int ROT = analogRead(A0);

ROT = map(ROT , 515 , 1023 , 60 , 120) ;

Serial.println(ROT);

HC12.write(ROT);

delay(100);


  }

Reciever code

#include <Servo.h>

#include <SoftwareSerial.h>

int ROT ;

Servo myServo;

SoftwareSerial HC12(10,11); 

void setup() {
  
myServo.attach(6);
  
HC12.begin(9600); 
  
Serial.begin(9600);

  
}
void loop() {
          
   while ( HC12.available()>0) {       
   
   ROT = HC12.read();
   Serial.println(ROT);
   }
  
   myServo.write(ROT);
   delay(100);
}

P.S. The delay added makes the servo shake less violent or thats what I noticed .

I thought that SoftwareSerial and Servo conflicted over the timer.
I see people using servotimer2 to get around this.

never heard of this library before .Thanks I will give it a try

cant quite understand the code

// this sketch cycles three servos at different rates 

#include <ServoTimer2.h>  // the servo library

// define the pins for the servos
#define rollPin  2
#define pitchPin 3
#define yawPin   4

ServoTimer2 servoRoll;    // declare variables for up to eight servos
ServoTimer2 servoPitch; 
ServoTimer2 servoYaw; 
 
void setup() {
  servoRoll.attach(rollPin);     // attach a pin to the servos and they will start pulsing
  servoPitch.attach(pitchPin); 
  servoYaw.attach(yawPin); 
}


// this function just increments a value until it reaches a maximum 
int incPulse(int val, int inc){
   if( val + inc  > 2000 )
      return 1000 ;
   else
       return val + inc;  
}

void loop()
{ 
 int val;
  
   val = incPulse( servoRoll.read(), 1);
   servoRoll.write(val);

   val =  incPulse( servoPitch.read(), 2);
   servoPitch.write(val);
   
   val = incPulse(servoYaw.read(), 4);
   servoYaw.write(val);
   
   delay(10);   
}

Anyone can explain me in detail what each line of code does?
PS I want to control only one motor so If anyone can modify the code to control only with one servo I would appreciate it

You can use your original code but instead of write() taking an angle from 60 - 120 it uses microseconds so probably something like 1200 to 1800. You'll have to calibrate the exact numbers yourself but that will be a starting point.

Just change the map() in the transmitter part to map to 1200-1800 instead of 60-120.

Steve

Well here is the code as you said . The thing is that now it makes some weird vibrating sounds and the maping needs to be modified because the center of the joystick isn’t the center of the servo . Also the shaking has been reduced dramatically
transmitter :

#include <SoftwareSerial.h>

int ROT ;
SoftwareSerial HC12(10, 11); 
void setup() {
  Serial.begin(9600);             
  HC12.begin(9600);               
  
}
void loop() {

int ROT = analogRead(A0);
ROT = map(ROT , 515 , 1023 , 1200 , 1800) ;
Serial.println(ROT);
HC12.write(ROT);
delay(100);


  }

I did say you would need to calibrate the mapping to suit your servo setup. If the end to end movement is the correct amount just move both numbers together until the centre is where you want (or physically move the servo arm round a bit!).

As for weird sounds that may be just because you don't have it properly centred yet. Otherwise when exactly is it vibrating?

Steve

okay I will experiment with it a little bit and give you feedback later . Thanks

I would break it down into steps.
Just read the joystick and print the values.
Note the values with the joystick full throw one way and full throw the other way.

Then put those values in the map()

Well I ve almost spent the whole day trying to map it but nothing seems to work :o . Anyone interested in helping me with the map thing:( ?

There are a lot of us who will help.
Are you printing out the value read from the joystick? This value, before mapping, is important to know.
What is the highest value at full throw one way?
What is the lowest value at full throw the other way?

"Nothing seems to work" ain't very helpful. Please post your current code for both parts so we can see what you've done.

In the transmitter part you should have changed from

ROT = map(ROT , 515 , 1023 , 60 , 120) ;

to the new

ROT = map(ROT , 515 , 1023 , 1200 , 1800) ;

The receiver part needed no change other than to include ServoTimer2.h. But check the output of your Serial.println(ROT) to make sure it's receiving the correct values.

When you do that what exactly is the problem you get? Is the range of movement from 1200 to 1800 correct or too much or not enough? Is the centring wrong? Which way, towards the 1200 side or 1800 side ?

Steve

joystick mid value is 527 max is 1023 and min is 0
I changed it to 515 in code because the number that I got was neative on the transmitter side and 255 on the receiver side ( yeah for some reason when it gets a negative value it goes staight to 255…
Here is the updated code (not working)
Transmitter:

#include <SoftwareSerial.h>

int ROT ;
SoftwareSerial HC12(10, 11); 
void setup() {
  Serial.begin(9600);             
  HC12.begin(9600);               
  
}
void loop() {

int ROT = analogRead(A0);
ROT = map(ROT , 515 , 1023 , 1200  , 1800) ;
Serial.println(ROT);
HC12.write(ROT);
delay(100);


  }

Reciever :

#include <Servo.h>
#include <SoftwareSerial.h>

int ROT ;
int Speed;
int Rotor = 0 ;
int Rotor1 = 1 ;
Servo myServo;
SoftwareSerial HC12(10,11); 
void setup() {
  myServo.attach(6);
  HC12.begin(9600); 
  Serial.begin(9600);

  
}
void loop() {
          
   while ( HC12.available()>0) {       
   
   ROT = HC12.read();
   Serial.println(ROT);
   }

   myServo.write(ROT);
   delay(100);

  
 
}

P.S. I think that I shoud mention again that this is an 120 degree servo motor

Oh dear...what happened to changing Servo.h to ServoTimer2.h?

And you still haven't said if you're seeing the correct 1200 - 1800 values coming through in the receiver. Or what the actual problems are. You may be able to see what's happening but the rest of us can't.

BTW I think I may have spotted a reason for the vibrating...you're writing to the servo every time round the loop even when no command has been received. See if this

void loop() {
          
   while ( HC12.available()>0) {       
   
   ROT = HC12.read();
   Serial.println(ROT);

   myServo.write(ROT);

   }
}

is any better.

Steve

Well I found the optimal mapping values but the servo is still shaking. Here is the code :
Transmitter :

#include <SoftwareSerial.h>

int ROT ;
SoftwareSerial HC12(10, 11); 
void setup() {
  Serial.begin(9600);             
  HC12.begin(9600);               
  
}
void loop() {

int ROT = analogRead(A0);
ROT = map(ROT , 515 , 1023 , 1585  , 1800) ;
Serial.println(ROT);
HC12.write(ROT);
delay(100);


  }

Reciever :

#include <Servo.h>
#include <SoftwareSerial.h>

int ROT ;

Servo myServo;
SoftwareSerial HC12(10,11); 
void setup() {
  myServo.attach(6);
  HC12.begin(9600); 
  Serial.begin(9600);

  
}
void loop() {
          
   while ( HC12.available()>0) {       
   
   ROT = HC12.read();
   Serial.println(ROT);
   
 
    myServo.write(ROT);
  delay(100);

 
   }
   }

Any ideas?

You STILL have Servo.h in there. I give up!

Steve

Sorry wrong code
Transmitter :

#include <SoftwareSerial.h>

int ROT ;
SoftwareSerial HC12(10, 11); 
void setup() {
  Serial.begin(9600);             
  HC12.begin(9600);               
  
}
void loop() {

int ROT = analogRead(A0);
ROT = map(ROT , 515 , 1023 , 1100  , 1800) ;
Serial.println(ROT);
HC12.println(ROT);
delay(150);


  }

Receiver :

#include <ServoTimer2.h>
#include <SoftwareSerial.h>

int ROT ;

ServoTimer2 myServo;
SoftwareSerial HC12(10,11); 
void setup() {
  myServo.attach(6);
  HC12.begin(9600); 
  Serial.begin(9600);

  
}
void loop() {
          
   while ( HC12.available()>0) {       
   
   ROT = HC12.parseInt();
   Serial.println(ROT);
   
 
    myServo.write(ROT);
  delay(150);

 
   }

Hello,

I have the same vibration problem. I have even tried to install capacitors but it doesn't work. I have also tried to use the library ServoTimer2 instead of Servo but it shows an error of compatibility with Arduino Nano....

Have anyone being able to solve this issue?

Thanks in advance!!