Arduino not following the program

I am new to Arduino and I am trying execute the following code:

int switchPin = 8;
int ledPin=12;

void setup() {
  pinMode(switchPin, INPUT);
  pinMode(ledPin, OUTPUT);
}

void loop() {
 if (digitalRead(switchPin) == HIGH)
  {
    digitalWrite(ledPin, HIGH);
  }
  else 
  {
    digitalWrite(ledPin, LOW); 
  } 
}

When the switch is on, the led is supposed to be on and when the switch is off,the led is supposed to turn off.
It complies and uploads correctly, and the built-in LED blinks, indicating that the program was sent to the Arduino. But the LED on pin 12 doesn't seem to be following what the switch does at all. It's just blinking randomly. Could anyone please explain what is going on?

How is the switch wired to your Arduino board?

You more than likely have a floating input. If the switch is open, the wire to the Arduino floats and picks up noise from the environment; this translates to random HIGH or LOW.

That's why @pert asks how your switch is wired.

It may depend if you are using the BUILT IN led as some ARduinos use pin 13 and NOT pin 12 unless you wired a seperate LED to pin 12 ?

Could you take a few moments to Learn How To Use The Forum.
It will help you get the best out of the forum in the future.
Other general help and troubleshooting advice can be found here.

Arduino not following the program

Of course we don't believe THAT!* :wink:

One good troubleshooting trick is to send variable-values or little messages to the serial monitor so you can "see" what your hardware & software is actually doing. For example, you can send a message that says "button pressed" and another message that says "turning-on LED", etc. The [u]Analog Read Serial Example[/u] shows you how to send messages to the serial monitor.

I almost always add these kind of messages while I'm developing the code. I don't wait until I'm troubleshooting. If you want to, or if you need to make your loop run faster, you can "comment-out" those serial transmissions when your code is done. Or there is a way to make a "compiler switch" so you can fairly-easily compile your normal code or the "debug version", but I've never done that.

  • It can happen if you have a power supply glitch that resets the processor or something like that, but It's usually a software or hardware error and the processor is following the instructions exactly as written, even if you're not getting the result you expected.

Must be a floating input problem:

Change

  pinMode(switchPin, INPUT);

to

  pinMode(switchPin, INPUT_PULLUP);  // enable internal pull-ups

and wire the switch between the pin and ground.

And change

 if (digitalRead(switchPin) == HIGH)

to

 if (! digitalRead(switchPin))

so that the active-low switch pin is correctly interpreted by the code.