Go Down

Topic: Servo with HC-12 Module is twitching (Read 275 times) previous topic - next topic

FBW921

Hello friends, I'm trying to control a Servo with an HC-12 wireless module, it works well except it twitches ever few seconds or so. Anybody know a quick fix?

Thanks

Equipment:

HC-12 Wireless Module
J-Deal Micro Servo 9g SG90
Transmitter: Arduino Nano with standard joystick
Reciever: Arduino Mega

Code is attached but also reproduced here:




RECEIVER:




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

int ROT ;

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

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

 
   }   }







CONTROLLER/TRANSMITTER:





#include <SoftwareSerial.h>

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

int ROT = analogRead(A3);
//ROT = map(ROT , 515 , 1023 , 1100  , 1800) ;
ROT = map(ROT , 0 , 1023 , 760  , 1800) ;
Serial.println(ROT);
HC12.println(ROT);
delay(50);


  }




tasmod

#1
May 09, 2019, 08:44 pm Last Edit: May 09, 2019, 08:47 pm by tasmod
In the transmitter, does the Serial.println(ROT);  produce a steady value in the serial monitor?

And the same for the receiver?

FBW921


FBW921

Yeah also for the receiver, the value can be held totally constant as i keep the joy stick in the exact same position and the serial moniter confirms it in both cases and yet the twitching continues.

rw950431

Is there a particular reason to use SoftwareSerial instead of the hardware serial ports? 

does the twitching stop when you use this code for the receiver?

Code: [Select]
void loop() {
         
   while (1) {       
   
   ROT = 0;  //or other constant value
   Serial.println(ROT);
   
 
    myServo.write(ROT);
  delay(50);

 
   }   }

FBW921

I'm not sure what to add to your code to get it to work but I tried this code:

#include <ServoTimer2.h>




int ROT ;
ServoTimer2 myServo;

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

void loop() {
         
   while (1) {       
   
   ROT = 0;  //or other constant value
   Serial.println(ROT);
   
 
    myServo.write(ROT);
  delay(50);

 
   }   }



And it did not work, it says that HC-12 was not declared in this scope.


I am using software serial because everywhere I've checked this method seems to be the norm, I'm not sure how to initiate anything using the hardware ports

Thanks for your response.

rw950431

You almost had it right.  Just needed to include the header and declare it

Code: [Select]

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

int ROT ;

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

 
}
void loop() {
         
   while (1) {       
   
   ROT = 0;
   Serial.println(ROT);
   
 
    myServo.write(ROT);
  delay(50);

 
   }   }


If this still shows the servo twitching comment out all references to software serial and try again like


Code: [Select]

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

int ROT ;

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

 
}
void loop() {
         
   while (1) {       
   
   ROT = 0;
   Serial.println(ROT);
   
 
    myServo.write(ROT);
  delay(50);

 
   }   }


If this still shows twitching perhaps you have a hardware problem?

FBW921

Hey RW95, I tried both sets of code with the receiving end. With the first set of code, the twitching continued but the servo stayed locked at the "0" (serial monitor) value and did not respond to my joy stick inputs. With the second set of code the servo also stayed locked at the zero position and didn't respond to joystick values BUT there was NO twitching.

I tried swapping out servos but the twitching continues. My arduino Mega is a brand new original but my nano is one of those generic amazon kinds, both have functioned well so far so it's probably not a hardware issue.

Any ideas?

Thanks again

Sean

FBW921

I just found that someone apparently found a solution to the jitter by using the NeoSwSerial.h library instead of the softwareserial.h library, something about softwareserial produces interuptions whereas Neo does not. I'm going to try to figure out how to swap the two librarys in the code and ill let you know if I can get it to work.

rw950431

Or you could use the inbuilt hardware serial ports- same wiring just a different pair of pins.

Glad to see you are making progress. 

FBW921

What would the code look like if I were to use the hardware pins? And thank you I've been so busy lately but I will be sure to post updates on whether the Neoswserial works.

Sean

FBW921

I swapped out the softwareSerial.h code with NeoSWSerial.h, the result was that I couldnt get any sort of signal and the servo stayed ultra-still. Here is the code- any suggestions rw?


#include <ServoTimer2.h>
#include <NeoSWSerial.h>

int ROT ;

ServoTimer2 myServo;
NeoSWSerial HC12(10,11); //(tx,rx)
void setup() {
  myServo.attach(7);
  HC12.begin(9600);
  Serial.begin(9600);

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

 
   }   }









Controller--






#include <NeoSWSerial.h>

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

int ROT = analogRead(A3);
//Serial.println(ROT);
//ROT = map(ROT , 515 , 1023 , 1100  , 1800) ;
ROT = map(ROT , 0 , 876 , 760  , 1800) ;
Serial.println(ROT);
HC12.println(ROT);
delay(50);


  }

PaulS

Quote
any suggestions rw?
Quit creating new threads about the same problem.
The art of getting good answers lies in asking good questions.

FBW921

Quit creating new threads about the same problem.
Care to explain (or better yet show) how I would exchange hardware serial communication with the software serial?

PaulS

Care to explain (or better yet show) how I would exchange hardware serial communication with the software serial?
How many hardware serial ports does your board have? The only reason for using software serial is because you don't have enough hardware serial ports.

If you are trying to talk to two serial devices, and only have one hardware serial port, then you have to use software serial. Or, get a different board.

So, how many hardware serial ports do you have? How many serial devices are you trying to talk to? Don't forget the PC as one of them.
The art of getting good answers lies in asking good questions.

Go Up