Particle loses cloud connectivity due to loop in program

The program below caused my particle to lose connectivity to the cloud indefinitely (green LED rising and falling slowly) after running for about 20 seconds - The program continued to run all the same.

When I removed the "while (true) {…} loop, everything returned to normal.

Can you explain the reason behind this?

const int Sin = D0;
int val = 0;

void setup() 
    pinMode(Sin, INPUT);
    Serial.print ("Opened the serial port and printing");

void loop () 
    while (true)
        val = digitalRead(Sin);

        if (val==1) 

            Serial.println ("Switch pressed");
            Serial.println (micros());
            delay (100);

The Photon need to reach out to the cloud every once in a while to check in to let it know it’s online. Your while loop is blocking that, therefor not allowing it to communicate.

Why would you place an infinite while loop within an already infinite loop()?

Try placing a Particle.process() within the while, or remove the while altogether.

You can also use multithreading, which is not enabled by default, to separate the user loop and connection loop. You can learn more about multithreading in our documentation, here:

Thank you for your valuable replies. You are quite correct when you ask why I would place a loop within a loop, and Yes, it was done in error (python habits die hard). I solved the problem before the post, but could not understand WHY the Particle lost its cloud connection, hence continuing with the post.

Moors7 explains the reason and it makes good sense in hindsight.

Will, I have not yet done any multithreading but will be tackling this shortly. The little bit of research I have done shows that it is not too difficult. Thanks for the link.