Is it better to store the digitalread value and then use if statement or use digitalread directly inside the if statement

For example,
in a loop, which is better:

case 1)
int VALVE_switch = 9; //digital signal pin

void loop() {
if(digitalRead(VALVE_switch) == HIGH){
...
}

case 2)
int VALVE_switch = 9; //digital signal pin

int value; //variable to store the value of digital signal pin

void loop() {
if(digitalRead(value) == HIGH){
...
}

Is there an advantage to either?


case 2)
int VALVE_switch = 9; //digital signal pin

int value; //variable to store the value of digital signal pin

void loop() {
if(digitalRead(value) == HIGH){
...
}

Case #2 is wrong, please try again.

It depends if you want to use the value again and expect consistency.

You probably meant:

case 2)

const int VALVE_switch = 9; //digital signal pin

int value; //variable to store the value of digital signal pin

void loop() {
  value = digitalRead(VALVE_switch);
  if(value == HIGH){

The choice is between:
case 1)

  if(digitalRead(Pin) == HIGH) {

or
case 2)

  int value = digitalRead(Pin);
  if(value == HIGH) {

Use case 2 if you need to test the same state of the pin more than once. Use case 1 if you only look at the pin once in loop() OR IN A 'while' LOOP:

  while (digitalRead(Pin) == HIGH) {

If you are using a 'while' loop to repeat something as long as the input pin is in a certain state you MUST re-read the input pin each time or you will never see a change.

7 posts were split to a new topic: Even if the input is "boy" it will still return "Kai"

Hello
You may open your own information request instead of highjacking one!