The problem comes from:
void loop() {
ledState = digitalRead(ledPin);
if (ledState == HIGH){
Serial.println("on");
}
else if (ledState == LOW );{
Serial.println("off");
}
{
digitalWrite(ledPin,LOW); // set the LED off
delay(1000); // wait for a second
digitalWrite(ledPin, HIGH); // set the LED on
delay(1000); // wait for a second
}
}
If you go through the program, it first checks the state of ledPin, and prints the value, high or low.
You then set ledPin low, wait, set ledPin high, and then wait again.
The problem comes because you are setting ledPin low then high each time. This means that each time the loop starts after the first iteration, ledPin will always be high (as that was the last state setting).
What you need to do is something like:
void loop() {
ledState = digitalRead(ledPin);
if (ledState == HIGH){
Serial.println("on");
digitalWrite(ledPin,LOW);
} else {
Serial.println("off");
digitalWrite(ledPin,HIGH);
}
delay(1000);
}
This only toggles the state of ledPin once per loop, so each time round the loop it will have a different state to the previous iteration.