BME280 sensor data with time

So, my partner has an Arduino R3, while I am rockin’ a Mega 2560. His program has the expected outcome, but when I run the same program, the outcome is a bit wonky. The intent is to calibrate first, then read the data from the BME280 sensor, after an input of 1. If 0 is input, then the program should stop.
The problem being that when I input 1 or 0, in order to start the sensor through getreading(), nothing happens. It seems that nothing is being read when I type in either 1 or 0. I have been searching and searching for how to fix this. From what I’ve read, serialEvent() should work on MEGA, and so should the Serial.parseInt(), so I am really at a loss. Here is my code. I hope I formatted this correctly, as this is my first ever post on Arduino forum.

I can provide a schematic if necessary, but I don’t feel the need right now to waste effort on that, since the if statement in the setup would not allow the program to continue, were there to be a wiring issue.

If I have a simpler program to just read the data every few seconds, the program runs fine.

My specific question in this scenario is, why would the program not read my input?

I am relatively new to programming, and this is my first project with Arduino.
Thank you in advance for any advice you all are able to give me!


#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

#define SEALEVELPRESSURE_HPA (1013.25)
#define samplingrate (250)

Adafruit_BME280 bme;
unsigned long time;
int inpt = 3;

//global variables for baselines
float avg_temp;
float avg_press = 0;
float avg_humid;

//struct for calibration readings
typedef struct reading
  float pressure[20];
  float temp[20];
  float humidity[20];

void setup() {

  if (!bme.begin(0x76)) {
    Serial.println("Could not find a valid BME280 sensor, wiring fucked");
    while (1);
  Serial.println("READY...(0:Stop Reading, 1: Start Reading)");

void loop() {
  if(inpt == 0)
  else if(inpt == 1)

//Waits for something typed into Serial input, then stores it in inpt
void serialEvent() {
    inpt = Serial.parseInt();

void getreading() {
  time = millis();
  //Temperature (*C)
  //Abs Pressure (Pa)
  //Relative Pressure (Pa)
  //Humidity (%)

void calibrate()
  reading init;
  for(int i = 0;i<20;i++)
    init.temp[i] = bme.readTemperature();
    init.pressure[i] = bme.readPressure();
    init.humidity[i] = bme.readHumidity();

  float tempsum = init.temp[0];
  float pressuresum = init.pressure[0];
  float humiditysum = init.humidity[0];

  for(int i = 1;i<20;i++)
    tempsum += init.temp[i];
    pressuresum += init.pressure[0];
    humiditysum += init.humidity[0];

  avg_temp = tempsum/20;
  avg_press = pressuresum/20;
  avg_humid = humiditysum/20;
  Serial.print("Baseline Pressure: ");




I ran the Serial Event example, and it runs fine...

Where is the operation system ?
An exit() closes the program to return to the operating system. You should not use the exit() function with an Arduino.

Do you have the Adafruit BME280 module, which is specifically designed to be compatible with 5V and 3.3V I2C busses.
If not, then you might need to add a level shifter.
The Arduino Mega 2560 has onboard pullup resistors of 10k for SDA and SCL to 5V. The newer Bosch sensors really don’t like 5V at their SDA and SCL pins (even if it is via a 10k resistor).

We don’t use Serial.event(). It has no advantages. You can put that code in the loop() and it will do the same thing.

The Serial.parseInt() might read a number, then the next Serial.parseInt() might read a Carriage Return or LineFeed and return zero. You read a zero as a command. Can you change that ?

When you send a command to a sketch, then you have to deal with it just once. You need to clear the command.

I suggest to put the code from Serial.Event() in the loop(), remove the exit(), think about the code structure.
Remember that the loop() runs over and over again. Don’t fight that with a exit(), embrace it :wink: Check if something needs to be done.

void loop()
  check the serial input, should I read data ?

  check if it is time to blink a led.

  check a button.
  and so on

zzb2017: why would the program not read my input?

Does "my input" mean what you type in the serial monitor? I that case, check if the box next to the baud rate selector (in the serial monitor) shows "Newline" or "Both NL & CR".

BTW: you must press "Enter" to send (a) character(s)!