Why is writing HIGH and LOW to a pin not working?

So I have just an led and an Resistor connected to Pin 2 and my code is this:

void setup() {
  pinMode(2, OUTPUT);
}

void loop() {
digitalWrite(2, HIGH);
delay(500);
digitalWrite(2, LOW);
}

However the Pin just stays in either HIGH or LOW depending on which came first in the code (I switched it around a few times). But this code works, so I am wondering, why just why

  digitalWrite(2, !digitalRead(2));
  delay(500);

Try:


void loop() {
digitalWrite(2, HIGH);
delay(500);
digitalWrite(2, LOW);
delay(500);
}

1 Like

You don't have a delay after you set the pin the second time. It will briefly go to that state and then back to the original state.

1 Like

oh my god I am so stupid, thank you very much for answering my dumb question...

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