`Hello all,
I have a Mega 2560 with a
BMP280 pressure/temp sensor (I2C)
GPS on hardware serial (TX/RX1)
Both of which run great, on their own. but I am trying to have them both display on the 20 x 4 LCD.
The BMP280 is only being read every 15000us as instructed but no sign of the GPS whilst the BMP results are being displayed.
I have read and re-read 'Blink without delay' and also the 'several things at the same time' examples. They have helped, as somehow I made the thing work as intended yesterday then promptly made a mess of it trying to 'tidy up' my code. Im sure its a simple error but Ive got to the point that I feel somewhat blinded by it all.
Could anyone offer a clue as to what I might have wrong here please?
But go easy on me, I am a struggling noob
Many thanks in advance.
MoJoZ
/*My 'basic' project to have a BMP280, GPS and DHT11 sensor to
provide---->>
GPS disciplined LOCAL time (UTC +10hrs here)
Temperature from the BMP280
Humidity from a DHT11 -- (Yet to be incorporated)
Barometric pressure from the BMP280
Outputs displayed on a 20 x 4 LCD Display
MoJoZ
* */
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h> // Library for the BMP280 pressure/temp sensor.
#include <SoftwareSerial.h> // include software serial library
#include <LiquidCrystal.h> // include LCD library
#include <TimeLib.h>
#include <TinyGPSPlus.h> // http://arduiniana.org/libraries/TinyGPS/
float temperature;
float pressure;
#define ALTITUDE 0 // Altitude of the BMP280 sensor at the home QTH
Adafruit_BMP280 bmp; // I2C
Adafruit_Sensor *bmp_temp = bmp.getTemperatureSensor();
Adafruit_Sensor *bmp_pressure = bmp.getPressureSensor();
LiquidCrystal lcd(42, 44, 46, 48, 50, 52); // My 20x4 LCD Pin config on a MEGA
unsigned long startMillis; // Start the counter rolling
unsigned long currentMillis = 0; // What the counter is up to right now.
unsigned long previousgpsMillis = 0; // when the GPS was last read.
unsigned long previousbmpMillis = 0;
const int bmpPeriod = 15000; // Check the BMP280 no more than every 15 seconds.
const int gpsPeriod = 1000; // I aim to check the GPS every second.
TinyGPSPlus gps;
#define SerialGPS Serial1
#define time_offset 36000 // clock offset of 36000 seconds (10 hours Brisbane) ==> UTC + 10
// variables
char Time[] = "TIME: 00:00:00";
char Date[] = "DATE: 00-00-2000";
byte last_second, Second, Minute, Hour, Day, Month;
int Year;
void setup(void)
{
SerialGPS.begin(9600);
startMillis = millis(); //initial start time
//time_t previousgpsMillis = 0;
lcd.begin(20, 4);
//lcd.print("Reading BMP280");
bool status;
status = bmp.begin(0x76); //My BMP280's are all addressed as 0x76
if (!status) {
lcd.clear();
lcd.setCursor(4, 1);
lcd.print("OOps. Check");
lcd.setCursor(3, 2);
lcd.print("the circuit!");
while (1);
}
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */
Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */
Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */
Adafruit_BMP280::FILTER_X16, /* Filtering. */
Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
bmp_temp->printSensorDetails();
}
//time_t previousgpsMillis = 0; // when the digital clock was displayed
void loop() {
currentMillis = millis();
if (currentMillis - startMillis >= bmpPeriod)
{
sensors_event_t temp_event, pressure_event;
bmp_temp->getEvent(&temp_event);
bmp_pressure->getEvent(&pressure_event);
startMillis = currentMillis;
getPressure();
// getHumidity();
getTemperature();
//lcd.clear();
}
//Printing Temperature
String temperatureString = String(temperature, 1);
lcd.setCursor(0, 3);
lcd.print("Temp: ");
lcd.print(temperatureString);
lcd.print((char)223);
//Printing Pressure
lcd.setCursor(0, 2);
lcd.print("Baro: ");
String pressureString = String(pressure, 2);
lcd.print(pressureString);
lcd.print(" hPa");
}
float getTemperature()
{
temperature = bmp.readTemperature();
}
float getPressure()
{
pressure = bmp.readPressure();
pressure = bmp.seaLevelForAltitude(ALTITUDE, pressure);
pressure = pressure / 100.0F;
previousbmpMillis += bmpPeriod;
}
void loop2()
{
//time_t previousgpsMillis = 0;
currentMillis = millis();
if (millis() - previousgpsMillis >= gpsPeriod) {
while (SerialGPS.available())
if (gps.encode(SerialGPS.read()))
{
if (gps.time.isValid())
{
Minute = gps.time.minute();
Second = gps.time.second();
Hour = gps.time.hour();
}
// get date drom GPS module
if (gps.date.isValid())
{
Day = gps.date.day();
Month = gps.date.month();
Year = gps.date.year();
}
if (last_second != gps.time.second()) // if time has changed
{
last_second = gps.time.second();
// set current UTC time
setTime(Hour, Minute, Second, Day, Month, Year);
// add the offset to get local time
adjustTime(time_offset);
}
// update time array
Time[12] = second() / 10 + '0';
Time[13] = second() % 10 + '0';
Time[9] = minute() / 10 + '0';
Time[10] = minute() % 10 + '0';
Time[6] = hour() / 10 + '0';
Time[7] = hour() % 10 + '0';
// update date array
Date[14] = (year() / 10) % 10 + '0';
Date[15] = year() % 10 + '0';
Date[9] = month() / 10 + '0';
Date[10] = month() % 10 + '0';
Date[6] = day() / 10 + '0';
Date[7] = day() % 10 + '0';
{
// print time & date
/* currentMillis = millis();
if (currentMillis - previousgpsMillis >= gpsPeriod);*/
lcd.setCursor(0, 0); // move cursor to column 0 row 2
lcd.print(Time); // print time (HH:MM:SS)
previousgpsMillis += gpsPeriod;
}
}
}
}