In need of pointers, next to none experience with software.

Hi all,

First of all I'd like to state that I'm here for the learning curve and I'm not asking anyone to solve a problem for me. I'd like to figure stuff out myself, but in desperate need of pointers where to look and how to solve as I'm new to software of any kind.

Having that said:

I'm one of fifty plus guys who bought a electronics kit witch went south and the developers do not respond on mail and posts in the forum erected for this project. As I'm really keen on having a working project I've decided to take things in my own hands.
The project I'm talking about is http://github.com/pa2rdk/Onweerdetector and do not build it as is, the schematic is faulty and when you want to build it just PM me and I'll send you the modifications I've made to get it to work properly. ( One thing I do get is electronics 8) )

There are two major things that really concerns me and like to address immediatly:

#1 after pressing the reset button, the detector holds. After pulling down the interrupt manually to ground the software starts working.

-The detector should work whithout any interferance after pressing the reset button. I'd like to implement a puldown/pullup of the interrupt after initialisation of all the processes as I would do manually like I discribed.

#2 The software freezes, seemingly to loss of connection to the website and does not recover from this state.

-When a freeze occurs, preferably a nice goaround but happy with a implementation of a reboot witch brings us back to #1

My question:

Where do I look?

When you have questions, don’t make us run around the internet.

Post your code and sketch here.

Sorry about that, I just found the attachment option and attatched a .zip file.

RAZLightning.zip (36.1 KB)

#define CLK                   7 //8 (7 = final, 8 = proto)
#define DIN                  12 //12 
#define DC                   11 //11
#define CE                    9  //9
#define RST                  10 //10

One of the first things I look at, when looking at code, is whether the comments match the code, and whether the comments contribute anything.

The first line looks like it might mean something. The rest are just plain stupid. I hope that this isn't an indication of the way that the rest of the program is written.

// defines for hardware config

So, you need to give us a clue just exactly what an Onweer detector is, and why you need to detect them, and what you are using to detect them.

PaulS:
I hope that this isn't an indication of the way that the rest of the program is written.

Well, when the hardware design and support I got might tell something about the quality of the software. Just hold on to your hat, you're in for a ride..

Black box:

Lateral translated it's "lightning detector" and it is meant to do just that, it is meant to detect an event that is similar to a lightning bolt and detect when it's a real event or a man made disturber.

When an event occurs, the event is displayed on the (Nokia 5110) display with day, time and distance to event
when several events occur in sequence of a small time window, the number of can be selected via the serial monitor, the backlight of the display flashes and a beep alarm is sounded.
All events are displayed in a list and a couple of graphs witch show events per 24h and distance to location.

All events are send to a website (http://onweer.pi4raz.nl/) and displayed per station on that website.

There are four buttons (up, down, left and right) for the menu toggle (should have been a Rotary encoder imho) to switch from "day/time/date" to "list of events" to several graphs. Time and date can be altered manually, but is retreived from the website after a boot cycle and updated every event.

All data is send via WiFi (ESP8266 ESP-12), the detections are made by a lightning sensor (AS3935) and both are connected to a Arduino nano (ATmega328P 5V)

All options are in a setup menu in the serial monitor (should have been reachable via the outside buttons imho)

What it should have had was a pin high at no alarm and low at alarm so a seperate potential free alarm contact can be made and used for external use.

What it should have had was a pin high at no alarm and low at alarm so a seperate potential free alarm contact can be used for external use.

Seems simple enough to add, assuming you have a free digital pin.

The code currently does something when the "alarm" condition is true (it beeps, you said) and something else when the "alarm" condition is not true ("Shut the hell up, would you?"). Setting another pin high or low seems trivial.

#1 after pressing the reset button, the detector holds.

I'm not sure what this means. Are you talking about the reset switch on the Arduino? What detector "holds"? What does "holds" even mean?

Links are better than clues.

After pulling down the interrupt manually to ground the software starts working.

Again, I have no idea what this means. What interrupt are you pulling down manually? How do you even DO that?

I can assure you that the software was working before you did whatever you did. What it was doing may not have been what you wanted, but that doesn't mean that it wasn't working quite hard. Like hiring a contractor to build a shed, and having to supply the crew with lunch before they get to work on the shed. Before lunch, they are washing your car, mowing your grass, painting your house, pruning your roses, etc. After lunch, they are building your shed. You can hardly say that they weren't working before lunch. That they weren't working on your shed is immaterial.

I'd like to implement a puldown/pullup of the interrupt after initialisation of all the processes as I would do manually like I discribed.

You don't need any pullup or pulldown resistors to make the code stop waiting for a switch press, if that is what it is doing. Just remove the while statement that is resulting in the Arduino just spinning its wheels (if that is what it is doing).

PaulS:
Seems simple enough to add, assuming you have a free digital pin.

The code currently does something when the "alarm" condition is true (it beeps, you said) and something else when the "alarm" condition is not true ("Shut the hell up, would you?"). Setting another pin high or low seems trivial.

I found this:

#define BEEPER               4
#define beepOn                1
#define beepOff               0
struct StoreStruct {
  byte chkDigit;
  char ESP_SSID[16];
  char ESP_PASS[27];
  char MyCall[10];
  byte doorMode;
  byte distMode;
  byte beeperCnt;
  byte AS3935_Capacitance;
  byte AS3935_NoiseFloorLvl;
  int timeCorrection;
  byte dispScreen;
  byte contrast;
};

StoreStruct storage = {
  '#',
  "XXX",
  "XXX",
  "XXX",
  AS3935_OUTDOORS,
  AS3935_DIST_EN,
  2,
  112,
  2,
  1,
  4,
  55
};

and

    if (storage.beeperCnt > 0 && minutes[0] >= storage.beeperCnt && minuteBeeped == 0) {
      for (int i = 0; i < 5; i++) {
        digitalWrite(BEEPER, beepOn);
        delay(200);
        digitalWrite(BEEPER, beepOff);
        delay(200);
        minuteBeeped++;
      }

I know the beeper is set to pin D4 and pin D8 is one not in use, pritty streightforward at first glance. Defining pin D8 as alarm and alarmon set to 1 and alarmoff set to 0 and merging it in the last code snippet is my first idea.

I'm not sure what this means. Are you talking about the reset switch on the Arduino? What detector "holds"? What does "holds" even mean?

seemingly it waits for pin D2 (IRQ) to go to 0 and back to 1 again before actually begin detecting.

Links are better than clues.
Again, I have no idea what this means. What interrupt are you pulling down manually? How do you even DO that?

Sorry, I pulled the pin down defined as interrupt, I actually physicaly shortened pin D2 to GND with a wire.

I can assure you that the software was working before you did whatever you did. What it was doing may not have been what you wanted, but that doesn't mean that it wasn't working quite hard. Like hiring a contractor to build a shed, and having to supply the crew with lunch before they get to work on the shed. Before lunch, they are washing your car, mowing your grass, painting your house, pruning your roses, etc. After lunch, they are building your shed. You can hardly say that they weren't working before lunch. That they weren't working on your shed is immaterial.
You don't need any pullup or pulldown resistors to make the code stop waiting for a switch press, if that is what it is doing. Just remove the while statement that is resulting in the Arduino just spinning its wheels (if that is what it is doing).

I agree, the software was "working" as it was. But the detector was screaming fire and murder in a cage of faraday and thus any result where mute as it wasn't detecting anything but events it was creating itself.

The software worked as it had a flaw in the electronics design. Now the flaw is gone and the rising of the interupt pin D2 isn't happening irraticly as it did with the flaw. So a change in the software is needed as like you stated that it's spinning it wheels.

The "while" statement, is that actually what I'm looking for? like ctrl-F and find it?

like:

  while (0 == AS3935_ISR_Trig && analogRead(BUTTON) > 500 && minute == lastMinute && !exitLoop) {
    if (buttonTime > 0 && millis() - buttonTime > 10000) {
      exitLoop = true;
      buttonTime = 0;
    }
  }
  exitLoop = false;
  digitalWrite(LED, bckLightOff);  

 if (AS3935_ISR_Trig > 0){
    fromSource = FROMLIGHTNING;
    digitalWrite(LED, bckLightOn);
  }
  delay(5);
  detachInterrupt(digitalPinToInterrupt(2));

  myButton = analogRead(BUTTON);

it seems to me that I had to add something here to pull pin D2 down and up again?

seemingly it waits for pin D2 (IRQ) to go to 0 and back to 1 again before actually begin detecting.

But, does it know that pin went LOW then HIGH because an interrupt handler was called, or does it know that because it polls the pin, and does nothing until the pin goes LOW?

PaulS:
But, does it know that pin went LOW then HIGH because an interrupt handler was called, or does it know that because it polls the pin, and does nothing until the pin goes LOW?

Good question, witch made me backtrack and found out I was wrong. The interrupt pin D2 stays LOW after I pull the pin to ground.

As is stated in the AS3935 datasheet

Whenever events happen, the AS3935 pulls the IRQ high and displays the interrupt in the
REG0x03

and

The interrupt bus IRQ is set back to low whenever the interrupt register is read out.

as is in the code:

  // reset interrupt flag
  AS3935_ISR_Trig = 0;
  attachInterrupt(digitalPinToInterrupt(2), AS3935_ISR, RISING);

The pin D2 should be LOW by default and should be stated to be LOW somewhere?

onfo on the arduino.cc website

This also means however, that pins configured as pinMode(pin, INPUT) with nothing connected to them, or with wires connected to them that are not connected to other circuits, will report seemingly random changes in pin state, picking up electrical noise from the environment, or capacitively coupling the state of a nearby pin.

I'm going to test with a 10k resistor to GND and report back.

Edit: no solution

I found:

{
  digitalWrite(2, LOW);        // sets the digital pin 2 off
}

But still pin 2 is HIGH.

In the boot cycle pin 2 swaps from HIGH to LOW a couple of times and gets stuck on HIGH at the end.

I'm lost I'm afraid :frowning:

An other guy who's, as I am, one of the 50 plus who bought that kit what went south. Discovered that the interrupt HIGH wasn't coming from the Arduino, but from the AS3935 detector module and began copy/pasting and swapping things around with the example code and got it to work-ish. It didn't work immediate after resetting, but after the first detection it did.

By looking at is I discovered he hadn't got the pinMode line in his code, so I added is and yeeeeeeeeeeee! :smiley: It worked, but no idea what we've done :zipper_mouth_face:

#include "I2C.h"
#include "PWFusion_AS3935_I2C.h"
#include <Adafruit_GFX.h>
#include <SoftwareSerial.h>
#include <RDKESP8266.h>
#include <EEPROM.h>

#define LINECLR               1
#define GRFCLR                1
#include <Adafruit_PCD8544.h>
#define CLK                   7 //8 (7 = final, 8 = proto)
#define DIN                  12 //12 
#define DC                   11 //11
#define CE                    9  //9
#define RST                  10 //10
#define LED                  13
#define bckLightOff           1
#define bckLightOn            0

#define beepOn                1
#define beepOff               0
// interrupt trigger global var
volatile int8_t AS3935_ISR_Trig = 0;

// defines for hardware config
#define SI_PIN               9
#define IRQ_PIN              2
#define AS3935_ADD           0x03     // x03 - standard PWF SEN-39001-R01 config

#define AS3935_INDOORS       0
#define AS3935_OUTDOORS      1
#define AS3935_DIST_DIS      0
#define AS3935_DIST_EN       1
#define BUTTON               0
#define BEEPER               4

#define btnNone              0
#define btnRight             1
#define btnDown              2
#define btnLeft              3
#define btnUp                4

#define FROMTIME             0
#define FROMMENU             1
#define FROMLIGHTNING        2
#define FROMNOTHING          3

#define HOST                "onweer.pi4raz.nl"     // Host to contact
#define PAGE                "/AddEvent.php?Callsign=PA2RDK&IntType=1&Distance=18" // Web page to request
#define PORT                80

#define dispMinute           0
#define dispHour             1
#define dispDay              2
#define dispStat             3
#define dispHist             4
#define dispTijd             5
#define dispLogo             6
#define dispMenu             7
#define dispMax              5

#define offsetEEPROM       0x0    //offset config

struct StoreStruct {
  byte chkDigit;
  char ESP_SSID[16];
  char ESP_PASS[27];
  char MyCall[10];
  byte doorMode;
  byte distMode;
  byte beeperCnt;
  byte AS3935_Capacitance;
  byte AS3935_NoiseFloorLvl;
  int timeCorrection;
  byte dispScreen;
  byte contrast;
};

StoreStruct storage = {
  '#',
  "XXX",
  "XXX",
  "XXX",
  AS3935_OUTDOORS,
  AS3935_DIST_EN,
  2,
  112,
  2,
  1,
  4,
  55
};

bool isConnected = false;
char receivedString[128];
char IPNo[18];
byte debug = 1;
int myButton = 0;
char chkGS[3] = "GS";

byte minutes[60];
byte maxMinute = 0;
byte divMinute = 1;

byte hours[48];
byte maxHour = 0;
byte divHour = 1;
byte heartBeatCounter = 55;

byte days[30];
byte maxDay = 0;
byte divDay = 1;
uint16_t pulses = 0;
byte minuteBeeped = 0;
unsigned long buttonTime = 0;
bool exitLoop = false;
bool dispStarted = false;
bool handleTime = 0;
int majorVersion=1;
int minorVersion=2; //Eerste uitlevering 15/11/2017


struct histData {
  byte dw;
  byte hr;
  byte mn;
  byte sc;
  byte dt;
};

histData lastData[10];

byte second = 0;
byte doubleSecond = 0;
byte minute = 0;
byte lastMinute = 0;
byte hour = 0;
byte lastHour = 0;
byte dayOfWeek = 1;
byte lastDayOfWeek = 1;
byte fromSource = 0;
byte dispSide = 0;
byte startPos = 0;
byte height;
byte btnPressed = 0;

// prototypes
void AS3935_ISR();

PWF_AS3935_I2C  lightning0((uint8_t)IRQ_PIN, (uint8_t)SI_PIN, (uint8_t)AS3935_ADD);
Adafruit_PCD8544 display = Adafruit_PCD8544(CLK, DIN, DC, CE, RST);

SoftwareSerial esp8266(5, 3); // RX, TX
RDKESP8266 wifi(&esp8266, &Serial, -1);

const unsigned char PROGMEM lightning_bmp[32] = {
  0x01, 0xE0, 0x02, 0x20, 0x0C, 0x18, 0x12, 0x24, 0x21, 0x06, 0x10, 0x02, 0x1F, 0xFC, 0x01, 0xF0,
  0x01, 0xC0, 0x03, 0x80, 0x07, 0xF8, 0x00, 0xF0, 0x00, 0xC0, 0x01, 0x80, 0x01, 0x00, 0x01, 0x00
};

void loop()
{

  {
  digitalWrite(2, LOW);        // sets the digital pin 2 off
  }


  //if (buttonTime == 0)  dispStop();

  // reset interrupt flag
  AS3935_ISR_Trig = 0;
  attachInterrupt(digitalPinToInterrupt(2), AS3935_ISR, RISING);

  fromSource = FROMNOTHING;
  
  while (0 == AS3935_ISR_Trig && analogRead(BUTTON) > 500 && minute == lastMinute && !exitLoop) {
    if (buttonTime > 0 && millis() - buttonTime > 10000) {
      exitLoop = true;
      buttonTime = 0;
    }
  }
  exitLoop = false;
  digitalWrite(LED, bckLightOff);  

 if (AS3935_ISR_Trig > 0){
    fromSource = FROMLIGHTNING;
    digitalWrite(LED, bckLightOn);
  }
  delay(5);
  detachInterrupt(digitalPinToInterrupt(2));

  myButton = analogRead(BUTTON);

  if (lastMinute != minute)
  {
    heartBeatCounter++;
    lastMinute = minute;
    fromSource = FROMTIME;
    if (handleTime == 0) {
      moveMinutes();

      if (hour != lastHour) {
        lastHour = hour;
        moveHours();
      }
      if (dayOfWeek != lastDayOfWeek) {
        lastDayOfWeek = dayOfWeek;
        moveDays();
      }
    }
  }

  if (heartBeatCounter == 60) {
    heartBeatCounter = 0;
    if (isConnected == 1) {
      getNTPData();
      sendToSite(0, 0);
    }
  }

  if (myButton < 500) {
    fromSource = FROMMENU;
    digitalWrite(LED, bckLightOn);
    handleMenu(myButton);
  }

  dispStart(storage.contrast);

  Serial.print(F("Escaped from loop:")); Serial.print(fromSource); Serial.print(F("  Buttontime:")); Serial.println(buttonTime);

  if (fromSource < FROMNOTHING) {
    if (fromSource == FROMLIGHTNING) handleLighting();
    dispData();
    buttonTime = millis();
  }

}

void dispData() {
  dispInfo(0);

  if (storage.dispScreen == dispMinute) {
    printMinutes();
  }
  if (storage.dispScreen == dispHour) {
    printHours();
  }
  if (storage.dispScreen == dispDay) {
    printDays();
  }
  if (storage.dispScreen == dispStat) {
    printStat();
  }
  if (storage.dispScreen == dispHist) {
    printHist();
  }
  if (storage.dispScreen == dispTijd) {
    printTime();
  }
  if (storage.dispScreen == dispLogo) {
    printLogo();
  }

  refreshDisplay();
}

The only thing left is it freezing up from a user standpoint, it might wait for input of the website and spinning it's wheels, meanwhile I can't operate the buttons nor does it detect. Seemingly it ends up in this status when the connection with the website is lost. I can replecate the situation, but not every time.
By just letting it be and operate will end up in this status every time eventually.

Whats the smart question I have to ask myself?

as3935_lightning_i2c_nocal.ino (6.78 KB)

Ask yourself this. Is pin 2 an input, so that the interrupt can trigger the interrupt service routine, or an output, so that writing it LOW (or HIGH) makes sense?

If the pin mode is defined as INPUT, the ISR can be triggered, but writing HIGH or LOW to the pin does NOT make the pin HIGH or LOW. It simply turns the internal pullup resistor on or off.

If the pin mode is defined as OUTPUT, the ISR won't be triggered, but you can set the state of the pin.

So, what mode IS the pin in?

PaulS:
So, what mode IS the pin in?

The mode for pin 2 was't defined at all,even in the initial code. EDIT: It is, pin 2 is defined as IRQ_PIN and defined as input.

I added the pinMode to the setuproutine and digitalwrite to the main code my fellow peer altered and gave to me for testing and it started working. He discovered that the interrupt was HIGH deu to the AS3935

void setup()
{
  pinMode(2, INPUT);
  pinMode(LED, OUTPUT);
  pinMode(IRQ_PIN, INPUT);
  pinMode(BEEPER, OUTPUT);
  digitalWrite(BEEPER,beepOff);

  Serial.begin(115200);
  esp8266.begin(9600); // Start de software monitor op 9600 baud.
  Serial.print(F("Playing With Fusion: AS3935 Lightning Sensor, SEN-39001-R01  v"));
  Serial.print(majorVersion);
  Serial.print(F("."));
  Serial.println(minorVersion);     
  Serial.println(F("beginning boot procedure...."));

  if (EEPROM.read(offsetEEPROM) != storage.chkDigit || analogRead(BUTTON)<500){
    Serial.println(F("Writing defaults...."));
    saveConfig();    
  }

  loadConfig();
  printConfig();  

  Serial.println(F("Type GS to enter setup:"));
  delay(5000);
  if (Serial.available()) {
    Serial.println(F("Check for setup"));
    if (Serial.find(chkGS)) {
      Serial.println(F("Setup entered..."));
      setSettings(1);
      delay(2000);
    }
  }
  
  dispStart(storage.contrast);
  printLogo();
  
  // setup for the the I2C library: (enable pullups, set speed to 400kHz)
  I2c.begin();
  I2c.pullup(true);
  I2c.setSpeed(1);
  delay(2);

  noInterrupts(); // disable all interrupts
  TCCR1A = 0;     // set entire TCCR1A register to 0
  TCCR1B = 0;     // same for TCCR1B
  TCNT1 = 34286;            // preload timer 65536-16MHz/256/2Hz
  TCCR1B |= (1 << CS12);    // 256 prescaler
  TIMSK1 |= (1 << TOIE1);   // enable timer overflow interrupt
  interrupts(); // enable all interrupts

  Serial.print(F("Clear array...."));
  for (int i = 0; i < 10; i++) {
    lastData[i].dw = 0;
    lastData[i].hr = 0;
    lastData[i].mn = 0;
    lastData[i].sc = 0;
    lastData[i].dt = 0;
    Serial.print(F("."));
  }
  Serial.println();
  Serial.println(F("Array cleared...."));

  InitWiFiConnection();
//  dispStart(storage.contrast);
//  printLogo();
  delay(2000);
  clearDisplay();
  display.setTextSize(1);
  refreshDisplay();
  myButton = analogRead(BUTTON);
  
  lightning0.AS3935_DefInit();   // set registers to default
  outputCalibrationValues();
  saveConfig();
  lightning0.AS3935_ManualCal(storage.AS3935_Capacitance, storage.doorMode, storage.distMode);
  lightning0.AS3935_SetNoiseFloorLvl(storage.AS3935_NoiseFloorLvl);
  lightning0.AS3935_PrintAllRegs();
  AS3935_ISR_Trig = 0;           // clear trigger
  
  clearDisplay();
  dispInfo(8);
  refreshDisplay();
  delay(2000);
}

I added the pinMode to the setuproutine

Which one? The one that sets the hard-coded pin 2 as INPUT? Or the one that sets IRQ_PIN, which you say has the value 2, to INPUT?

Pins default to INPUT, so unless you changed a pinMode() call that set pin 2 to OUTPUT to set it to INPUT instead, that change is NOT why the code now works.

PaulS:
Which one? The one that sets the hard-coded pin 2 as INPUT? Or the one that sets IRQ_PIN, which you say has the value 2, to INPUT?

I was wrong, I didn't see the relation that IRQ_PIN was defined as pin D2 and that IRQ_PIN itself was set to input. The hard-coded pin 2 as INPUT I put there, I removed as the IRQ_PIN was already set to INPUT and the code still works.

Pins default to INPUT, so unless you changed a pinMode() call that set pin 2 to OUTPUT to set it to INPUT instead.

So the default of the pin renders the pinMode(IRQ_PIN, INPUT); mute? Then it would be safe to remove?

That change is NOT why the code now works.

Ok, back to the drawringboard. I will set the non-functional, the functional-ish and the AS3935 example code back to back and compare the three.

So the default of the pin renders the pinMode(IRQ_PIN, INPUT); mute?

It might render it "moot", not "mute".

But, no, it is always better to explicitly define the mode of all pins used.

Ok, back to the drawringboard. I will set the non-functional, the functional-ish and the AS3935 example code back to back and compare the three.

Good idea. Let us know what you find.

PaulS:
It might render it "moot", not "mute".

Tnx, didn't knew there where two. This Dutchie is on a roll :slight_smile:

Good idea. Let us know what you find.

There are no real difrences, only in the working-ish code the pinmode for the interrupt is defined as input, but is not in the non-working.

Only the sequence and preload timer is diffrent, but the discovery of the other guy that the interrupt was kept HIGH by the AS3935 kept me busey. As of in the datasheet was stated:

The interrupt bus IRQ is set back to low whenever the interrupt register is read out

The thought came to mind that the AS3935 is waiting to be read out and makes me think I have to look for a way to read/clear the register of the AS3935?

And:

in the example code I found this:

// this is irq handler for AS3935 interrupts, has to return void and take no arguments
// always make code in interrupt handlers fast and short
void AS3935_ISR()
{
  AS3935_ISR_Trig = 1;
}

What I can't find in the non-working and working-ish code.

AS3935_ISR_Trig = 0;           // clear trigger

is stated that it clears trigger, do I need to worry about that?

is stated that it clears trigger, do I need to worry about that?

No. Trigger is really the wrong word. What the variable is doing is recording that the interrupt happened. The ISR sets the value to 1.

Later, the code checks the value, and if it IS 1, does something, and sets the value back to 0 to indicate that is has dealt with the interrupt having happened.

Ok, the:

// this is irq handler for AS3935 interrupts, has to return void and take no arguments
// always make code in interrupt handlers fast and short
void AS3935_ISR()
{
  AS3935_ISR_Trig = 1;
}

I found at the far bottom of the example code, I also found in another file at the top of this:

void AS3935_ISR() {
  AS3935_ISR_Trig = 1;
}

ISR(TIMER1_OVF_vect) // Timer1_COMPA_vect timer compare interrupt service routine
{
  //digitalWrite(LED, digitalRead(LED) ^ 1); // toggle LED pin
//  TCNT1 = 34286;            // preload timer
  TCNT1 = 34230;            // preload timer
  doubleSecond++;
  if (doubleSecond > 121)
    doubleSecond = 0;
  second = doubleSecond / 2;
  if (second == 60) {
    doubleSecond = 0;
    second = 0;
    minute++;
    Serial.print(".");
    if (minute == 60) {
      minute = 0;
      hour++;
      if (hour == 24) {
        hour = 0;
        dayOfWeek++;
        if (dayOfWeek == 8) {
          dayOfWeek = 1;
        }
      }
    }
  }
}

void moveMinutes() {
  for (int i = 0; i < 59; i++) {
    minutes[59 - i] = minutes[58 - i];
  }
  minutes[0] = 0;
  maxMinute = 0;
  for (int i = 0; i <= 59; i++) {
    if (minutes[i] > maxMinute) {
      maxMinute = minutes[i];
    }
  }
  divMinute = maxMinute / 20;
  if (divMinute < 1) {
    divMinute = 1;
  }
  minuteBeeped = 0;
}

void moveHours() {
  for (int i = 0; i < 23; i++) {
    hours[23 - i] = hours[22 - i];
  }
  hours[0] = 0;
  maxHour = 0;
  for (int i = 0; i <= 23; i++) {
    if (hours[i] > maxHour) {
      maxHour = hours[i];
    }
  }
  divHour = maxHour / 20;
  if (divHour < 1) {
    divHour = 1;
  }
}

void moveDays() {
  for (int i = 0; i < 29; i++) {
    days[29 - i] = days[28 - i];
  }
  days[0] = 0;
  maxDay = 0;
  for (int i = 0; i <= 29; i++) {
    if (days[i] > maxDay) {
      maxDay = days[i];
    }
  }
  divDay = maxDay / 20;
  if (divDay < 1) {
    divDay = 1;
  }
}

But have the hunch that it should be at the far bottom of the code in this file:

void setup()
{
  pinMode(LED, OUTPUT);
  pinMode(IRQ_PIN, INPUT);
  pinMode(BEEPER, OUTPUT);
  digitalWrite(BEEPER,beepOff);

  Serial.begin(115200);
  esp8266.begin(9600); // Start de software monitor op 9600 baud.
  Serial.print(F("Playing With Fusion: AS3935 Lightning Sensor, SEN-39001-R01  v"));
  Serial.print(majorVersion);
  Serial.print(F("."));
  Serial.println(minorVersion);     
  Serial.println(F("beginning boot procedure...."));

  if (EEPROM.read(offsetEEPROM) != storage.chkDigit || analogRead(BUTTON)<500){
    Serial.println(F("Writing defaults...."));
    saveConfig();    
  }

  loadConfig();
  printConfig();  

  Serial.println(F("Type GS to enter setup:"));
  delay(5000);
  if (Serial.available()) {
    Serial.println(F("Check for setup"));
    if (Serial.find(chkGS)) {
      Serial.println(F("Setup entered..."));
      setSettings(1);
      delay(2000);
    }
  }
  
  dispStart(storage.contrast);
  printLogo();
  
  // setup for the the I2C library: (enable pullups, set speed to 400kHz)
  I2c.begin();
  I2c.pullup(true);
  I2c.setSpeed(1);
  delay(2);

  noInterrupts(); // disable all interrupts
  TCCR1A = 0;     // set entire TCCR1A register to 0
  TCCR1B = 0;     // same for TCCR1B
  TCNT1 = 34286;            // preload timer 65536-16MHz/256/2Hz
  TCCR1B |= (1 << CS12);    // 256 prescaler
  TIMSK1 |= (1 << TOIE1);   // enable timer overflow interrupt
  interrupts(); // enable all interrupts

  Serial.print(F("Clear array...."));
  for (int i = 0; i < 10; i++) {
    lastData[i].dw = 0;
    lastData[i].hr = 0;
    lastData[i].mn = 0;
    lastData[i].sc = 0;
    lastData[i].dt = 0;
    Serial.print(F("."));
  }
  Serial.println();
  Serial.println(F("Array cleared...."));

  InitWiFiConnection();
//  dispStart(storage.contrast);
//  printLogo();
  delay(2000);
  clearDisplay();
  display.setTextSize(1);
  refreshDisplay();
  myButton = analogRead(BUTTON);
  
  lightning0.AS3935_DefInit();   // set registers to default
  outputCalibrationValues();
  saveConfig();
  lightning0.AS3935_ManualCal(storage.AS3935_Capacitance, storage.doorMode, storage.distMode);
  lightning0.AS3935_SetNoiseFloorLvl(storage.AS3935_NoiseFloorLvl);
  lightning0.AS3935_PrintAllRegs();
  AS3935_ISR_Trig = 0;           // clear trigger
  
  clearDisplay();
  dispInfo(8);
  refreshDisplay();
  delay(2000);
}

Is that just a hunch? Or are I on to something?

EDIT: tried it and didn't work...

After testing again and again it "feels" like the AS3935 is waiting to the Arduino to do something, and me creating disturbers is triggering the interrupt witch makes the Arduino do something and the both come into a state of a working-ish code.

So it feels like the "jumpstart" of the code working is a detection of the AS3935 and not, as it should be, an immediate working code..

I'll try reading the datasheet again.

Reading the datasheet gave me an idea I'd like to verify.

In the datasheet is stated:

8.2 Operating Modes

*Power-down Mode.
In Power-down Mode, the entire AS3935 is switched off to reduce the current consumption to minimum.

*Listening Mode.
In listening mode the AFE, the watchdog, the noise floor level generation, the bias block, the TRCO, and the voltage regulator
(in case it is enabled) are running. In this mode the system can push down the power consumption to a minimum (typ 60μA). In case the maximum
voltage supply does not exceed 3.6V, it is possible to switch off the voltage regulator to save power.

*Signal Verification.
The AS3935 enters in this mode every time the watchdog detects dynamic activity picked up by the antenna (the incoming signal crosses a certain threshold). The IC will leave this mode either if the incoming signal is classified as disturber or if the analysis of the single event (lightning)
is finished. If the received signal is classified as a disturber, then the AS3935 will automatically go back to listening mode without any needed
action from outside and an interrupt will be generated (with option bit this interrupt can be masked). If the received pattern matches all
requirements, the energy calculation is performed and the AS3935 provides distance estimation.

and:

The output signal of the AFE is monitored by the watchdog, which enables the signal validation
(see Signal Verification on page 11)
in case the
input signal crosses a certain threshold.

and:

8.3.2 Register Table Description and Default Value

address = 0x00
Register name= PWD
bit = 0
type = R/W
Default Value = 0
Description = Power-down

The theory came to mind that the module boots into power-down mode, the first event that exeeds the threshold triggers the watchdog that put the module in listening mode witch makes the module work for future events.

Does that compute?

If yes, then the next smart question would be: "How can I change the value that in the register for the default power mode?".

Am I going somewhere?

I checked, there is no:

void powerUp()

Or something stating "power" or "PWD" in de code anywhere to bring chip out of power down mode.

EDIT:

No solution sadly enough, the default value is listening and the default values are set in the code.

Adding:

lightning0.AS3935_PowerUp();

in the code is redundant and doesn't solve the problem.

Stuck reading the datasheet and library notes again :sleeping: