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
}