[How can I keep track of pressure readings in the past and compare to the latest reading?](http://How can I keep track of pressure readings in the past and compare to the latest reading?)
You do not need absolute time, just incremental history. Therefore a simple array of type float would work. Assuming 4 entries per hour and 6 hour span, you would need 24 elements, 0 - 23 of type integer. Keep a pointer to the last value written.
So, you will need to test [23] to see if it is ==0 and if not, your logic has been running for 6 hours or longer. Your forecast would be the the current pointer back by 23 steps. Supply the appropriate arithmetic for your weather algorithm.
There are other ways, too. If you need more than 6 hours, extend the array. If you need more than 4 datum in an hour, expand the array.
Thanks guys but I think we need to dumb it down a bit! This will be my first project after completing the sparkfun inventors kit tutorials.
I read up about arrays which sounds exactly like what I need but now my head is spinning! Especially the bit about keeping a pointer.
So, we have pressure:
and I have figured out how to print that to an LCD...YAY
lcd.setCursor(0, 1);
lcd.print("Pressure:");
lcd.print(pressure/100, 0); //whole number only.
lcd.print("hPa");
I researched the weather station I have, it lets you configure a storm warning. If it detects a pressure drop of 5-9 hPa (user selectable) over 3 hours it will trigger the warning. This is what I would like my sketch to do. I can't find any code similar to what I am looking for. Some example code would be really helpful.
I had a go at making some code but I think I have the wrong end of the stick regarding arrays. I doesn't give the desired result which is to record a pressure every 15 minutes then compare the current reading with the one taken 3 hours ago and calculate the change. I think I need to create a circular buffer?
#include <Wire.h>
#include <BMP085.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
const int numReadings = 13;
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int change = 0;
void setup()
{
bmp085Calibration();
lcd.begin(16, 2);
// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
}
void loop() {
const unsigned long fifteenMinutes = 15 * 60 * 1000UL;
static unsigned long lastSampleTime = 0 - fifteenMinutes; // initialize such that a reading is due the first time through loop()
unsigned long now = millis();
if (now - lastSampleTime >= fifteenMinutes)
{
lastSampleTime += fifteenMinutes;
float temperature = bmp085GetTemperature(bmp085ReadUT()); //MUST be called first
float pressure = bmp085GetPressure(bmp085ReadUP());
// read from the sensor:
readings[index] = pressure;
// calculate the pressure change
change = readings[index] - readings[index -12];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
lcd.setCursor(0, 0);
lcd.print(change);
Serial.println(change);
}
}
I suppose I need to wait until I have 13 readings in the array before calculating the change? Is that right?
If you want to compare the current reading to one three hours ago, yes. You said that you were looking for a change over the last hour, so, at 4 readings per hour, that requires only 5 readings (now, and the last 4).