Check if LED State is "HIGH"

Hi.

How can I check if a LED state is hight in void loop?

I tried this:

int ledIn = 13;
int ledOut = 13;
int ledtwo = 8;

void setup() {                

  pinMode(ledOut, OUTPUT); 
  pinMode(ledIn, INPUT); 
  pinMode(ledtwo, OUTPUT);
 
}


void loop() {
    
if(digitalRead(ledIn == HIGH)) 
{
digitalWrite(ledtwo, LOW);
}else{
digitalWrite(ledtwo, HIGH);
}

  digitalWrite(ledOut, HIGH);   
  delay(1000);               
  digitalWrite(ledOut, LOW);  
  delay(1000);

But it wont work (the code is propably full of mistakes, but sorry, I'm a beginner :D)

Coueld you help me?

Thanks a lot!

You can just read back the value of a PIN an assign it directly to a digital write. the ! inverts the value.

int led1 = 13;
int led2 = 8;

void setup()
{ 
  Serial.begin(9600);             
  pinMode(led1, OUTPUT); 
  pinMode(led2, OUTPUT);
}

void loop() 
{
  digitalWrite(led1, HIGH); 
  digitalWrite(led2,  !digitalRead(Led1));  
  delay(1000);        
       
  digitalWrite(led1, LOW); 
  digitalWrite(led2,  !digitalRead(Led1));
  delay(1000);  
}

If you want it to happen always, you better encapsulate the behavior in one function.

int led1 = 13;
int led2 = 8;

void setup()
{ 
  Serial.begin(9600);             
  pinMode(led1, OUTPUT); 
  pinMode(led2, OUTPUT);
}

void loop() 
{
  setLed(HIGH); 
  delay(1000);        
       
  setLed(LOW); 
  delay(1000);  
}

void setLed(int v)
{
  digitalWrite(led1, v); 
  digitalWrite(led1, !v); 
}

Hint: read the section about using arrays if you want to use multiple leds.

robtillaart I think there is little mistake in your code, corrected:

void setLed(bool v)
{
  digitalWrite(led1, v); 
  digitalWrite(led2, !v); 
}
if(digitalRead(ledIn == HIGH))

Compare this with any of the other examples and notice the difference with the parenthesis.

Thanks for your answers!

digitalWrite(led2,  !digitalRead(Led1));

That actually works, but if I want to make it with "If" it would need to be like this, or? :

if(digitalRead(led1, HIGH))

Thanks

No that would be

if( digitalRead(led1) == HIGH ) // if ( function == value )

But, as HIGH is 1 and LOW is 0, you could just use boolean operation:

if( digitalRead(led1) ) // if HIGH

if( !digitalRead(led1) ) // if LOW

Thank you! It's working, I'm slowly beginning to understand the programming-language :slight_smile:

  • Sosmann

How can I check if a LED state is hight in void loop?

Let's think about that... Your software (your sketch) is turning the LED on & off... There should be no reason to check, because your software already "knows" if it's turned the LED on or off.

You (your sketch) is always controlling the state of output pins... If you have to read the state of an output pin, you are probably doing something "wrong".

If you need to keep track, you can create a variable (as robtillaar used the variable 'v'). Then, you can check the state of the variable, instead of the state of the pin.

FYI - In the following code ledIn and ledOut are both 13.
That's OK by itself... But, first you set ledOut (pin 13) to OUTPUT. Then, you set ledIn (pin 13 again) to INPUT. As soon as you set the pin to input, it's no longer an output.

In other words, setting it to output has no effect, if you immediatly change it to input.

int ledIn = 13;
int ledOut = 13; 
int ledtwo = 8;

void setup() {                

  pinMode(ledOut, OUTPUT);
  pinMode(ledIn, INPUT); 
  pinMode(ledtwo, OUTPUT);
 
}

i have a program , that turn on led and turn it off .. but i want my arduino to give me the status of my led .. and to send bytes by bluetooth to my android phone .. please can you help me ..
if led is high
send byte 6
else send byte 7
or whatever byte ..
but i need to c the status of my device before controlling it..
please can u help

Hello,

Have you read the replies in this topic?