user14394: I have been trying to exit from the while(1) loop by getting a value from the sensor and then run the code outside while(1).But the problem is that it does not exit. How may I fix this problem?
See Code Below.......
void setup() {
    pinMode(2,INPUT);
    Serial.begin(9600);
}

void loop() {
    int sensor;
    while(1)
    {
        sensor = digitalRead(2);
        if(sensor == 0)
        {
            Serial.println("inside while loop");
        }
        break;
    }
    Serial.println("Break......");
}


user66107: instead of while(1), one can use the variable name in while and change the value of the variable when you want to exit the loop. part of the code can be modified based on the following concept. 
bool condition= true; 
while(condition){ 
if (sensor==0)
condition=false;
This is my suggestion for the above problem. 

user69863: Used like this
void setup() {
    pinMode(2,INPUT);
    Serial.begin(9600);
}

void loop() {
     bool cond = true;

int sensor;
while(cond)
{
    sensor = digitalRead(2);
    if(sensor == 0)
    {
        Serial.println("inside while loop");
    }
    cond = false;
}

  // like this you can used while inside the void loop()

}


user20245: In the Arduino paradigm, unlike ordinary C programming where there is only 1 entry point called "main()", there are 2 entry points called "setup()" and "loop()".  "setup()" is called only once after booting up.  But "loop()" is called over and over again right after "setup()" is called.  There is no need to create your own infinite loop (for example a "while(1)" statement) as in ordinary C programming.

14394: <p>I have been trying to exit from the <code>while(1)</code> loop by getting a value from the sensor and then run the code outside <code>while(1)</code>.But the problem is that it does not exit. How may I fix this problem?</p>

<p>See Code Below.......</p>

<pre><code>void setup() {
    pinMode(2,INPUT);
    Serial.begin(9600);
}

void loop() {
    int sensor;
    while(1)
    {
        sensor = digitalRead(2);
        if(sensor == 0)
        {
            Serial.println("inside while loop");
        }
        break;
    }
    Serial.println("Break......");
}
</code></pre>
 66107: <p>instead of while(1), one can use the variable name in while and change the value of the variable when you want to exit the loop. part of the code can be modified based on the following concept. 
<code>bool condition= true; 
while(condition){ 
if (sensor==0)
condition=false;</code>
This is my suggestion for the above problem. </p>
 69863: <p>Used like this</p>
<pre><code>void setup() {
    pinMode(2,INPUT);
    Serial.begin(9600);
}

void loop() {
     bool cond = true;

int sensor;
while(cond)
{
    sensor = digitalRead(2);
    if(sensor == 0)
    {
        Serial.println(&quot;inside while loop&quot;);
    }
    cond = false;
}

  // like this you can used while inside the void loop()

}
</code></pre>
 20245: <p>In the Arduino paradigm, unlike ordinary C programming where there is only 1 entry point called "main()", there are 2 entry points called "setup()" and "loop()".  "setup()" is called only once after booting up.  But "loop()" is called over and over again right after "setup()" is called.  There is no need to create your own infinite loop (for example a "while(1)" statement) as in ordinary C programming.</p>
