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......");
}
while(1)
loop executes once, every timeloop()
is called, printing or not, depending on the state of pin2. I would expect this code to print: either "inside while loop" (if pin 2 is false) or nothing, followed by "Break......"; over and over again. Is that what you're getting? Your question is incomplete. Please describe what you expected to see, and what you do see, on the terminal. – JRobert Nov 29 at 3:20loop
function loops (is called repeatedly). Why do you put an indefinite loop inside a function that is called indefinitely? – Nick Gammon♦ Dec 15 at 6:35