const int buttonPin = 2;
int buttonState = 0;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
Serial.println("1");
buttonState=LOW;
delay(20000);
while(0);
}
}
Basically the code works like this:
- the number of the pushbutton pin
- variable for reading the pushbutton status
- initialize the pushbutton pin as an input:
- read the state of the pushbutton value:
- check if the pushbutton is pressed. If it is, the buttonState is HIGH: send char 1 via Bluetooth:
I have an Arduino, HC 06 bluetooth module, a button and an app that makes a phonecall when the button is pressed (HC 06 send a byte, 1, to the app)
My question is, what's the while (0);
for?
----ORIGINAL CODE---- const int PirSensor = 2; int motionState = 0;
void setup() {
Serial.begin(9600);
pinMode(PirSensor, INPUT);
}
void loop() {
motionState = digitalRead(PirSensor);
if (motionState == HIGH) {
Serial.println("1");
motionState = LOW;
delay(20000);
// while(0);
}
}
while (0);
is at the end of ado { … } while (0);
loop. I think it is fair to argue it is the only context in whichwhile (0);
is useful. As it stands, the empty body of the loop is never executed because the loop continuation condition is unconditionally false (zero). An alternative which is commonly seen is an infinite loop —while (1) { … }
and sometimes that will have an empty loop body. This time the condition is unconditionally true (non-zero). - Jonathan Leffler