Void loop() not running or changing data

I have an Pro Mini running code that counts the pulses of a weather station wind speed anemometer using an ISR. The data from the Mini is polled over I2C by an ESP8266 every 10 seconds. The I2C works fine and I can read the returned data if it is assigned in the Wire.onRequest(sendEvent) function or setup() but there seems to be a problem in the main loop.

My plan was to have the Mini look after calculating the wind speed every 3000mS and when polled, return the current speed. I needed a second processor for the speed as it it time sensitive. (pulses per sec = speed) The ESP is sending data to a server every 5 seconds and can hang for a time before finishing so the speeds were not calculating correctly.

In the main loop I have a simple millis routine to calculate the speed every 3000mS but it doesn't want to time. I have also placed code in the loop and outside of the millis routine to assign variables to the I2C data but the values never get changed.

It's as if the loop isn't looping.

Sorry if this sounds weird or odd but I have no idea as to why the loop isn't changing anything or timing. This is the first time in in my years programming these things that I've ever run across this issue. Any help would be appreciated.

// Arduino Pro Mini
#include <Wire.h>

unsigned long currentMillis = 0;
unsigned long lastMillis = 0;

int windPulseCount = 0;
int windspeedKPH = 0;

const int interruptPinWind = 2;
byte myData[4];
int delaytime = 3000;
float timed;

void setup() {
  myData[0] = 0;
  myData[1] = 0;
  myData[2] = 88; // TEST DATA VALUE
  myData[3] = 0;
    
  Wire.begin(8);  //0x08 =8
  Wire.onRequest(sendEvent);
  pinMode(interruptPinWind, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPinWind), ISR_update_wind_speed, FALLING);
}

void loop() {
  //UPDATE SPEED VALUE
  // This if statement never runs.
    currentMillis = millis;
    if (currentMillis - lastMillis > delaytime) { 
      lastMillis = currentMillis;
      timed = delaytime / 1000;
      windspeedKPH = windPulseCount * (2.4 / timed);
      myData[0] = (delaytime >> 8) & 0xFF;
      myData[1] = delaytime & 0xFF;
      myData[2] = windPulseCount;
      myData[3] = windspeedKPH;
      windPulseCount=0;
    }
  
    // TEST DATA TO CHANGE VALUES - 
    // THEY DO NOT CHANGE THE ORIGINAL VALUE SET IN void setup()
    myData[0] = (delaytime >> 8) & 0xFF;
    myData[1] = delaytime & 0xFF;
    myData[2] = 98; // TEST DATA VALUE
    myData[3] = windspeedKPH;
}


void sendEvent(){
  // If I set values here, they change.
  Wire.write(myData, 4); //data bytes are queued in local buffer
}

void ISR_update_wind_speed() {  
  // A wind speed of 2.4km/h causes the switch to close once per second. (1.491 miles/h)
  windPulseCount++;
}

The code reading the data works fine.

Wire.requestFrom(8, 4);//0x08 = 8;
    if(Wire.available() == 4)
        {
          data[0] = Wire.read();
          data[1] = Wire.read();
          data[2] = Wire.read();
          data[3] = Wire.read();
        }

do you mean millis();?

1 Like

The first error I see is that millis should be millis()

You do realize that lastMillis is zero on the first iteration?

Weird that I missed that. Thanks!

The loop is running now but why would the compiler not throw an error and why would the rest of the code still function if written outside of the loop? Eg: in the sendEvent() routine.

I do. But it isn't an issue on startup. Thanks for pointing that out.

Likely because the compiler is doing an implicit type conversion from Function Pointer to unsigned long. Try turning up your compiler warnings in File --> Preferences

What @ gfvalvo said. I suggest you get yourself Visual Studio 2019 and VisualMicro plugin. It is not free but starts with trial, then it is not that expensive. I cant imagine doing any project bigger than blink.ino in Arduino IDE

I already had the compiler warnings set to "All" and it still didn't throw an error. Weird.

I'll have a look into that. Thanks!

Indeed. What platform?

Compiled for an Uno:

void setup() {
  Serial.begin(115200);
  delay(1000);

  uint32_t x = millis;
  uint32_t y = (uint32_t)millis;

  Serial.println(x, HEX);
  Serial.println(y, HEX);
}

void loop() {
}

Warning:

C:\Users\GFV\AppData\Local\Temp\arduino_modified_sketch_886680\sketch_may29a.ino: In function 'void setup()':

C:\Users\GFV\AppData\Local\Temp\arduino_modified_sketch_886680\sketch_may29a.ino:5:16: warning: invalid conversion from 'long unsigned int (*)()' to 'uint32_t {aka long unsigned int}' [-fpermissive]

   uint32_t x = millis;

                ^~~~~~

Sketch uses 1802 bytes (5%) of program storage space. Maximum is 32256 bytes.
Global variables use 188 bytes (9%) of dynamic memory, leaving 1860 bytes for local variables. Maximum is 2048 bytes.

Arduino IDE. Not sure of the version off the top of my head. Busy cutting a sign for a client on my homemade CNC wood router right now. I can give you more info later if needed.

this is really good information, thank you for sharing with us...