use (if) cases when LED is high/ LOW

Hi,

I want a certain thing to happen but only when the LED is HIGH. How do I do this with an if case? Because I can't get it to work in my main code.

int LED = 3;
int LED2 = 4;
void setup(){

 pinMode(LED, OUTPUT);
 pinMode(LED2, OUTPUT);
}

void loop(){
 
 digitalWrite(LED, HIGH);

 if(LED, HIGH){
  
  digitalWrite(LED2, HIGH);
 }
 else{
  
  delay(5000);
  digitalWrite(LED, LOW);
 }
}
const byte pinOfYourLed = 3;

if (digitalRead(pinOfYourLed) == HIGH) {
 // it's HIGH

} else {
  // it's not HIGH (probably LOW)

}

J-M-L:

const byte pinOfYourLed = 3;

if (digitalRead(pinOfYourLed) == HIGH) {
// it's HIGH

} else {
 // it's not HIGH (probably LOW)

}

Okay I'll try this out! Thank you for the quick help!

digitalWrite(LED, HIGH);

 if(LED, HIGH){

You've just set it HIGH - why would you want to test it (badly) to see if it is HIGH?

TheMemberFormerlyKnownAsAWOL:
You've just set it HIGH - why would you want to test it (badly) to see if it is HIGH?

Доверяй, но проверяй. (trust but verify :slight_smile: :grin: )

J-M-L:
Доверяй, но проверяй. (trust but verify :slight_smile: :grin: )

But can you really trust digitalRead() ?

that's a good question indeed!

rico459:
Hi,

I want a certain thing to happen but only when the LED is HIGH. How do I do this with an if case? Because I can't get it to work in my main code.

int LED = 3;

int LED2 = 4;
void setup(){

pinMode(LED, OUTPUT);
pinMode(LED2, OUTPUT);
}

void loop(){

digitalWrite(LED, HIGH);

if(LED, HIGH){
 
 digitalWrite(LED2, HIGH);
}
else{
 
 delay(5000);
 digitalWrite(LED, LOW);
}
}




@rico459
your above code doesn't make any sense.
you turn LED on, and you test it if it is ON, which will always be true. (the else statement will never execute).

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