Arduino Forum

Topics => Home Automation and Networked Objects => Topic started by: FBW921 on May 09, 2019, 07:55 pm

Title: Servo with HC-12 Module is twitching
Post by: FBW921 on May 09, 2019, 07:55 pm
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);


  }



Title: Re: Servo with HC-12 Module is twitching
Post by: tasmod on May 09, 2019, 08:44 pm
In the transmitter, does the Serial.println(ROT);  produce a steady value in the serial monitor?

And the same for the receiver?
Title: Re: Servo with HC-12 Module is twitching
Post by: FBW921 on May 11, 2019, 05:08 pm
Yes it does.
Title: Re: Servo with HC-12 Module is twitching
Post by: FBW921 on May 11, 2019, 05:14 pm
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.
Title: Re: Servo with HC-12 Module is twitching
Post by: rw950431 on May 12, 2019, 03:24 pm
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);

 
   }   }
Title: Re: Servo with HC-12 Module is twitching
Post by: FBW921 on May 13, 2019, 03:21 am
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.
Title: Re: Servo with HC-12 Module is twitching
Post by: rw950431 on May 13, 2019, 11:36 am
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?
Title: Re: Servo with HC-12 Module is twitching
Post by: FBW921 on May 14, 2019, 04:55 am
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
Title: Re: Servo with HC-12 Module is twitching
Post by: FBW921 on May 14, 2019, 06:02 am
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.
Title: Re: Servo with HC-12 Module is twitching
Post by: rw950431 on May 14, 2019, 12:40 pm
Or you could use the inbuilt hardware serial ports- same wiring just a different pair of pins.

Glad to see you are making progress. 
Title: Re: Servo with HC-12 Module is twitching
Post by: FBW921 on May 15, 2019, 04:53 am
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
Title: Re: Servo with HC-12 Module is twitching
Post by: FBW921 on May 22, 2019, 06:35 am
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);


  }
Title: Re: Servo with HC-12 Module is twitching
Post by: PaulS on May 22, 2019, 11:43 am
Quote
any suggestions rw?
Quit creating new threads about the same problem.
Title: Re: Servo with HC-12 Module is twitching
Post by: FBW921 on May 24, 2019, 05:20 am
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?
Title: Re: Servo with HC-12 Module is twitching
Post by: PaulS on May 24, 2019, 11:04 am
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.
Title: Re: Servo with HC-12 Module is twitching
Post by: FBW921 on May 25, 2019, 02:36 am
The arduino NANO (my transmitter in this case) has two ports, pins 0 (RX) and 1 (TX)

My receiving device- the arduino mega has 8 -

Mega

0(RX), 1(TX)

19(RX), 18(TX)

17(RX), 16(TX)

15(RX), 14(TX)


Since both devices will be disconnected from any CPU (unless new code is being uploaded), I think I should be good on the number of hardware serial ports. The question is how to go about the coding?
Title: Re: Servo with HC-12 Module is twitching
Post by: PaulS on May 28, 2019, 04:54 pm
Quote
The arduino NANO (my transmitter in this case) has two ports, pins 0 (RX) and 1 (TX)
A serial port consists of an input pin and an output pin, so you have ONE port on the Nano and FOUR on the Mega.

Quote
The question is how to go about the coding?
To do what?

Quote
Since both devices will be disconnected from any CPU
Then, just use Serial on each to talk to the HC-12.
Title: Re: Servo with HC-12 Module is twitching
Post by: FBW921 on May 30, 2019, 06:46 am
How do I use Serial (hardware serial) to communicate between the two hc-12s? I know how to use software serial to do it but not hardware serial. Here's some more direct questions since you like specific questions: Which functions can I directly swap out between software serial and hardware serial? Here's my controller code:

#include <SoftwareSerial.h>

int ROT ;
SoftwareSerial 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);








What do I #include for harware serial?
Does "SoftwareSerial HC12(10, 11);" stay?
Do I use Serial.read() anywhere?
Title: Re: Servo with HC-12 Module is twitching
Post by: PaulS on May 30, 2019, 02:04 pm
Quote
Which functions can I directly swap out between software serial and hardware serial?
The public methods of the HardwareSerial class and the SoftwareSerial class are the same. So, any method of the SoftwareSerial class that you use can be used with the HardwareSerial instance (Serial) instead.

Quote
What do I #include for harware serial?
Nothing.

Quote
Does "SoftwareSerial HC12(10, 11);" stay?
No.

Quote
Do I use Serial.read() anywhere?
If you want to read from the HC-xx, after you connect it to the hardware serial pins, yes. And, if you want to talk to the HC-xx, you use Serial.print() and/or Serial.println() and/or Serial.write().
Title: Re: Servo with HC-12 Module is twitching
Post by: FBW921 on Jun 02, 2019, 10:07 pm
I will try to use hardware serial given this new information, thank you for your answers.