 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");


void loop()
{
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis > LOG_PERIOD) {
        previousMillis = currentMillis;
        cpm = counts * multiplier;

        Serial.println(cpm);
        counts = 0;

        // send over MQTT
        char message[16];

    Serial.print("ESP ID: ");
    Serial.println(esp_id);

    // connect to wifi
    Serial.println("Starting WIFI manager ...");
    wifiManager.autoConnect("ESP-GEIGER");

    // start counting
    memcpy(secondcounts, 0, sizeof(secondcounts));
    Serial.println("Starting count ...");
    attachInterrupt(0, tube_impulse, FALLING); 
}

static void mqtt_send(const char *topic, const char *value)
