The document (example) for STARTUP() suggests that any code will run. STARTUP() worked great for me until I added my own wait that called Time.now().
See snippet. If I comment out my waitAwhile, in setPins() everything is great otherwise blinking red. I call waitAwhile in loop() to show that it works fine. I’m using the DEV IDE and whatever the default system version is 4.8?
UPDATE: I removed the Particle.process() (leaving just {} and no more errors. That may be logical; but, the documentation should be clearer.
#pragma SPARK_NO_PREPROCESSOR
#include "Particle.h"
#include <math.h>
void waitAwhile(unsigned long delay);
//----------------------------------------------------------------
void setPins()
{
pinMode(D4, OUTPUT); // control valve relay HIGH means open
digitalWrite(D4, HIGH);
waitAwhile(1); // this the killer; comment out this statement and it works great.
}
STARTUP(setPins()); //get the pins setup ASAP
//
void setup()
{
Serial.begin(9600);
waitAwhile(5);
}
void loop()
{
int i = 10;
while (i>0)
{
Serial.println(i);
i --;
waitAwhile(1);
}
System.enterSafeMode(); // exit
}
//----------------------------------------------------------------
//
//---------------------------------------------------------------
void waitAwhile(unsigned long delay)
{
unsigned long t0 =Time.now() + delay;
while (Time.now() <= t0) {Particle.process();}
}