Array problem

Im trying to connected two sensors to Arduino mega plus a keypad. First is a pressure sensor spirometer the second is a temperature sensor.

Im trying take the average of the sensors data and print one value in the Serial monitor when i remove my finger from a button. When I run my code for one sensor it works perfectly but when I put the two sensors together it gives me false reading. Please Help

#include <Keypad.h>
#include <Wire.h> 
#include "U8glib.h"
#include <SHT1x.h>


U8GLIB_ST7920_128X64_4X u8g(22, 24, 26);  // SPI Com: SCK = en = 18, MOSI = rw = 16, CS = di = 17



//---------------------------------keypad-----------------------------------------------------------------------------
#define Password_Lenght 7 // Give enough room for six chars + NULL char
char Data[Password_Lenght]; // 6 is the number of chars it can hold + the null char = 7
char Master[Password_Lenght] = "123456";
char Master2[Password_Lenght] = "000000";
char Master3[Password_Lenght] = "555555"; 
byte data_count = 0, master_count = 0;
bool Pass_is_good;
char customKey;

const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};

byte rowPins[ROWS] = {46, 44, 42, 40}; 
byte colPins[COLS] = {38, 36, 34}; 

Keypad customKeypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS); //initialize an instance of class NewKeypad 

//---------------------------------keypad-----------------------------------------------------------------------------

//---------------------------------Spirometer-----------------------------------------------------------------------------

const int analogInPin = A10; // Analog input pin, connected to pressure sensor 
const int analogButton = A11; // Button
//Variables to change
float inputVolt = 0; // Voltage read from pressure sensor (in bits, 0 to 1023) 
float volt_0 = 2.5; //Initial voltage
float volt = 0; // Voltage (converted from 0-255 to 0-5)
float pressure_psi = 0; // Pressure value calculated from voltage, in psi
float pressure_pa = 0; // Pressure converted to Pa
float massFlow = 0; // Mass flow rate calculated from pressure
float volFlow = 0; // Calculated from mass flow rate
float volume = 0; // Integral of flow rate over time
//Constants
float vs = 5 ; // Voltage powering pressure sensor 
float rho = 1.225; // Density of air in kg/m3
float area_1 = 0.000415; // Surface area in m2 
float area_2 = 0.0000283; // Surface area in m2 
float dt = 0.001;
int button = 0; // Value of button

//---------------------------------Spirometer-----------------------------------------------------------------------------

//---------------------------------Serial Reading-----------------------------------------------------------------------------
const int numReadings = 10;
int readings[numReadings];      // the readings from the analog input
 int readIndex = 0;              // the index of the current reading
 int total = 0;                  // the running total
 int average = 0;                // the average
//int inputPin =volume;
 int collectedValue = 0;

const int numReadings2 = 10;
 int readings2[numReadings2];      // the readings from the analog input
int readIndex2 = 0;              // the index of the current reading
 int total2 = 0;                  // the running total
 int average2 = 0;                // the average
 int collectedValue2 = 0;
//---------------------------------Serial Reading-----------------------------------------------------------------------------

//---------------------------------Temp Sensor-----------------------------------------------------------------------------
const int analogButton2 = A13;  
#define dataPin  30
#define clockPin 32
SHT1x sht1x(dataPin, clockPin); 
float temp_c;
float temp_f;
float humidity;  
int button2 = 0;
//---------------------------------Temp Sensor-----------------------------------------------------------------------------

void setup()
{
//---------------------------------Serial Reading-----------------------------------------------------------------------------
   Serial.begin(9800);
   for (int thisReading = 0; thisReading < numReadings; thisReading++) {
    readings[thisReading] = 0;
   }
   for (int thisReading2 = 0; thisReading2 < numReadings2; thisReading2++) {
    readings2[thisReading2] = 0;
   }
//---------------------------------Serial Reading-----------------------------------------------------------------------------


}

void loop()
{

  
 
//---------------------------------keypad-----------------------------------------------------------------------------

//---------------------------------Spirometer-----------------------------------------------------------------------------
button = analogRead(analogButton);
button2 = analogRead(analogButton2);
//Serial.println(volume);
  if(button>100 && button<1020)
  {
  inputVolt = analogRead(analogInPin); // Voltage read in (0 to 1023)
    if(inputVolt>600)
    {
        volt = inputVolt*(vs/1023.0);
        pressure_psi = (15/2)*(volt-2.492669); // Pressure in psi
        pressure_pa = pressure_psi*6894.75729; // Pressure in Pa
        massFlow = 1000*sqrt((abs(pressure_pa)*2*rho)/((1/(pow(area_2,2)))-(1/(pow(area_1,2))))); // Mass flow of air
        volFlow = massFlow/rho; // Volumetric flow of air
        
        volume = ((volFlow*dt)/0.04) + volume; // Total volume (essentially integrated over time) dt = 0.001;
        delay(1);
    
    }
  }
//---------------------------------Spirometer-----------------------------------------------------------------------------


//---------------------------------Serial Reading-----------------------------------------------------------------------------

   // subtract the last reading:
  total = total - readings[readIndex];
  // read from the sensor: 
  readings[readIndex] = volume;  
  // add the reading to the total:
  total = total + readings[readIndex];
  // advance to the next position in the array:
  readIndex = readIndex + 1;
  // if we're at the end of the array...  
  if (readIndex >= numReadings && button>100 && button<1020) {
    // ...wrap around to the beginning:
    average = total / numReadings;
    //delay(1000);
    collectedValue = 1;
    readIndex = 0;    
  }
  if(collectedValue == 1 && button<100)
  {
  Serial.println("PEFR: ");
  Serial.println(average);
    collectedValue = 0;
  }

  total2 = total2 - readings2[readIndex2];
  // read from the sensor: 
  readings2[readIndex2] = sht1x.readTemperatureC() + 5;  
  // add the reading to the total:
  total2 = total2 + readings2[readIndex2];
  // advance to the next position in the array:
  readIndex2 = readIndex2 + 1;
  // if we're at the end of the array...  
  if (readIndex2 >= numReadings2 && button2>100 && button2<1020) {
    // ...wrap around to the beginning:
    average2 = total2 / numReadings2;
    //delay(1000);
    collectedValue2 = 1;
    readIndex2 = 0;    
  }
  if(collectedValue2 == 1 && button2<100)
  {
  Serial.println("Body Temp: ");
  Serial.println(average2);
    collectedValue2 = 0;
  }
  

//---------------------------------Serial Reading-----------------------------------------------------------------------------

}

void clearData()
{
  while(data_count !=0)
  {   // This can be used for any array size, 
    Data[data_count--] = 0; //clear array for new data
  }
  return;
}

Ignoring the button press, are you able to print the average?
If not, you have problems with your code.

.

I am able to print the average, but the values are way off when and only when both sensors average arguments are in the code. When only one of the sensors average argument is in the code the values are correct. so I'm guessing it is an array problem?

castorino88:
I am able to print the average, but the values are way off when and only when both sensors average arguments are in the code. When only one of the sensors average argument is in the code the values are correct. so I'm guessing it is an array problem?

You need to fix this before going on.

Create a simple sketch with just the function for calculating the averages.
Make sure it works, then you can use this function in your original sketch.

I figured out this is the problem but i don't know why and how to fix it, When ever I run these two lines together they give me false reading, when ever I run one of them no false reading. Why???

readings[readIndex] = volume;  
 readings2[readIndex2] = sht1x.readTemperatureC() + 5;

Why???

Maybe if you numbered BOTH arrays and BOTH indexes, so you KNOW that you are not mistakenly using the wrong index with an array.

button = analogRead(analogButton);
button2 = analogRead(analogButton2);

So you can read that a switch is barely pressed, halfway pressed, really mashed?

Just what ARE you reading?

  // read from the sensor:
  readings[readIndex] = volume;

That is NOT what the code does. Your array to average the volumes has a really stupid name!

  readings2[readIndex2] = sht1x.readTemperatureC() + 5;

Another example of a useless name!