From 3bdd4fa291a904989b59b21c88f2c322a1136551 Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Thu, 14 Nov 2024 18:36:04 +0100 Subject: [PATCH] implemented list instead of array for counts yeah, I am C developer, I didn't know C++ had such bullshit as list --- RadiationMapper.ino | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/RadiationMapper.ino b/RadiationMapper.ino index 60593a8..a0f7792 100644 --- a/RadiationMapper.ino +++ b/RadiationMapper.ino @@ -1,21 +1,19 @@ +#include + #define INPUT_PIN D6 //VIN PIN FOR GEIGER COUNTER #define MEASUREMENT_TIME_MS 60000 //MINUTE -#define MAX_CPM 5000 //HOW MANY COUNTS TO STORE -unsigned long counts[MAX_CPM]; -int last_index = -1; +std::list counts; //COUNTS IN LAST MINUTE (OR MEASUREMENT_TIME_MS IF MODIFIED) //CALLBACK WHEN RADIATION PARTICLE IS DETECTED void IRAM_ATTR increment_counts() { - counts[(++last_index) % MAX_CPM] = millis(); + counts.push_back(millis()); //APPEND CURRENT TIME TO LIST counts } void setup() { - //INIT counts - for (int i = 0; i < MAX_CPM; i++) counts[i] = 0; - + //SERIAL COMMUNICATION INIT Serial.begin(9600); //INIT PINS @@ -26,21 +24,17 @@ void setup() int last_counts = -1; void loop() { - unsigned long current_millis = millis(); + unsigned long current_millis = millis(); //CURRENT TIME int current_counts = 0; - //COUNT - for (int i = 0; i < MAX_CPM; i++) + //REMOVE COUNTS OLDER THAN MEASUREMENT_TIME_MS + counts.remove_if([current_millis](unsigned long particle) //haha, *lambda* (cries in pure-C) { - if (counts[i] == 0) continue; - if (current_millis - counts[i] >= MEASUREMENT_TIME_MS) //REMOVE COUNTS OLDER THAN MEASUREMENT_TIME_MS - { - counts[i] = 0; - continue; - } + return (current_millis - particle >= MEASUREMENT_TIME_MS); + }); - current_counts++; //COUNT RECENT - } + //COUNT RECENT + current_counts = counts.size(); if (last_counts != current_counts) //PRINT CPM IF CHANGED {