Hey guys, I just got a cheap Chinese Pro Micro for Christmas, and I was messing around trying to make a button push turn on a LED. I believe I set it up correctly, but I will attach a picture of the Code and Errors.
I hooked up an LED to pin 2 and ground, and a button from RAW to Pin 4.
I don't see anything wrong with my code,
but I am not very experienced! Thanks a bunch for anyone who responds!
In programming syntax(punctuation) is everything.
You're missing a couple of curly braces {}
In the arduino IDE program if you click EDIT>>>Copy for Forum
you can paste the code with code tags directly into you're post.
that way it makes it easy for us to check it.
Also the usual way to use a button is to wire it between the arduino pin and ground.
Then you can use INPUT_PULLUP to enable the built in pullup resistor to keep your pin from "floating" which means that it will float back and forth between reading HIGH and LOW when not pressed.
You could also use an external resistor for this.
When doing it this way the button connects to ground when pressed so the logic is reversed.
// So this:
if (digitalRead (4) == HIGH)
// Needs to be changed to:
if (digitalRead (4) == LOW)
When you push a mechanical button, it may make and break several times before settling down in ths closed state. All in a millisecond or so. The Arduino can run fast enough to read these excursions as separate presses, so your button push may have unpredictable results. Declare a variable, say
unsigned long later;
and set it equal to 0 in setup();
Then do your button press as follows
if ((digitalRead(button) == HIGH) && millis() > later))
{
... //service button request
later = millis() + 250; //adjustable -- this give a quarter sec. timeout
}
once the button is pressed, later is set forward, and the button reading conditional will not run again until 0.25 sec. has passed.