help on basics needed(digital read)

Hi, I checked a Button example and wrote 2 pieces of the simplest instructions for my Arduino UNO(328):

int buttonState = 0; void setup() { pinMode(13, OUTPUT); pinMode(2, INPUT); } void loop() { buttonState = digitalRead(2); digitalWrite(13, !buttonState); and second piece is "digitalWrite(13, buttonState);" } When I run the first example I get LED on , it is switching off as soon as I press the button. After releasing it- the LED quickly blinks 4 times(?), and then is off for a half a second (?) and finally switches on.(which is what I would roughly accept)

When I do the second example, the LED is initially off, when I press the button is still off (why!?), when I release the button it quickly blinks 4 times(?) and gets off.

I do not understand it - it should switch on, as soon as I am pressing the button, not mentioning the blinking, which should be present, however not so slow and noticeable.

Hi, I checked a Button example

Which one?

You are not enabling the internal pullup resistor, so you need an external pullup or pulldown resistor. If you have no clue what that means, you've got some research to do.

void loop() {
  buttonState = digitalRead(2);
  digitalWrite(13, !buttonState);  and second piece is "digitalWrite(13, buttonState);"
}

I'm pretty sure the compiler will reject this code.

You need to understand (more research, perhaps) switch debouncing and floating pins.

PaulS: I'm pretty sure the compiler will reject this code.

It ain't - good compiler :-)

It is always better to read what the button being pushed reads ("reset") XD

After proper wiring -all works perfect.

Thanks for the prompt response

I think it should work if its not working then double check your connections of push button as in most cases its loose wiring makes problems..

P000: When I do the second example, the LED is initially off, when I press the button is still off (why!?), when I release the button it quickly blinks 4 times(?) and gets off.

I do not understand it - it should switch on, as soon as I am pressing the button, not mentioning the blinking, which should be present, however not so slow and noticeable.

The blinking is a result of two things 1) The button is floating and 2) bouncing. Paul addressed floating by suggesting you use the internal pull-up resistors (unless you are using external resistors.)

If flashing still occurs after using pull-ups, you need to look into how to "de-bounce" a switch. (You can do it in software or with a small capacitor.)