Serial bite to servo convertion????

Hello!! everyone i almost new in arduino i try to make a 6dof the code i have work whit a program that
send ascii code in the seria but the one i whant to work sen binary code

here a example of the code i recive in the serial the servo coresponding to each byte = serv1 serv2 serv3 serv4 serv5 serv6
binary code recive in the serial = “AB” byte1 byte2 byte3 byte4 byte5 byte6 byte7 0x0D
scale= 0 to 250 in all

Here the code for the ascii:

void loop() // the main loop where it all happens
{ 
  if (Serial.available() > 0) { // Checks if anything in the serial buffer
    j1 = Serial.read(); // Read a serial byte
    if (j1 == 'AB'){ // Found the reading for "Pitch"
      delay (5); // It seems to need a delay here
      pitch = (Serial.read()); // first digit of the data
      //pitch += char(Serial.read()); // Second digit and so-on
      //pitch += char(Serial.read());
      //pitch += char(Serial.read());
      //pitch += char(Serial.read()); 
      //pitch += char(Serial.read()); 
      if (pitch != pitchold){ // checks to see if its different to the "old" reading 
        char carray[7]; //converting a string to number sequence
        pitch.toCharArray(carray, sizeof(carray)); //converting string to number sequence
        int n = atoi(carray); //converting string to number sequence
        n = map(n, -250, 250, 0, 179); //Maps the readings to the servo
        myservopitch.write(n); //Sends the data to servo
        pitchold = pitch;          // Writes the current reading to the "old" string.
      }      // end of it's a different reading section 
    }            // end of "found A" section

I think the error here is the convertion i dont know :~

I appreciated any help!!!

Thanks!!!

'AB' won't fit in a single byte.
Now, I know Serial.read returns an int, but it can only ever return a single character, or byte.
Also, you're falling into the common mistake of figuring there's at least one character available, then trying to read as many as you think you want.

Here is the software Data Output Formats send to arduino , the program give me the option to change to 3 tipe of format BIN, HEX2 ,and HEX3 i don't know which one of this is more easy to get working the servo.

BIN output format is - “AB” byte1 byte2 byte3 byte4 byte5 byte6 byte7 0x0D (CR)

"AB" - start of data identifier for the receiving micro controller

byte1 - reserved

byte2 - 8 bit binary number giving act1 demand in 0-255 scale

byte3 - 8 bit binary number giving act2 demand in 0-255 scale

byte4 - 8 bit binary number giving act3 demand in 0-255 scale

byte5 - 8 bit binary number giving act4 demand in 0-255 scale

byte6 - 8 bit binary number giving act5 demand in 0-255 scale

byte7 - 8 bit binary number giving act6 demand in 0-255 scale

0x0D - single byte Carriage Return data terminator

HEX2 output format is - “JK” AA BB CC DD EE FF GG HH II 0x0D (CR)

"JK" - Start of data identifier for the receiving micro controller

AA - 1 byte - 8 bits, each indicates on/off for status indicators - see below

BB - hex value of act1 demand in 0-255 (0x00 to 0xFF) scale

CC - hex value of act2 demand in 0-255 (0x00 to 0xFF) scale

DD - hex value of act3 demand in 0-255 (0x00 to 0xFF) scale

EE - hex value of act4 demand in 0-255 (0x00 to 0xFF) scale

FF - hex value of act5 demand in 0-255 (0x00 to 0xFF) scale

GG - hex value of act6 demand in 0-255 (0x00 to 0xFF) scale

HH - hex value for current forward ground speed in m/s, capped at 255

II - hex value for engine 1 rpm, 0 - 100 percent

0x0D - single byte Carriage Return data terminator

HEX3 output format is - “JK” AAA BBB CCC DDD EEE FFF GGG HHH III 0x0D (CR)

"JK" - Start of data identifier for the receiving micro controller

AAA - Least significant 8 bits active, each indicates on/off for status indicators - see AA above

BBB - hex value of act1 demand in 0-1020 (0x000 to 0x3FC) scale

CCC - hex value of act2 demand in 0-1020 (0x000 to 0x3FC) scale

DDD - hex value of act3 demand in 0-1020 (0x000 to 0x3FC) scale

EEE - hex value of act4 demand in 0-1020 (0x000 to 0x3FC) scale

FFF - hex value of act5 demand in 0-1020 (0x000 to 0x3FC) scale

GGG - hex value of act6 demand in 0-1020 (0x000 to 0x3FC) scale

HHH - hex value for current forward ground speed in m/s, capped at 255 (0x0FF)

III - hex value for engine 1 rpm, 0 - 100 percent

0x0D - single byte Carriage Return data terminator

Note that 'AB' will fit into an int (though I wouldn't recommend it), but "AB" will not.

ok i write this code an work, whit the act6 only but im XD i have a really little glitch but work whit this later. OK ,how i read a specific bite from the serial for the servo 1,2,3 ect??.

#include <Servo.h>
Servo myservo;
int j1;

void setup()
{
  myservo.attach(9);
  Serial.begin(115200);
}
void loop()
{
  if (Serial.read() > 0) {
   
    j1 = Serial.read(); 
    j1 +=(Serial.read());
    j1 +=(Serial.read());
   
   j1 = map(j1, 10, 250, 250, 0);
    myservo.write(j1);
    

    }
  }

No, checking to see if there's at least one character available, then reading all three of them has been shown many times to mostly not work.
See if you've got three then read them, or check before each read.

When you've got that right (it comes up often here in the forum), you'll find that simply summing ASCII codes will not give you the result you want.
Either use strings, or check the characters as you receive them.

OK if i read 3 or 4 digits the servo 6 work, if i read 11 digits the servo 5 work whit a lot of glitch back forward but work when i move. I think if i continuum adding digits he continuous to map the last 3 but going to get more glitch.

Not let me change string to int, and i cant use char for read this tipe of data. Correct me if i wrong , i try and not work. i not read now ascii is BIN.

I'm sorry, I don't understand you - your code doesn't have a servo 5 or 6.

Converting ASCII representations of decimal numbers comes up here on the forum at least once a week, and I'm pretty sure there is example code available.

OK, sorry i no explain really well, ok in the simulation software i have a window to test all the 6 servo , so wen i move servo 6 for example i change the value in the byte 7 ect ect
In the arduino code i only have one servo because i dont know how get a specific byte in this line for each servo ,all variation come in one line like you see start whit "AB"
ok i try to read one of this byte to move the servo in my code and make a test whit the (software test window) to see what byte i get . My problem now is how to get a specific byte.
Later i add the 6 servo to the code responding to the corresponding byte.

the servo coresponding to each byte = serv1 serv2 serv3 serv4 serv5 serv6
binary code recive in the serial = “AB” byte1 byte2 byte3 byte4 byte5 byte6 byte7 0x0D

serv1 = act1 in the table

ok servo1 need to be move according to the position in the byte2 servo2 in byte3 ect. im try to move the servo in the code conected to 9 whit one of this byte

I really don't understand what you mean by stuff like "byte 7 ect ect"
Is "ect" some other function code?
What is "the simulation software"?

I have program that send BIN format to the seria for moving motors, i whant to read this format and move 6 servos , but i dont know how to get the specific byte to move each servo.

Here is a format example i supose to get in the serial. In this example act1 i think is (actuator) is the servo to move in a scale 0-250.

BIN output format is - “AB” byte1 byte2 byte3 byte4 byte5 byte6 byte7 0x0D (CR)

"AB" - start of data identifier for the receiving micro controller

byte1 - reserved

byte2 - 8 bit binary number giving act1 demand in 0-255 scale

byte3 - 8 bit binary number giving act2 demand in 0-255 scale

byte4 - 8 bit binary number giving act3 demand in 0-255 scale

byte5 - 8 bit binary number giving act4 demand in 0-255 scale

byte6 - 8 bit binary number giving act5 demand in 0-255 scale

byte7 - 8 bit binary number giving act6 demand in 0-255 scale

0x0D - single byte Carriage Return data terminator

How are you moving your motors now? If using pwm, directly set the analogWrite function with your byte value. Or map the byte to a time scale ( 0 = none, 255 = 1 second maybe ) and move the motors for each specified time.

pwm. OK the software have a windows to test each servo 1 to 6 , im no know to much, i am new in this, but i make this code and i only read the act6 , byte7, And the servo move perfect,, i know is the byte7 because i test in the software test window. This is the code i write, i think im wrong whit this code , but i not understand byteRead i try but not give any result.

#include <Servo.h>
Servo myservo;
int j1;

void setup()
{
  myservo.attach(9);
  Serial.begin(115200);
}
void loop()
{
  if (Serial.available() > 0) {
    //delay (10);
    j1 = Serial.read(); 
    j1 +=(Serial.read());
    j1 +=(Serial.read());
   
   j1 = map(j1, 0, 250, 250, 0); 
    myservo.write(j1);
    

    }
  }

Can you help me whit a little code to see the stucture and funtion i need , if you have the time.

you read three bytes, make sure there is that much data available.

if (Serial.available() >= 3) {

why are you adding the values together, maybe you want them in an array?

While you're thinking about the serial processing, remember that errors will occur occasionally leaving you with extra, garbled or missing bytes. This means you would need a scheme to detect when the sender and receiver are put of sync and get them back in sync. Since AB and CR are both legitimate servo position values, you will need to be careful how you do that.

Tanks!! pYro i really appreciated your help is working, my error was believe that i receive the format like character and now i understand that 1 byte have the complete value 0-250.
And sorry for my english is not realy good. I really appreciate your help, and you time. Thank you very much. If you need any part for a prototype, I am a cad designer and cnc programer and have a cnc in home so let me know if you need something . No i going to see how to assign each byte to the corresponding pinout jehehe, 'm going crazy, I need to learn a lot about this, i really like this.

Thanks!!

I thought these two threads looked very similar, so I merged them.

i think few days ago i understand a byte jehehe and i don't and i tink i not going to do. I make this code and only the servo1 and servo2 work, if i change >= to == i not read any byte . I not understand whats wrong.

#include <Servo.h>
Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;
Servo myservo6;

int j1;

int servo1;
int servo2;
int servo3;
int servo4;
int servo5;
int servo6;

void setup()
{
  myservo1.attach(2);
  myservo2.attach(3);
  myservo3.attach(4);
  myservo4.attach(5);
  myservo5.attach(6);
  myservo6.attach(7);
  
  Serial.begin(115200);
}
void loop()
{
  if (Serial.available() >= 0) {
    j1 = Serial.read();
    
    if (j1 >= 2){ 
      servo1 = (Serial.read());
   
      servo1 = map(servo1, 0, 250, 250, 0); 
      myservo1.write(servo1);
    }
  }
  
  
    if (j1 >= 3){ 
      servo2 = (Serial.read());
   
      servo2 = map(servo2, 0, 250, 250, 0); 
      myservo2.write(servo2);
    
    }
  
  

    if (j1 >= 4){ 
      servo3 = (Serial.read());
   
      servo3 = map(servo3, 0, 250, 250, 0); 
      myservo3.write(servo3);  

    }
  
  
  
    if (j1 >=5){
      servo4 = (Serial.read());
   
      servo4 = map(servo4, 0, 250, 250, 0); 
      myservo4.write(servo4);  

    }
  
     
     
    if (j1 >= 6){
      servo5 = (Serial.read());
   
      servo5 = map(servo5, 0, 250, 250, 0); 
      myservo5.write(servo5);  

    }
  
   
    if (j1 >= 7){
      servo6 = (Serial.read());
    
      servo6 = map(servo6, 0, 250, 250, 0); 
      myservo6.write(servo6); 
    
   }

  }

You need to check that there is sufficient data available to read, BEFORE you read it.

I make a test whit this code and change if (Serial.available() >= 2) to if (Serial.available() >= 7) and read each 6 byte perfect the servo answered to the value of each byte. But again if i change >= to == not work. When i try move all whit the 6 servo code only the first 2 work.

#include <Servo.h>
Servo myservo1;
int j1;

void setup()
{
  myservo1.attach(2);
  Serial.begin(115200);
}
void loop()
{
  if (Serial.available() >= 6) {          // i change this from 2 to 7 and read each byte
    j1 = Serial.read();
    j1 = map(j1, 0, 250, 250, 0); 
    myservo1.write(j1);
    

    }
  }