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!
Zach
#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];
}reading;
void setup() {
Serial.begin(9600);
if (!bme.begin(0x76)) {
Serial.println("Could not find a valid BME280 sensor, wiring fucked");
while (1);
}
delay(500);
calibrate();
delay(100);
Serial.println("READY...(0:Stop Reading, 1: Start Reading)");
}
void loop() {
if(inpt == 0)
{
exit(0);
}
else if(inpt == 1)
{
getreading();
}
}
//Waits for something typed into Serial input, then stores it in inpt
void serialEvent() {
while(Serial.available())
{
inpt = Serial.parseInt();
}
}
void getreading() {
//Time
time = millis();
Serial.print(time);
Serial.print(';');
//Temperature (*C)
Serial.print(bme.readTemperature());
Serial.print(';');
//Abs Pressure (Pa)
Serial.print(bme.readPressure());
Serial.print(';');
//Relative Pressure (Pa)
Serial.print(avg_press-(bme.readPressure()));
Serial.print(';');
//Humidity (%)
Serial.print(bme.readHumidity());
Serial.print(';');
Serial.println();
delay(samplingrate);
}
void calibrate()
{
Serial.print("Calibrating");
reading init;
for(int i = 0;i<20;i++)
{
init.temp[i] = bme.readTemperature();
init.pressure[i] = bme.readPressure();
init.humidity[i] = bme.readHumidity();
Serial.print(".");
delay(samplingrate);
}
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];
Serial.print(".");
delay(100);
}
avg_temp = tempsum/20;
avg_press = pressuresum/20;
avg_humid = humiditysum/20;
Serial.println("Complete");
Serial.print("Baseline Pressure: ");
Serial.println(avg_press);
Serial.println("---------------------------------------------------------------------------------");
}
Here