Understanding while(digitalRead())

Hello!

I am working to convert this following block of code into python so I can run it on my Raspberry Pi. In doing so I am trying to understand what each line does and was hoping someone could provide insight! Here is the code:

{                               
  pinMode(pin, OUTPUT);         // Step 1, part 1
  digitalWrite(pin, HIGH);      // Step 1, part 2
  delay(1);                     // Step 2
  pinMode(pin, INPUT);          // Step 3  part 1
  digitalWrite(pin, LOW);       // Step 3, part 2
  long time  = micros();        // Step 4  
  while(digitalRead(pin));      // Step 5
  time = micros() - time;       // Step 6 & 7
  return time;                  
}

The code was taken from a tutorial on learn.parallax. Step 5 is where I am trying to get further clarification. According to the website the purpose is to *"*Wait for the voltage to decay and pass below the Arduino’s 2.1 V threshold". It would make sense if the code said:

while(digitalRead(pin)== HIGH);

But it doesn't not so any clarification would be great. I have tested this code on my Arduino so I know that it works, just trying to understand why. Bonus points to anyone who can give me the python equivalent line using CircuitPython! :slight_smile:

mitchb705:
Hello!

I am working to convert this following block of code into python so I can run it on my Raspberry Pi. In doing so I am trying to understand what each line does and was hoping someone could provide insight! Here is the code:

{                               

pinMode(pin, OUTPUT);        // Step 1, part 1
  digitalWrite(pin, HIGH);      // Step 1, part 2
  delay(1);                    // Step 2
  pinMode(pin, INPUT);          // Step 3  part 1
  digitalWrite(pin, LOW);      // Step 3, part 2
  long time  = micros();        // Step 4 
  while(digitalRead(pin));      // Step 5
  time = micros() - time;      // Step 6 & 7
  return time;                 
}




The code was taken from a tutorial on [learn.parallax](http://https://learn.parallax.com/tutorials/robot/shield-bot/robotics-board-education-shield-arduino/chapter-6-light-sensitive-6). Step 5 is where I am trying to get further clarification. According to the website the purpose is to *"**Wait for the voltage to decay and pass below the Arduino’s 2.1 V threshold".* It would make sense if the code said: 



while(digitalRead(pin)== HIGH);





But it doesn't not so any clarification would be great. I have tested this code on my Arduino so I know that it works, just trying to understand why. **Bonus points to anyone who can give me the python equivalent line using CircuitPython!** :)

In the line:

while(digitalRead(pin));      // Step 5

digitalRead returns the state of the pin as either HIGH or LOW. HIGH is defined as 1 and LOW is defined as 0.

When no comparison is specified (e.g. "==HIGH") the while statement continues as long as the return value is non-zero. In essence:

while(digitalRead(pin));      // Step 5

and

while(digitalRead(pin) ==  HIGH);      // Step 5

do exactly the same thing.

Is the variable 'pin' defined as an actual digital pin somewhere?

Blackfin:
In the line:

while(digitalRead(pin));      // Step 5

digitalRead returns the state of the pin as either HIGH or LOW. HIGH is defined as 1 and LOW is defined as 0.

When no comparison is specified (e.g. "==HIGH") the while statement continues as long as the return value is non-zero. In essence:

while(digitalRead(pin));      // Step 5

and

while(digitalRead(pin) ==  HIGH);      // Step 5

do exactly the same thing.

Thank you so much, that makes soooooo much more sense. That answered my question perfectly and now know how to proceed with my other code!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.