Functions called in setup() are ignored

Very weird behavior: my app seems to be completely ignoring two functions that are explicitly called from setup(). Here’s the code:

    // setup() runs once, when the device is first turned on.
    void setup() {

      // Put initialization like pinMode and begin functions here.
      Serial1.begin(115200);
      Serial1.println("Hello Steve!");
      delay(1000);
      
      // DS18B20 sensors setup
      onewire.search(sensorAddress);
      if (sensorAddress[0]) {
        sensors.begin();
        sensors.request(sensorAddress);
      }

      // initialize the i2c bus
      Wire.begin();

      // run the calibration loop
      calibrateSensors();
      saveConfiguration();
      
      if (connectToWiFi() == true) {
      	if (client.connect(server, 5500)) {
      		Serial1.println("Connected!");
      	} else {
      		Serial1.println("Connection failed!");
      	}
      }
    }

The calls to calibrateSensors() and saveConfiguration() are never called. It skips directly to the Wifi connection routine.

I’m using a couple of structs in this app, so I have the preprocessor turned off. Both of those functions are declared above setup(). I’m not getting any compile errors. The rest of the code works. I’ve changed the “Hello Steve” bit just to make sure I’m running the right version and it all looks good.

Any ideas?

Thanks!

-S

Are you really using a Particle device?
Your use of Serial1 for debug output suggests different.

If so, you should be aware that this community is focused on Particle devices and is not a general Arduino forum.

If you are using Particle, then ...

What WiFi connection routine?
If you are not explicitly setting SYSTEM_MODE() to a non-AUTOMATIC mode nor use SYSTEM_THREAD(ENABLED) WiFi will get connected before setup() even gets called and hence your code may get stuck somewhere after your two Serial1.print() statements but still have WiFi connection already established.

This is indeed a Particle - it’s a prototype of a product that will ultimately be built around the P1.

I am using it manual mode. The Wifi networks to which these devices will connect are installed in airplanes, so no cloud available.

I’m using Serial1 for debug output and Serial for data output (airplane people are big no redundancy, so USB + Wifi).

That said… I think I figured out the issue. Both the calibration and settings update routines had loops that had counters that were not initialized. As in:

for (int x; x < 1000; x++) {
  // do calibration test
}

Rather than:

for (int x = 0; x < 1000; x++) {
 // do calibration test
}

All of the action took place inside the loops, so I didn’t see any debug output and I didn’t wind up with a valid calibration or anything stored in the EEPROM. Seems a bit strange that the lack of an initial value doesn’t cause an error - or that the int isn’t simply initialized to zero, causing the routine to work right.

Apologies for the erroneous error report. All working now.

Since it's declared on the stack, it's likely to be a re-used memory location. Generally the RAM is zeroed out on boot.

1 Like