Paket Probleme Servomotoren+HC12

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

Wenn Du alle Warnungen in der IDE einschaltest, bekommst Du beim Sender diese Warnung:

warning: too many arguments for format [-Wformat-extra-args]
       snprintf(buffer, 14, "%d,%d", A, B, C, D, E);
                                                  ^

"%d" muß also fünfmal vorkommen.

      char buffer[30];
      snprintf(buffer, sizeof(buffer), "%d,%d,%d,%d,%d", A, B, C, D, E);

ich würde mir zunächst einmal anzeigen lassen was du empfangen hast

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

mit folgender Änderung in SHowData

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

dann würde ich mir im serial monitor ausgeben lassen
was hier drinsteht

strings[index] = ptr;
serial.print("#");
serial.print(strings[index]);
serial.println("#");

und dann was in A,B,C, usw. drinsteht

A = atoi(strings[0]);
serial.print("A=#");
serial.print(A);
serial.println("#");

vgs

Im anderen Thread zu wenig Schwarm?

Ich verstehe ja, dass diese Funktion genutzt wird!
Aber warum dann nicht auch den Gegenpart davon?
Hier ist doch Symmetrie quasi kostenlos zu haben.

Gleiche Dinge gehören in ein Array!

Gleiche Dinge gehören in ein Array.

Wären die Pins in einem Array, täte es hier eine Schleife.


Pins , Values, Algorithmen und Servos sind doch jeweils verschiedene und doch zusammengehörige Dinge. So gehören sie eigentlich in eine Struktur, und diese in das schon genannte Array.

Leitsatz:

Ordnung und Symmetrie, ist Kunst für Doofe.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.