Serial communication between mega and Uno

Hi,

i have problems getting numbers in the right order. I get the numbers, but the positioning isnt working the way i planned. Like i send 2730 but its show 3720 or 0237 and sometimes 2730 but it seems random. Anyone can help ?

Sender arduino mega

//Sender Code
unsigned long currentMillis;
unsigned long prevMillisServer;

char str[4];
char led1[4];
const byte output=A3;
void setup() {
  Serial1.begin(9600);
  pinMode(output,OUTPUT);
  currentMillis=millis();
prevMillisServer=millis();
}

void loop() {
  currentMillis=millis();
   if(currentMillis-prevMillisServer>=3000){
      
    
  float value=27.30; //this would be much more exciting if it was a sensor value
  int value2=value*100;
  int led1State=89;
  
  itoa(value2, str, 10); //Turn value into a character array
  Serial1.write(str, 4);
  //analogWrite(output,250);
  delay(1000);
  
 itoa(led1State, led1, 10); //Turn value into a character array
  Serial1.write(led1, 4);
  Serial1.end();
  Serial1.begin(9600);
  
  }
  //analogWrite(output,777);
  delay(1000);
}

Reciever arduino Uno

void loop(){

currentMillis=millis();
            
            if(currentMillis-previousMillisWebserver2>=3000){
         Serial.begin(9600);
            //Receiver Code


            char str[4];
            char led1[4];
            int i = 0;
            int j = 0;
            //int g = 0;
           // int h = 0;
            //int k = 0;
Serial.println(signal);

            if (Serial.available()) {
             
              delay(100); //allows all serial sent to be received together
              while (Serial.available() && i < 4) {
                str[i++] = Serial.read();

              }
              Serial.flush();
              delay(100);
              while (Serial.available() && j < 4) {
                led1[j++] = Serial.read();

              }
              Serial.flush();
              str[i++] = '\0';
              led1[j++] = '\0';
            }

            if (i > 0) {

              client.print("Temperatuur =  ");
              client.println(str[0]);
              client.println(str[1]);
              client.println(".");
              client.println(str[2]);
              client.println(str[3]);
              client.println(str[4]);


              client.println("
");

              

            }

if(j>0){
              client.print("Led State 1 =  ");
              client.println(led1[0]);
              client.println(led1[1]);
              client.println(led1[2]);
              client.println(led1[3]);
              client.println(led1[4]);
             

              client.println("
");

            }

previousMillisWebserver2=currentMillis;


Serial.flush();







            }

          }

It looks like your program does not know where the data begins and ends.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. The technique in the 3rd example will be the most reliable.

Notice that there is no need for any delay()s as part of gathering the data.

...R

Serial.flush() does nothing with received characters. It is a transmit function.

I suspect that you are not allowing for the terminating null in your allocation and use of C strings.

Have a look at the Serial.parseInt() and Serial.parseFloat() functions.
Also, there is no need to use itoa() or similar as Serial.print() will do that for you,
e.g Serial.print(myFloat); or Serial.print(myInt); directly convert floats or ints to ascci :slight_smile:

indev2:
Have a look at the Serial.parseInt() and Serial.parseFloat() functions.

They are blocking functions and I don't like them.

...R

Robin2:
They are blocking functions and I don't like them.

...R

Indeed :slight_smile: but when used properly with a delimiter/endmarker and faster baudrate (say 115200) they get the task done for a beginner in pretty short order.

void setup() {

  Serial.begin(115200);//Run it again at 9600 and note the time increase!
}

void loop() {

  while (Serial.available()) { //Expects something like 2345:25.46> as user input
    //Make sure Serial Monitor is set to No Line Ending and the correct baudrate!
    uint32_t startTime = micros();
    uint16_t a = Serial.parseInt();
    float b = Serial.parseFloat();
    if (Serial.read() == '>') {
      Serial.print("Parsed Int = ");
      Serial.println(a);
      Serial.print("Parsed Float = ");
      Serial.println(b);
      Serial.print("\nIn: ");
      Serial.print(micros() - startTime);
      Serial.println(" us");
    }
  }
}