Serial Print Byte - Only changed values

Hello,

How would I go about changing this code around to only show the n0.val numerical change instead of printing each byte individually. Array maybe?

Currently my serial monitor looks like:

Attached below is the code.

(Also I'm using a Audiowell UM0034)
For reference on how this device uses its UART communication (https://www.unitronic.de/wp-content/uploads/productimports/23509_DB_UM0034.pdf)

const unsigned int MAX_MESSAGE_LENGTH = 12;
static char message[MAX_MESSAGE_LENGTH];
static int message_pos = 0;

void setup() {
 Serial.begin(9600);
    //Create a place to hold the incoming message

}

void loop() {

 //Check to see if anything is available in the serial receive buffer
 while (Serial.available() > 0)
 {

   char inByte = Serial.read();


   if ( inByte != '\n' && (message_pos < MAX_MESSAGE_LENGTH - 1) )
   {
     message[message_pos] = inByte;
     message_pos++;
   }
   else
   {
     message[message_pos] = '\0';
     
     Serial.println(message);
     message_pos = 0;
   }
 }
}

you could use readBytesUntil() to read a complete line terminated by a '\n'.

you could have a 2nd string and compare, strcmp(), the line just read to the 2nd string, print it if different and then strcpy() the received line to the 2nd string

Would you mind fitting your example into my code? I'm having a hard time with the 2nd string. I've been chasing my tail on this problem for a week now. This is the second sensor I've tried. The single transducer version has too big of a blind spot. This one will work great if I can get the value to print correctly :stuck_out_tongue:

From the message format, that should be more like:

if ( inByte != 0xFF && (message_pos < MAX_MESSAGE_LENGTH - 1) )


const int MsgLen = 50;
char msg  [MsgLen];
char msg1 [MsgLen] = "";

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

void loop() {
    if (Serial.available() > 0) {
        int n = Serial.readBytesUntil ('\n', msg, MsgLen);
        msg [n] = 0;        // terminate with null

        if (strcmp (msg1, msg)) {
            Serial.println (msg);
            strcpy (msg1, msg);
        }
    }
}

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