if (pin = HIGH)...

Hello, can I do this or what's the right way to do it? Thank You

int power = 0;          //Where to store the value
const int inPower = 4;  //Pin on the Arduino

void setup(){

  pinMode(inPower, INPUT);
}

void loop(){

  int power = digitalRead(inPower);
  if (power = HIGH){
  //do something 
  }
}

= is assignment.

== is comparison.

What you have will assign HIGH to the variable power, and use that value (HIGH is 1 is true) in the if statement, which isn't what you want.

As an aside, you can also do

if (digitalRead(pin)==HIGH)

or even - since HIGH is 1 (true) and LOW is 0 (false)

if (digitalRead(pin))

Thank Your very much :smiley:
The way you said it (digitalRead(pin)) , make it way more simple.

DrAzzy:
or even - since HIGH is 1 (true) and LOW is 0 (false)

if (digitalRead(pin))

But the Arduino API for digitalRead() is defined as returning HIGH or LOW not 1 / 0 or true/false.
The specific values for HIGH and LOW are never specified and not guaranteed to be any particular values.
https://www.arduino.cc/reference/en/language/functions/digital-io/digitalread/

To make the assumption that HIGH is 1 and LOW is 0 or to the return value as a boolean return value is abusing the API.
Yes you can go look at the code and see that HIGH is 1 and LOW is 0 but the actual API never guarantees the values.
An implementer of that function is free to use any values and still conform to the API.

From a best practices programming point of view it is best to not abuse an API so the better choice would be

if (digitalRead(pin)==HIGH)

And if HIGH happens to be 1, it will generate the same compiled code as

if (digitalRead(pin))

which is not guaranteed to always work since the API offers no assurance that HIGH is a non zero value.

--- bill

The specific values for HIGH and LOW are never specified and not guaranteed to be any particular values.

the values are well defined:

from arduino.h

#define HIGH 0x1
#define LOW  0x0

the return value from digitalRead is even an int, returning LOW or HIGH

from wiring_digital.c

int digitalRead(uint8_t pin)
[/quote]

so the function WILL return 1 or 0.

if someone decides to switch the definitions behind HIGH and LOW, these digitalRead comparisons will be the least problem...

noiasca:
the values are well defined:

from arduino.h

#define HIGH 0x1

#define LOW  0x0

You are looking at the internal implementation details of the underlying code.
Those values are not part of the actual documented API.
Using implementation details that are not part of an official API is abusing an API and goes against programming best practices.

While implementors of the digitalRead() function may choose to define HIGH as 1 and LOW as 0 they are not required to do so.

There are cases where it can make sense to abuse an API. Performance can be one of them.
However, when the choice to abuse an API is made, it should not be made lightly, and should be made with the understanding that it is possible that at some point in the future the code could break.

--- bill

DrAzzy:
As an aside, you can also do

if (digitalRead(pin)==HIGH)

or even - since HIGH is 1 (true) and LOW is 0 (false)

if (digitalRead(pin))

I beg to differ;

int power = digitalRead(inPower);
  if (power == HIGH){
  //do something
  }

Is better as it is readable.
Reading ALL your inputs first in a loop, where possible, makes for better code and debugging.

You take a snapshot of your project’s input and act apon it.
Then
Take another snapshot of your project’s input and act apon it.
KISS…

Tom… :slight_smile: