[SORTED] - Weird board symptoms with basic sketches

I picked up Arduino again after probably a year. Fun to be back, but I'm getting some weird symptoms now that I cannot explain which makes me think I damaged my board, although a friends board does the same.

I was trying to do a LED on with momentary push button (push button, LED on - stays on, push button, LED off - stays off). I couldn't figure out the code so I went looking for some code to learn. Found some, wrote my own version, LED stays on when I load the sketch. Push button, nothing changes - some flickers when I push the button. Removed button, LED on again - then off. Noticed I could just touch the wire going to the pinButton (reading state of the pin - pin goes HIGH button is pressed).

Then started to worry the board might be damaged. Loaded the blink sketch. Works fine. Changed the pin to output to say pin 10, wired up an LED. Plug board into USB (for power) - works fine, LED blinks. Tried the same with the FADE sketch - works perfect.

Then loaded the sketch called "Button" in the example menu under Digital. Wired up (three times in a row) board - loaded sketch - LED stays on. Remove the 5V wire going to the switch - LED stays on. Checked the code -else statement puts ledPin to LOW.

Wired up a pull-up and a pull-down switch to check - same result. Tried a different input pin, same result. Seems as soon as I use digitalWrite on any pin, it starts acting weird.

I'm using IDE 1.0 - am I missing something which I should know that changed from 0.18 to 1.0? Running Windows 7 Pro 64bit. Loaded the driver according to installation file. Its a seeeduino V2.21 board (ATmega328)

Could I have somehow (due to ignorance after a year absence) have fried just enough of my board to have it act this way? And then I suppose I friend my friends' board as well to have the same symptoms - well it had the same symptoms with the one sketch I ran which I copied from a member's post that does exactly what I wanted to try and accomplish - using a momentary switch, turn on an LED when I press and let go of the switch, and turn off the LED when I press and let go of the switch again.

I'm stumped. Could very high humidity have anything to do with the current state of things? It rained (BTW I am working in the house, not in the rain :grin: ) and the last few days we have been having high humidity levels.

Thanks for the good or not so good news.

EDIT: Whilst making this post I had the board disconnected from the USB (from my Linux laptop just to check if it was Windows but same issue). I inserted the USB on the board. Hooked up the LED one wire at a time - the LED is off. Hooked up only the wires for the switch but. LED is OFF. Touched the wires to act as a switch - LED ON. Cool. Touched wires again - LED OFF. Cool. Touched wires again - LED ON. Cool. Again - now LED stays on no matter how many time I touch the wires. I left it connected with the LED on whilst posting the "EDIT" of this post, turned around just now and the LED was OFF. Tried to put the switch in the breadboard to see if it'll switch on the LED and the LED came on by itself. Could this be humidity? I don't want to plant the seed of cause by saying that, but my thinking is if you fry your board its pretty much fried and useless, not perfectly useless in some cases and perfectly working in others.

EDIT2: I also notice that I seem to be one massive ball of static even though I am sitting barefoot on sandstone. If I have a wire plugged into pin 7 (reading pin 7 as input) and I just close my fingers around the wire without even touching it, I can make the onboard (pin 13) LED go ON and OFF. Maybe I'm like the Incredible Hulks' dad, or was it brother where he turned into lightning or electricity or something.

What you lack are either pull-up or pull-down resistors, depending on how you connect the switch. Just wiring a momentary switch to 5V and an input won't work.

madworm:
What you lack are either pull-up or pull-down resistors, depending on how you connect the switch. Just wiring a momentary switch to 5V and an input won't work.

I tried that, but for good measure I will do it again tomorrow to double check. Thanks madworm.

The simplest way to use a push button is to wire it from the pin to GND, and enable the on-chip pull-up resistors thus:

#define switch_pin 10

void setup ()
{
  pinMode (switch_pin, INPUT) ;  // ensure is input
  digitalWrite (switch_pin, HIGH) ; // enable weak pullups
}

void loop ()
{
  if (digitalRead (switch_pin) == LOW)
  {  // get here if button pressed.
    ...
  }
}

If you wire external pull-up then you need the pin via pull-up to +5V, pin via switch to GND. The pin will be normally HIGH

If you wire external pull-down then pin via pull-down to GND, pin via switch to +5V. The pin will be normally LOW.

@MarkT. Looking at your code (thank you) something caught my eye, this bit: You setup the switch pin to be an INPUT, in addition you set the pin as HIGH. You noted "enable weak pullups". This is sort-off something I was wondering about yesterday, if the pin is set to read inputs, should one, for the lack of a better description, set the pin either low or high depending on your switch? Meaning, if I digitalRead the state to pickup a "HIGH" signal, should I set the pin as LOW to ensure the state is low in order to eliminate false INPUT reads.

Another way of describing it is with the cliché - water and pipes. If I want to start motor when water flows past a point in a pipe, and I want the motor to only start when the pipe has flowing water as oppose to a small trickle of water, I put a pressure valve in the pipe to ensure the motor will only switch on when a set pressure is reached, opening the valve and water flows in the pipe and thus past the flow switch.

EDIT: Right it seems to be all working fine now. I suspect it is because of the two lines given by MarkT. What happens (my understanding) is you set a given pin as an INPUT. Then you monitor the state of that pin, either HIGH or LOW. This bit I think solved it: If you want the pin to read HIGH then you have to set it LOW; if you want the pin to read LOW, then you have to set it HIGH. Example: I want pin 13 to change it's state when pin 2 reads HIGH with digitalRead. However setup pin 2 to LOW using the statement: digitalWrite(buttonPin,LOW). "buttonPin" being assigned to pin 2.
This ensures the elimination of the INPUT pin reading even minute changes as HIGH or LOW.
Well that crude explanation of which some might even be incorrectly stated conceptionally within the Arduino architecture, is the gitz of it. Point is, if you want a pin to be an INPUT and you want it to read a HIGH signal, digitalWrite that INPUT pin as LOW to ensure no false HIGH readings. Did I miss that in the documentation - probably.