From 5b5375dd9c8093bacd19846016799d0f66d384df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Gauthier?= Date: Fri, 16 Jan 2026 16:24:27 +0100 Subject: [PATCH] fix average type --- src/main.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 9b1d72d..ab23464 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,7 +15,7 @@ uint8_t ledPin = 2, alarm_pin = 19, decTemp = 4, nb_average_measure = 10; // nb const uint8_t nbPhotoSensor = 6, nbTempSensor = 1; uint8_t analogPin [nbPhotoSensor] = {A0, A1, A2 ,A3 ,A5 ,A6}, schedule = 0, photo_sensor_size = sizeof(uint8_t), temp_sensor_size = sizeof(uint8_t); int32_t min_res [nbPhotoSensor] = {128- 30, 160-30, 193-80, 96-10, 323-180, 96-10}; // Manual measurement of personal sensors -int32_t max_res [nbPhotoSensor] = {2062273-206227, 5554006-555400, 784809-78480, 4755895-475589, 1939035-193903, 289546-28954}; // Manual measurement of personal sensors // less 10 %, there are more résidual light in the measurement working (city context) than the box calibration +int32_t max_res [nbPhotoSensor] = {2062273, 5554006/2, 784809, 4755895, 1939035, 289546}; // Manual measurement of personal sensors uint32_t time_sec_sum; bool winter = true, timestamping = true, photo_sensor = true, temp_sensor = true, awake = true; static byte state = true; @@ -151,6 +151,7 @@ void loop() { uint32_t unixTime; int16_t mapped_val_array[nb_average_measure][nbPhotoSensor]; uint8_t converted_val_array[nb_average_measure][nbPhotoSensor], temp[nbTempSensor]; + uint16_t acc[nbPhotoSensor]; unixTime = myRTC.now().unixtime(); @@ -173,18 +174,23 @@ void loop() { delay(delay_bet_measure); } - for(int i = 1; i < nb_average_measure; i++){ + // init accumulator + for(int i = 0; i < nbPhotoSensor; i++){ + acc[i] = 0; + } + + for(int i = 0; i < nb_average_measure; i++){ for(int y = 0; y < nbPhotoSensor; y++){ - converted_val_array[0][y] += converted_val_array[i][y]; + acc[y] += (uint16_t) converted_val_array[i][y]; } } for(int y = 0; y < nbPhotoSensor; y++){ - converted_val_array[0][y] = converted_val_array[0][y] / nb_average_measure; + converted_val_array[0][y] = (uint8_t)(acc[y] / (uint16_t) nb_average_measure); } #ifdef DEBUG Serial.println("Readed values from sensors:"); - print_named_tab(converted_val_array[0], nbPhotoSensor, "int8_t normalised"); + // print_named_tab(converted_val_array[0], nbPhotoSensor, "int8_t normalised"); #endif // TODO : apply same change of max limit to avoid EEPROM overflow sto_intrf.add_measure(converted_val_array[0], temp, unixTime, nbPhotoSensor, nbTempSensor);