I got a button with 2 legs in my arduino starter kit and am not quite sure how to use it. As of now I've tried connecting one leg to gnd and the other to a digital output with pullup enabled
Pin 2 is set as an INPUT, not as an OUTPUT. Maybe slow yourself down a bit and label a drawing?
Pin 2 is set as INPUT HIGH. It supplies a weak 5V current and it can be read which makes it an INPUT.
INPUT_PULLUP is safe to ground without any extra resistance, it has effective 20+K on-chip.
void setup() should have Serial.begin( 115200 ) and your Serial Monitor baud rate set to 115200 and add newline to lines.
Why? The quicker Serial runs the sooner that the message chars -all- get sent. When your code executes a print statement the text is put in the Serial output buffer and the sketch keeps running. Serial sees text in the buffer and prints chars out one at a time until the buffer is empty.
Arduino can overfill the buffer quicker than a single serial char is finished sending ( ~89 micros at 115200 baud) and when the buffer is full a char has to print before another can be added --and nothing else can run until the blocked print is done.
So take care to not print more/faster than there's time to clear at serial speed and be real happy Serial can run faster than 9600.
If we presume you are using a breadboard, you should first use a test program which in loop() reads the button state and writes it to the pin 13 LED to check that you have actually connected it.
GoForSmoke:
Pin 2 is set as an INPUT, not as an OUTPUT. Maybe slow yourself down a bit and label a drawing?
Pin 2 is set as INPUT HIGH. It supplies a weak 5V current and it can be read which makes it an INPUT.
INPUT_PULLUP is safe to ground without any extra resistance, it has effective 20+K on-chip.
void setup() {
pinMode(2, INPUT_PULLUP);
}
void loop() {
int buttonState = digitalRead( 2 );
if (buttonState == LOW) {
Serial.print(1);
}
}
void setup() should have Serial.begin( 115200 ) and your Serial Monitor baud rate set to 115200 and add newline to lines.
Why? The quicker Serial runs the sooner that the message chars -all- get sent. When your code executes a print statement the text is put in the Serial output buffer and the sketch keeps running. Serial sees text in the buffer and prints chars out one at a time until the buffer is empty.
Arduino can overfill the buffer quicker than a single serial char is finished sending ( ~89 micros at 115200 baud) and when the buffer is full a char has to print before another can be added --and nothing else can run until the blocked print is done.
So take care to not print more/faster than there's time to clear at serial speed and be real happy Serial can run faster than 9600.
Printing is all we've got to debug/trace with on AVR-duinos.
You can print timestamps for events, printed in hex they take 8 chars and no decimal conversion = much faster if you read hex. A hex calculator app on your PC makes it easy to subtract start time from end time to find the time between stamped/logged events.
Don't use millis() for close timing if +/-1 matters, use micros() instead.
Use unsigned integers for time and unsigned subtraction handles rollovers same as not; end - start = elapsed, must be >= 0.
If the hour hand on a clock points to 4 and I move the hand CCW 7 hours it points to 9. end=4, start=7, interval=9, all >= 0.
If you press the button once and you see multiple press/release events, see how close those changes happen with timestamps.