Hallo
Ich benötige bitte euer Schwarmwissen
Ich steuere 5 Servomotoren mit 2 Joysticks mit x und y achse und mit x, y und z achse ).
Mein Problem ich schaffe es nicht das Paket richtig aufzulösen das alle 5 Servos angesteuert werden. Es werden nur Servo A und B angesteuert.
Der Code:
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", A, B, C, D, 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
}
Schaltplan:
Danke euch