Hc 12 changing servomotor controll

Somehow my arduino is changing which servo the Joystick controlls.
I have to Joysticks, with x and y and x, y, and z(rotation of the joystick.

If i restart the arduino or reset the layout changes sometime the right joystick controlls servo 1 and sometimes servo 2.

In total there should be 5 Servos but i have only hooked up 2 atm.

Thanks for your help

Code:
Sender:

#include <SoftwareSerial.h>

int A ;
int B ;
int C ;
int D ;
int E ;


SoftwareSerial HC12(2, 3); 
void setup() {
  Serial.begin(9600);             
  HC12.begin(9600);               
  
}
void loop() {

int A = analogRead(A0);
A = map(A , 0 , 1023 , 900  , 2000) ;
Serial.println(A);
HC12.println(A);

int B = analogRead(A1);
B = map(B , 0 , 1023 , 900  , 2000) ;
Serial.println(B);
HC12.println(B);

int C = analogRead(A2);
C = map(C , 0 , 1023 , 900  , 2000) ;
Serial.println(C);
HC12.println(C);

int D = analogRead(A3);
D = map(D , 0 , 1023 , 0  , 1000) ;
Serial.println(D);
HC12.println(D);

int E = analogRead(A4);
E = map(E , 0 , 1023 , 900  , 2000) ;
Serial.println(E);
HC12.println(E);

delay(150);


  }

Reciver:

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

int A ;
int B ;
int C ;
int D ;
int E ;


ServoTimer2 Servo1;
ServoTimer2 Servo2;
ServoTimer2 Servo3;
ServoTimer2 Servo4;
ServoTimer2 Servo5;

SoftwareSerial HC12(2,3); 
void setup() {
 
  Servo1.attach(5);
  Servo2.attach(6);
  Servo3.attach(9);
  Servo4.attach(10);
  Servo5.attach(11);
  HC12.begin(9600); 
  Serial.begin(9600);

  
}
void loop() {
          
   while ( HC12.available()>0) {       
   
   A = HC12.parseInt();
   Serial.println(A);
    Servo1.write(A);

    B = HC12.parseInt();
   Serial.println(B);
    Servo2.write(B);

    C = HC12.parseInt();
   Serial.println(C);
    Servo3.write(C);

    D = HC12.parseInt();
   Serial.println(D);
    Servo4.write(D);

    E = HC12.parseInt();
   Serial.println(E);
    Servo5.write(E);

  delay(150);

 
   }
}

The way that you are reading the HC12 serial port there is no guarantee which incoming int will be read into which variable. I would suggest that you read the joysticks and put all the readings into one comma delimited packet to send. Then at the other end parse the packet into the variables for the servos. See the serial input basics tutorial.

Example code with one joystick(tested). Based on code from the serial input basics tutorial. Easily expanded to more joysticks and axes.

Sender

#include <SoftwareSerial.h>

const byte xPin = A0;
const byte yPin = A1;

SoftwareSerial ss(4, 7);

void setup()
{
   Serial.begin(115200);
   ss.begin(9600);
}

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 1000;
   if (millis() - timer >= interval)
   {
      timer = millis();
      // read joysticks
      int xValue = analogRead(xPin);
      int yValue = analogRead(yPin);
      // print values to serial monitor
      Serial.print("x value = ");
      Serial.print(xValue);
      Serial.print("   y value = ");
      Serial.println(yValue);
      // create comma delmited packet
      char buffer[15];
      snprintf(buffer, 14, "%d,%d", xValue, yValue);
      Serial.println(buffer);
      // send packet
      ss.println(buffer);
   }
}

Reciever

#include <SoftwareSerial.h>

int xValue = 0;
int yValue = 0;

const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

SoftwareSerial ss(4, 7);

void setup()
{
   Serial.begin(115200);
   ss.begin(9600);
}

void loop()
{
 recvWithEndMarker();
   //showNewData();
   if (newData)
   {
      parseData();
      displayData();
      newData = false;
   }
}

void recvWithEndMarker()
{
   static byte ndx = 0;
   char endMarker = '\n';
   char rc;

   while (ss.available() > 0 && newData == false)
   {
      rc = ss.read();

      if (rc != endMarker)
      {
         receivedChars[ndx] = rc;
         ndx++;
         if (ndx >= numChars)
         {
            ndx = numChars - 1;
         }
      }
      else
      {
         receivedChars[ndx] = '\0'; // terminate the string
         ndx = 0;
         newData = true;
      }
   }
}

void showNewData()
{
   if (newData == true)
   {
      Serial.print("This just in ... ");
      Serial.println(receivedChars);
      //newData = false;
   }
}

void parseData()
{
   char *strings[2]; // an array of pointers to the pieces of the above array after strtok()
   char *ptr = NULL; byte index = 0;
   ptr = strtok(receivedChars, ",");  // delimiter comma
   while (ptr != NULL)
   {
      strings[index] = ptr;
      index++;
      ptr = strtok(NULL, ",");
   }
      
   // convert string data to numbers
   xValue = atoi(strings[0]);
   yValue = atoi(strings[1]);   
}

void displayData()
{
   Serial.print("x value = ");
   Serial.print(xValue);
   Serial.print("   y value = ");
   Serial.print(yValue);
   
   Serial.println(); // blank line  
}

Reference for strtok().

Hello truckdriver101

Take a search engine of your choice and ask the WWW for 'send struct via HC12'.

Thanks a lot for your answers,

sadly im ah complete beginner in programming,

I managed to controll the 2 servos, however when im adapting for 5 servos it is not working, and now ah servo is broken it is only moving one way and not back while the other one seems to be working, I have 5 volts from ah converter going to the arduino and to the bosst converter which boosts the voltage to 6.8volts which is the recommended for the servos(for maximum torque).

Is there anything i am missing or why my servos keep breaking this is the 3rd one which is doing this.

I am tired of destroying my servos so i thought i ask you guys
thanks a lot!

Here is the code
I think the sender one is right but the reciver one is missing something:

And the problem with the Servos i still dont get, altough these should be high quality servos:
Miuzei 270° Digital RC Servo Motor 20KG

Maybe it would be better to use ah Servo Motor Drive Module but i have no idea how to programm that in combination with the HC12

Sender:

#include <SoftwareSerial.h>

const byte Ax = A0;
const byte Bx = A1;
const byte Cx = A2;
const byte Dx = A3;
const byte Ex = A4;

SoftwareSerial ss(2, 3);

void setup()
{
   Serial.begin(9600);
   ss.begin(9600);
}

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 250;
   if (millis() - timer >= interval)
   {
      timer = millis();
      // read joysticks
      int A = analogRead(Ax);
      A = map(A , 0 , 1023 , 900  , 2000) ;

      int B = analogRead(Bx);
      B = map(B , 0 , 1023 , 900  , 2000) ;

      int C = analogRead(Cx);
      C = map(C , 0 , 1023 , 900  , 2000) ;

      int D = analogRead(Dx);
      D = map(D , 0 , 1023 , 900  , 2000) ;

      int E = analogRead(Ex);
      E = map(E , 0 , 1023 , 900  , 2000) ;

      // print values to serial monitor
     
      Serial.print(A);
      Serial.println(B);


      // create comma delmited packet
      char buffer[15];
      snprintf(buffer, 14, "%d,%d", A, B);
      Serial.println(buffer);
      // send packet
      ss.println(buffer);
   }
}

Reciver:
for 2 Servos

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

int A = 0;
int B = 0;
int C = 0;
int D = 0;
int E = 0;


const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

SoftwareSerial ss(2, 3);

ServoTimer2 Servo1;
ServoTimer2 Servo2;
ServoTimer2 Servo3;
ServoTimer2 Servo4;
ServoTimer2 Servo5;

void setup()
{
   Serial.begin(9600);
   ss.begin(9600);

 Servo1.attach(5);
 Servo2.attach(6);
 Servo3.attach(9);
 Servo4.attach(10);
 Servo5.attach(11);
}

void loop()
{
 recvWithEndMarker();
   //showNewData();
   if (newData)
   {
      parseData();
      displayData();
      newData = false;
   }
}

void recvWithEndMarker()
{
   static byte ndx = 0;
   char endMarker = '\n';
   char rc;

   while (ss.available() > 0 && newData == false)
   {
      rc = ss.read();

      if (rc != endMarker)
      {
         receivedChars[ndx] = rc;
         ndx++;
         if (ndx >= numChars)
         {
            ndx = numChars - 1;
         }
      }
      else
      {
         receivedChars[ndx] = '\0'; // terminate the string
         ndx = 0;
         newData = true;
      }
   }
}

void showNewData()
{
   if (newData == true)
   {
      Serial.print("This just in ... ");
      Serial.println(receivedChars);
      //newData = false;
   }
}

void parseData()
{
   char *strings[2]; // an array of pointers to the pieces of the above array after strtok()
   char *ptr = NULL; byte index = 0;
   ptr = strtok(receivedChars, ",");  // delimiter comma
   while (ptr != NULL)
   {
      strings[index] = ptr;
      index++;
      ptr = strtok(NULL, ",");
   }
      
   // convert string data to numbers
   A = atoi(strings[0]);
   B = atoi(strings[1]); 
   
}
void displayData()
{
   Servo4.write(A);
   Servo5.write(B);
  
   Serial.println(); // blank line  
}

Reciver adapted for 5 Servos ?

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

int A = 0;
int B = 0;
int C = 0;
int D = 0;
int E = 0;


const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

SoftwareSerial ss(2, 3);

ServoTimer2 Servo1;
ServoTimer2 Servo2;
ServoTimer2 Servo3;
ServoTimer2 Servo4;
ServoTimer2 Servo5;

void setup()
{
   Serial.begin(9600);
   ss.begin(9600);

 Servo1.attach(5);
 Servo2.attach(6);
 Servo3.attach(9);
 Servo4.attach(10);
 Servo5.attach(11);
}

void loop()
{
 recvWithEndMarker();
   //showNewData();
   if (newData)
   {
      parseData();
      displayData();
      newData = false;
   }
}

void recvWithEndMarker()
{
   static byte ndx = 0;
   char endMarker = '\n';
   char rc;

   while (ss.available() > 0 && newData == false)
   {
      rc = ss.read();

      if (rc != endMarker)
      {
         receivedChars[ndx] = rc;
         ndx++;
         if (ndx >= numChars)
         {
            ndx = numChars - 1;
         }
      }
      else
      {
         receivedChars[ndx] = '\0'; // terminate the string
         ndx = 0;
         newData = true;
      }
   }
}

void showNewData()
{
   if (newData == true)
   {
      Serial.print("This just in ... ");
      Serial.println(receivedChars);
      //newData = false;
   }
}

void parseData()
{
   char *strings[5]; // an array of pointers to the pieces of the above array after strtok()
   char *ptr = NULL; byte index = 0;
   ptr = strtok(receivedChars, ",");  // delimiter comma
   while (ptr != NULL)
   {
      strings[index] = ptr;
      index++;
      ptr = strtok(NULL, ",");
   }
      
   // convert string data to numbers
   A = atoi(strings[0]);
   B = atoi(strings[1]);
   C = atoi(strings[2]);
   D = atoi(strings[3]);
   E = atoi(strings[4]); 
   
}
void displayData()
{


   Servo1.write(A);
   Servo2.write(B);
   Servo3.write(C);
   Servo4.write(D);
   Servo5.write(E);
   
  
   Serial.println(); // blank line  
}

What is the specific problem?

I recommend that you use the Start/End markers examples from serial input basics. With start and end markers it is harder to loose data and get out of synch.

Please post a schematic. Written descriptions are always more ambiguous than a drawing. Hand drawn, photographed and posted is fine. Include all pin names/numbers, components, their part numbers and/or values and power supplies.

Do not power the servos from the USB or from the Arduino board's 5V regulator. Those power sources will not supply sufficient current for even 1 servo.

One thing that can kill servos is stalling the motors in the servo. That can happen if the servo arm is not allowed to move by a blockage or by attempting to move the servo past the servo's internal end stops. One can limit the travel of the servo by adding min and max parameters to the attach() function.

servo.attach(pin, min, max);

So here is the scematic

I hope that is sufficent:

Ah few infos about the components:

Board:
Microcontroller: Atmel
Architecture: AVR
Operating voltage: 5 V.
Input voltage: 7-12 V.
SRAM: 2KB
Clock frequency: 16 MHz.
Analogue E / A pins: 8
EEPROM: 1 KB.
DC Per E / A Pins: 40mA (E / A Pins)
Digital E/A pins: 22
PWM output: 6
Power consumption: 19mA

Power Supply 20A
Bauer Electronics
|Wattage |‎100 watts|
|Amperage Capacity |‎20 Amps|
|Maximum Power |‎100 Watts |
Input: 8-36V Output: 5V | 100W | 20A

Joysticks:
RETTI residual value: standard 10KΩ.
Residual tolerance: ± 20%.
Independent linearity: ± 1%.
Resolution: Essentially infinite.
Smoothness of the spout: maximum 0.5%

Servos:
Impulsbereich: 500 ~ 2500usec(0 ~ 270 °)
Tot Bandbreite: 3 usec
Neutrale Position: 1500usec
Stallstrom: 2100mA±10% (4,8 V) -2700mA±10% (6,8V)
Stall Torque: 20,5kg·cm (284,7oz·in) -22,8kg·cm (316,63oz·in)

Boost Converter
DSN6009 Booster Module
Type of module: unembossed step-up (boost).
Rectification method: synchronous rectifier
Input range: 3V to 32V
Output range: 5V to 35V
Input current 4A (maximum), idle 18mA (5V input, 8V output, idle less than 18mA. The higher the voltage, the greater the idle current).
Conversion efficiency: <94% (the greater the pressure difference, the lower the efficiency)
Switching frequency: 400 KHZ
Output ripple: 50mV (the higher the voltage, the current, the ripple is greater)
Load control: plus or minus 0.5%.
Voltage control: plus or minus 0.5%.

Step down Module
Input voltage: DC 3.2 V to 40 V, input voltage must be higher (1.5 V) than the output voltage.
Output voltage: DC 1.25 V to 35 V, voltage is freely adjustable, with high efficiency (max. 92%) and max. output current is 3 A.


Update

I think the servo problem should be solved but not 100% sure, only time will tell
My only problem is this line (i think
snprintf(buffer, 14, "%d,%d", C, D, A, B, E);

So i am sending to values for the servo but not all 5
if i change it to A, B the other joystick is working

What do i need to change and which parts are useless in my code

Thanks a lot

Sender:

#include <SoftwareSerial.h>

const byte Ax = A0;
const byte Bx = A1;
const byte Cx = A2;
const byte Dx = A3;
const byte Ex = A4;

SoftwareSerial ss(2, 3);

void setup()
{
   Serial.begin(9600);
   ss.begin(9600);
}

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 250;
   if (millis() - timer >= interval)
   {
      timer = millis();
      // read joysticks
      int A = analogRead(Ax);
      A = map(A , 0 , 1023 , 900  , 2000) ;

      int B = analogRead(Bx);
      B = map(B , 0 , 1023 , 900  , 2000) ;

      int C = analogRead(Cx);
      C = map(C , 0 , 1023 , 900  , 2000) ;

      int D = analogRead(Dx);
      D = map(D , 0 , 1023 , 900  , 2000) ;

      int E = analogRead(Ex);
      E = map(E , 0 , 1023 , 900  , 2000) ;

      // print values to serial monitor
     
      Serial.print(A);
      Serial.print(B);
      Serial.print(C);
      Serial.print(D);
      Serial.print(E);


      // create comma delmited packet
      char buffer[15];
      snprintf(buffer, 14, "%d,%d", C, D, A, B, E);
      Serial.println(buffer);
      // send packet
      ss.println(buffer);
   }
}

Reciver

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

int A = 0;
int B = 0;
int C = 0;
int D = 0;
int E = 0;


const byte numChars = 32;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

SoftwareSerial ss(2, 3);

ServoTimer2 Servo1;
ServoTimer2 Servo2;
ServoTimer2 Servo3;
ServoTimer2 Servo4;
ServoTimer2 Servo5;

void setup()
{
   Serial.begin(9600);
   ss.begin(9600);

 Servo1.attach(5);
 Servo2.attach(6);
 Servo3.attach(9);
 Servo4.attach(10);
 Servo5.attach(11);
}

void loop()
{
 recvWithEndMarker();
   //showNewData();
   if (newData)
   {
      parseData();
      displayData();
      newData = false;
   }
}

void recvWithEndMarker()
{
   static byte ndx = 0;
   char endMarker = '\n';
   char rc;

   while (ss.available() > 0 && newData == false)
   {
      rc = ss.read();

      if (rc != endMarker)
      {
         receivedChars[ndx] = rc;
         ndx++;
         if (ndx >= numChars)
         {
            ndx = numChars - 1;
         }
      }
      else
      {
         receivedChars[ndx] = '\0'; // terminate the string
         ndx = 0;
         newData = true;
      }
   }
}

void showNewData()
{
   if (newData == true)
   {
      Serial.print("This just in ... ");
      Serial.println(receivedChars);
      //newData = false;
   }
}

void parseData()
{
   char *strings[4]; // an array of pointers to the pieces of the above array after strtok()
   char *ptr = NULL; byte index = 0;
   ptr = strtok(receivedChars, ",");  // delimiter comma
   while (ptr != NULL)
   {
      strings[index] = ptr;
      index++;
      ptr = strtok(NULL, ",");
   }
      
   // convert string data to numbers
   A = atoi(strings[0]);
   B = atoi(strings[1]);
   C = atoi(strings[2]);
   D = atoi(strings[3]);
   E = atoi(strings[4]); 
   
}
void displayData()
{


   Servo1.write(A);
   Servo2.write(B);
   Servo3.write(C);
   Servo4.write(D);
   Servo5.write(E);
   
  
   Serial.println(); // blank line  
}