Problems with printing right value on nextion dispaly

I bought a sensor which analog voltage raises and falls quite a lot. So I decided to do an average. First I did it with one Arduino but I recognized that it might be too much to calculate and so I connected two via I2C bus. But also in this case it didn´t work. I hope you dont´t have problems with the german variables.

These are the Problems:

In the V1.0.5:

  1. I need around 100 values to get a good average. If I increase the amount of values over 30 there happen strange things: from 0-4.56 the values are positive they become negative but still rising (so -4.57 – -1.16) and then again positive.

In the V1.0.7.(1--> /2):

  1. Sometimes the display shows the errors (nan, ovf or 0.00).

  2. It needs a lot of time if there are no errors to get the values around 5 seconds.

  3. After some communication between the Arduinos the I2C but Fails and I have to push the restart button on the slave

V1.0.5.ino (2.68 KB)

V1.0.7.1.ino (2.59 KB)

V1.0.7.2.ino (1.29 KB)

 //Drucksensor
 pinMode (A0, INPUT);

This is setting the mode of the digital pin that shares space with analog pin 0. You should NOT be doing that.

     Druck [z] = analogRead(A0);
     Summe_Druck = Summe_Druck + Druck [z];

There is no reason to save all the values in an array.

If you add up 100 values, and the average value is over 320, you will overflow an int. That is what you are seeing happening in the 1.0.5 code.

 Data[0] = Durchschnitt_DruckPtr[0]; 
 Data[1] = Durchschnitt_DruckPtr[1]; 
 Data[2] = Durchschnitt_DruckPtr[2]; 
 Data[3] = Durchschnitt_DruckPtr[3];
 Data[4] = Durchschnitt_DruckPtr[4];

A for loop would save typing.
A for loop would save typing.
A for loop would save typing.
A for loop would save typing.
A for loop would save typing.

 Serial.print (Durchschnitt_Druck);

Does not belong in an ISR.

 byte* Data;
 Durchschnitt_DruckPtr = (byte*) &Durchschnitt_Druck;
 Data[0] = Durchschnitt_DruckPtr[0]; 
 Data[1] = Durchschnitt_DruckPtr[1]; 
 Data[2] = Durchschnitt_DruckPtr[2]; 
 Data[3] = Durchschnitt_DruckPtr[3];
 Data[4] = Durchschnitt_DruckPtr[4];  
 Wire.write(Data,5); // respond with message of 4 bytes

Data is a pointer, to who knows where. You should NOT be treating it as an array, because you do NOT own the space pointed to.