Create time out for Spark.connect()?

  1. Good to know
  2. Please show your new code as the interrupt and volatile variable show up in about 5 places, and all of this is supposed to be helping beginners.
  3. Actually 0, 1, 2 is needed since 0 and 1 does not prove Wifi has been disconnected and could show a false positive. But seriously we just want working code, let the academics stress about correctness.

Anyway, got tired of waiting for someone to test my code so I thought I would include it in my Rover. Here is the slightly changed code that works:
With Wifi in range, it connects quickly breaths cyan, waits 20 seconds and then flashes D7 fast to show Wifi Cloud connectivity.
Without Wifi nearby it flashes green for 20 seconds then shows solid D7 HIGH and strangely breaths cyan??

Here is the code:

SYSTEM_MODE(SEMI_AUTOMATIC);

int myConnect = 0;      // means neither connected or disconnected yet

void setup() {

   pinMode(D7, OUTPUT);  
   Spark.connect();             // non-blocking attempt to connect to Wifi

   delay(20000);   // wait 20 seconds at startup


   if (WiFi.ready()){   
        myConnect = 1;   // means wifi got connected
    } else {
        Spark.disconnect(); 
        WiFi.off();
        myConnect =2;   // means no wifi cloud connectivity
        digitalWrite(D7,HIGH);     // D7 high says no wifi but spark working fine.
      }
}

void loop() {

    // put your generic code here that works if Wifi connected or not

    if (myConnect == 1){   // only put code here that needs wifi
    
        digitalWrite(D7,HIGH);     // fast blink says Wifi connected
        delay(50);
        digitalWrite(D7,LOW); 
        delay(50);    
        
        // put your code here that needs wifi Cloud connectivity
    }


    if (myConnect == 2){   // only put code here that is fine with no wifi
    
      // put your code here that runs without Wifi Cloud connectivity
      // LED D7 should be HIGH to prove Wifi has been disconnected

    }
}