Saving 4 different integers from serial communication

Hi guys! I am buiding a submarine where are two servos, stepper motor and dc motor, I used to have working prototype using NRF24L01 where i was sending joystick values from transmitter using radio.write(&joy1, sizeof(joy1));. Becouse of NRF24L01 transmits at 2.4 ghz I had to change to HC-12 whitch transmits at 433mhz. The difference is that i need to create algorithm which will recognize 4 different values (from two joysticks) from serial communication and save them to 4 different integers. For example A144B183C180D231E if serial.read incomming data begins with A read values and save them : val1= 144, val2 = 183, val3 = 180, val4 = 231. I am quite new to programming so any help is appriciated. Thanks

I do not have your code but you can do val1 = (whatever you read) and do it for each variable. You need to post more information so we can better understand what you are doing.

305.27 MB folder on MEGA it was my school graduation project, there are all the files you need to uderstand the project, however transmitter code is corrupted.

I found way more than needed. Also post a schematic, your frizzy things in the schema folder do not cut it. Did you turn in a fully functioning project?

what i need help with is to create algorithm that recognizes and writes 4 values from 1 message, for example i will send 4 joystick values devided by four and write them to HC-12 serial as A000B255C211D100E (random numbers) and on the other arduino read them with algorithm that goes something like this : if message begins with character A begin to read number if it continues with B save value, if it continues with C save value , if it continues with D save value, if it continues with E end of the message, and then somehow write these numbers to integers or strings (whatever is best) value1 = 000, value2 = 255, value3 = 211, value4= 100

I had it to work once but it was with NRF24L01 modules, but these are not good for penetrating water so I swithed to HC-12 but becouse of this i need to change the code as explained

Take a look at Robin2's tutorial on Serial Input Basics.

It will give you all you need about how to read and parse serial messages.

1 Like

Have you followed cattledog's link? Personally I do not have the time nor desire to write the code for you, you need to do that. When you do that and you have problems post it properly, do not send us on links to elsewhere. When you post the schematic also include links to technical information on all the hardware parts. There are many devices that have the same name and description that are not the same.

I have tryied quick demo and edited it to my desire, seems to work

Great, be sure to give cattledog a click on the heart button to say thanks. Thanks for letting us know.

  1. Assume that the symbols/charcaters of your message frame are being transmitted/received in ASCII codes; where "A is the START Mark" and "E is the END Mark". You may execute the folloeing sketch.

  2. The Sketch

char myData[50];
char *temPtr;

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

void loop()
{
    byte n = Serial.available ();
    if(n ! = 0)
    {
        if(flag == false)
        {
           char x = Serial.read();
           if(x == 'A')
           {
               flag = true;
           }
        }
        else
        {
           byte m = Serial.readBytesUntil('E', myData, 50);
           myData[m] = '\0';  //null-byte
           Serial.println(myData);
           //----------------------
           unsigned int x1 = strtoul(myData, &temPtr, 10);
           Serial.println(x1, DEC);  //shows: 144
           //--------------------
           unsigned int x2 = strtoul(temPtr+1, &temPtr, 10);
           Serial.println(x2, DEC);  //shows: 183
           //-------------------
           unsigned int x3 = strtoul(temPtr+1, &temPtr, 10);
           Serial.println(x3, DEC);  //shows: 180
           //-------------------
           unsigned int x4 = strtoul(temPtr+1, &temPtr, 10);
           Serial.println(x4, DEC);  //shows: 231
           //-------------------
        }
    }
}
  1. Upload shetch of Step-2.

  2. Open Serial Monitor with "No line ending" option.

  3. In the InputBox of Serial Monitor, enter the following string and then click on the Send button.

  4. Check that the following message has appeared in the OutputBox of Serial Monitor.

144B183C180D231
144
183
180
231