why the hell doesn't this work?

#define LED_Pin 13

void setup() {

digitalWrite(LED_Pin,HIGH);

pinMode(LED_Pin,OUTPUT);

}

void loop() {
 
if (digitalRead(LED_Pin == HIGH)) {
  digitalWrite(LED_Pin,LOW);
  delay(200);
}
else {
  digitalWrite(LED_Pin,HIGH);
  delay(200);
}

}

If i do the following then it works and will blink the led.

#define LED_Pin 13

void setup() {

digitalWrite(LED_Pin,HIGH);

pinMode(LED_Pin,OUTPUT);

}

void loop() {
 
if (digitalRead(LED_Pin == HIGH)) {
  digitalWrite(LED_Pin,LOW);
  delay(200);
}
if (digitalRead(LED_Pin == LOW)) {
  digitalWrite(LED_Pin,HIGH);
  delay(200);
}

}

I have to add a second if statement to get this to work instead of just a simple else.

Any guidance would be greatly appreciated.

You have a misplaced parenthesis.

// change this
  if (digitalRead(LED_Pin == HIGH)) {
// to this
  if (digitalRead(LED_Pin) == HIGH) {

Thank you very much, sometimes you stare at things and it makes no sense. All it takes is fresh set of eyes to see the simple mistake.

I like doing it this way

#define LED_Pin 13

void setup() {

digitalWrite(LED_Pin,HIGH);

pinMode(LED_Pin,OUTPUT);

}

void loop() {
 digitalWrite(LED_Pin,!digitalRead(LED_Pin));
 delay(400);
}

That works too, lot less typing. Thanks.

ACremers:
Thank you very much, sometimes you stare at things and it makes no sense. All it takes is fresh set of eyes to see the simple mistake.

Consider keeping a code review teddy bear on the workbench. When you have one of these kinds of things, you explain to the teddy how your code is supposed to work. Very frequently the teddy will catch errors in your code and logic.

PaulMurrayCbr:
Consider keeping a code review teddy bear on the workbench. When you have one of these kinds of things, you explain to the teddy how your code is supposed to work. Very frequently the teddy will catch errors in your code and logic.

I like that idea very much.

I do my explaining to a mythical person in a notebook or a text file. The discipline of writing/explaining coherently often shows up stupidities.

...R

Small nit to pick...

void setup() {

// set the correct pin mode
pinMode(LED_Pin,OUTPUT);

// before you use it
digitalWrite(LED_Pin,HIGH);

}

No, it's in the right order :wink: If you want it to default (aka in setup) call digitalWrite() before pinMode(). Otherwise the pin will be pulled LOW for a short amount of time. And depending on what's connected give you startup blimps.

Robin2:
I like that idea very much.

I do my explaining to a mythical person in a notebook or a text file. The discipline of writing/explaining coherently often shows up stupidities.

For me it helps to start a thread on a forum. By the time I have described the code and problem correctly, I've also figured out what is wrong and don't have to post :wink: