 static WiFiClient wifiClient;
 static PubSubClient mqttClient(wifiClient);
 
-static unsigned long counts;
-static unsigned long cpm;
-static unsigned int multiplier;
-static unsigned long previousMillis;
+// the total count value
+static volatile unsigned long counts = 0;
 
-//subprocedure for capturing events from Geiger Kit
-static void tube_impulse()
+static int secondcounts[60];
+static int secidx_prev = 0;
+static unsigned long int count_prev = 0;
+static unsigned long int second_prev = 0;
+
+// interrupt routine
+static void tube_impulse(void)
 {
     counts++;
 }
 
-void setup()
+void setup(void)
 {
-    counts = 0;
-    cpm = 0;
-    multiplier = MAX_PERIOD / LOG_PERIOD;       //calculating multiplier, depend on your log period
-    attachInterrupt(0, tube_impulse, FALLING);  //define external interrupts 
-
     // initialize serial port
     Serial.begin(115200);
     Serial.println("GEIGER\n");

#include <stdint.h>

#include <ESP8266WiFi.h>
#include <WiFiManager.h>


void loop()
{
    // update the circular buffer every second
    unsigned long int second = millis() / 1000;
    unsigned long int secidx = second % 60;
    if (secidx != secidx_prev) {
        // new second, store the counts from the last second
        unsigned long int count = counts;
        secondcounts[secidx_prev] = count - count_prev;
        count_prev = count;
        secidx_prev = secidx;
    }

    // report every LOG_PERIOD
    if ((second - second_prev) > LOG_PERIOD) {
        second_prev = second;

        // calculate sum
        int cpm = 0;
        for (int i = 0; i < 60; i++) {
            cpm += secondcounts[i];
        }

        // send over MQTT
        char message[16];
