Solved classic 0/1 error in source code

master
marcel 2 months ago
parent 08b845f792
commit 6c1134ca3a
  1. 20
      CHANGELOG.md
  2. 43
      firmware/weather_station.ino

@ -57,3 +57,23 @@ All notable changes to this project will be documented in this file.
### Changed
- Humidity often got stuck at 100%. Heater algorithm now heats for 10 minutes and cools down for 10 minutes (was 5min/15min)
## [0.2.4] - 2024-03-08
### Changed
- Humidity timer back to 5/15 minutes: shorter cooling times give false temperature readings.
### Fixed
- Classic 0/1 error in max and average calculating routine, which resulted in a buffer overflow.
## [0.2.5] - 2024-03-11
### Fixed
- 0/1 error in max and average (again, or better said: still!).
### Changed
- Humidity threshold heater is now a define instead of hard coded numbers in the source.

@ -32,6 +32,8 @@
* - Changed some variables to the propper standard (uint8_t, uint16_t, etc.)
* - SparkFun wind interrupt now calculates over 3 seconds in stead of 1 second (KNMI standard)
*
* See CHANGELOG.md
*
**********************************************************************************/
#include <ModbusSerial.h>
#include "SparkFun_Weather_Meter_Kit_Arduino_Library.h"
@ -103,6 +105,7 @@ const int SensorStatusBitsIreg = 14;
* 0 = Heater algorithm (0 = disable, 1 = enable)
*/
const int HeaterCoil = 0;
#define HUMIDITY_THRESHOLD 92
// RS-485 serial port
#define MySerial Serial // define serial port used, Serial most of the time, or Serial1, Serial2 ... if available
@ -165,17 +168,17 @@ char HeaterSi7021 (float humidity)
switch (state)
{
// Default state: humidity is below 95%
// Default state: humidity is below HUMIDITY_THRESHOLD
case 0:
Heater = 0;
if (humidity >= 95) {
if (humidity >= HUMIDITY_THRESHOLD) {
StatemachineTimer = millis();
state = 1;
}
break;
// Humidity went above 95%. See if humidity stays above 95% for more than an hour, if so turn on heater
// Humidity went above HUMIDITY_THRESHOLD. See if humidity stays above HUMIDITY_THRESHOLD for more than an hour, if so turn on heater
case 1:
if (humidity >= 95) {
if (humidity >= HUMIDITY_THRESHOLD) {
if ( (millis() - StatemachineTimer) >= 3.6e+6 ) {
//if ( (millis() - StatemachineTimer) >= 300000 ) { // short delay for testing
Heater = 1;
@ -191,9 +194,9 @@ char HeaterSi7021 (float humidity)
}
break;
// Heater is now on, let the sensor cook for 10 minutes
// Heater is now on, let the sensor cook for 5 minutes
case 2:
if ( (millis() - StatemachineTimer) >= 600000 ) {
if ( (millis() - StatemachineTimer) >= 300000 ) {
StatemachineTimer = millis();
Heater = 0;
state = 3;
@ -202,17 +205,17 @@ char HeaterSi7021 (float humidity)
}
TempValid = 0;
break;
// Heater is now off, let the sensor cool for 10 minutes
// Heater is now off, let the sensor cool for 15 minutes
case 3:
if ( (millis() - StatemachineTimer) >= 600000 ) {
if ( (millis() - StatemachineTimer) >= 900000 ) {
TempValid = 1; // Sensor cooled, so we can take a valid temperature reading
if (humidity >= 95) {
// Humidity still above 95%, repeat heating/cooling
if (humidity >= HUMIDITY_THRESHOLD) {
// Humidity still above HUMIDITY_THRESHOLD, repeat heating/cooling
Heater = 1;
StatemachineTimer = millis();
state = 2;
} else {
// Humidity below 95%, reset statemachine
// Humidity below HUMIDITY_THRESHOLD, reset statemachine
Heater = 0;
state = 0;
}
@ -243,7 +246,7 @@ void ReadSi7021 (void)
if (humidity>100 || humidity<0)
humidity = 100;
//If humidity is larger than 95% switch on heater to get more acurate measurement and prevent memory offset
//If humidity is larger than HUMIDITY_THRESHOLD switch on heater to get more acurate measurement and prevent memory offset
result = HeaterSi7021(humidity);
MeasuredData.StatusBits &= 0xFFFC; // Reset heater status bits to zero
MeasuredData.StatusBits |= result; // And set the proper bits to one if there are any. The result is we copied the status bits to the register
@ -295,9 +298,12 @@ void ReadBMP280 (void)
int MaxOfArray (int array[], uint16_t length)
{
int maximum_value = 0;
while (length--)
while (length)
{
// decrement length, because 0/1 problem: if lenght = n, than last position off array is n-1
length--;
if (array[length] > maximum_value)
maximum_value = array[length];
}
@ -309,9 +315,12 @@ uint16_t AverageOfArray (uint16_t array[], uint16_t length)
uint32_t tmp_value = 0;
uint8_t tmp_length = length;
uint16_t average_value = 0;
while (length--)
while (length)
{
// decrement length, because 0/1 problem: if lenght = n, than last position off array is n-1
length--;
tmp_value += array[length];
}
average_value = tmp_value/tmp_length;
@ -451,7 +460,7 @@ void setup() {
mb.Ireg (SensorRainSinceMidnightIreg, 0);
mb.Ireg (SensorSnowFallIreg, 0);
Serial.println(F("Weather station v0.2.2"));
Serial.println(F("Weather station v0.2.5"));
Serial.println(F("(C)2024 M.T. Konstapel"));
Serial.println(F("This project is free and open source"));
Serial.println(F("More details: https://meezenest.nl/mees/"));

Loading…
Cancel
Save