0
votes
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);
  }

}
2
It doesn't do anything, like that. Why did you put it there? - hobbs
Because I addapted my code from another that, instead of a button, it had an PIR motion detector. And i couldn´t understand what was the meaning of the while. - Josh Miles
@PedroFerreita Can you please show the original example then? - orhtej2
The most common context in which you see while (0); is at the end of a do { … } while (0); loop. I think it is fair to argue it is the only context in which while (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
I have seen do{} while(0) used in pre-processor macros to guard against something but I can't remember what it was. I think it was to make sure that multi-statement macros that got put behind if statements without braces like they were single lines would act right. - Delta_G

2 Answers

0
votes

Well if I am not mistaken in this case nothing.

In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement.

Note that if you do have a condition, a while loop must have an exit option, like this:

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);
    int a =0;
      while(a==0){
        if (something)
          a=1;
      }
  }
}

Then on your chosen condition you will exit the loop.

0
votes

while(0) is most commonly used in macro definitions, specifically in those which are meant to be used like void functions or other instructions, like

#define inc(x) do { x = ((x)+1); } while (0)

The reason to use it is simply to make the use of the macro look like an invocation of a function by forcing the user to append a ; to the macro invocation. In the above example,

inc(a)
a = a*2;

would cause a compile error due to the missing ; after the while(0) the macro expands to, so you have to write

inc(a);
a = a*2;

which makes the code look more like inc was a function. Also, embedding the macro's code in a loop like this helps in preventing accidental use of the macro as an expression, akin to a void function. ( a = ((a)+1) is a valid expression, so b = inc(a) would be valid too (expands to b = a = ((a)+1)) if the macro did not contain the (semantically irrelevant) while loop.)