using a button with 2 legs

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

Here's my code

void setup() {
    pinMode(2, INPUT_PULLUP);
}

void loop() {
  int buttonState = digitalRead(2);

  if (buttonState == LOW) {
    Serial.print(1);
  }
}

However nothing prints in the serial monitor when the button is pressed

And your serial line speed is. . . ?

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.

const byte buttonPin = 2;
byte buttonState, prevState;

void setup() 
{
    Serial.begin( 115200 );
    pinMode( buttonPin, INPUT_PULLUP );
}

void loop() 
{
  buttonState = digitalRead( buttonPin );

  if ( buttonState != prevState ) 
  {
    if ( buttonState == LOW )
    {
      Serial.write( 'V' );
      Serial.write( '\n' );
    }
    else
    {
      Serial.write( '^' );
      Serial.write( '\n' );
    }
    prevState = buttonState;
  }
}

You will be able to see bounce with this. Can you fix it without using blocking code like delay()?

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. :grinning:

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.



const byte buttonPin = 2;
byte buttonState, prevState;

void setup()
{
    Serial.begin( 115200 );
    pinMode( buttonPin, INPUT_PULLUP );
}

void loop()
{
  buttonState = digitalRead( buttonPin );

if ( buttonState != prevState )
  {
    if ( buttonState == LOW )
    {
      Serial.write( 'V' );
      Serial.write( '\n' );
    }
    else
    {
      Serial.write( '^' );
      Serial.write( '\n' );
    }
    prevState = buttonState;
  }
}




You will be able to see bounce with this. Can you fix it without using blocking code like delay()?

It's working now, thank you! I've only ever coded with python before and thought that printing would be similar haha

Printing in C/C++ is like printing in Basic.

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.