Simple click switch operation

Hi

There are a few issues that jump out from this. Firstly the switch, when activated, pulls the line connected to pin 5 to ground however there's nothing to force it to the alternate state when it's off. One way to do this is to use a pull-up resistor. That would look like a 10,000 Ohm (10kR) resistor between the leg of the switch that connects to D5, and the +5V rail which would make the "default" forced to 5V or HIGH when you use digitalRead() on that pin. A very convenient way to do that is by activating the internal pull-up resistor that's a feature of the Arduino's processor. To do that, after setting the pin to OUTPUT as you have in setup(), follow it up with the line digitalWrite(pushButton, HIGH);. For a full description of that feature, check this page.

So that will give you a solid result to your digitalReads every time. But be aware, buttonState will only be 1 when your switch is off (ie it's pulled high). So swap the logic around or it will be backwards as you're seeing.

Next, be sure to use a current limiting resistor on your LED or bad things can happen to it. A good explanation of that is available from this sparkfun page and many other places elsewhere when you google the topic.

To find all of these things in one place, checkout the Arduino tutorial series by Jeremy Blum on youtube.

All the best,
Geoff