How to control an LED with a button?

Post content lost due to vandalism by author

Everytime you recognize a button press, do this:

ledState = 1 - ledState; // before setup, have this: byte ledState; result is 1,0,1,0,1,0,
digitalWrite (ledPin, ledState);

Don't forget a current limit resistor with the LED.
"The forward voltage of the LED is about 2.2V so connect a 220 to 1000 ohm resistor in series just as you would with any other LED to your 3V or higher power supply."

(5V - 2.2V)/220 ohm = 12.7mA

So add this too:
byte ledPin = 10; // or whatever

In setup(), add this:
pinMode (ledPin, OUTPUT);

SomewhatCompetent:
As long as the positive is connected to 5V and the negative is connected to ground it is just constantly lit.

To control something with an arduino it needs to be wired to it no? Just pretend that the button and LED are two separate components. Program accordingly.

The button was connected to ground and pin 10.

With this wiring, the inPin should be declared as INPUT_PULLUP. This activate the internal pullup resistor on the input and prevents it from floating. The inPin will read HIGH if the button is not pressed, and LOW if pressed.

The logic in you sketch matches the comments, but for that to work

 pinMode(inPin, INPUT_PULLUP);    // declare pushbutton as input

This diagram makes no sense for the pins in your code or your statement that

The button was connected to ground and pin 10.

The combo led/button should have four wires. Be sure you know which two are for the button, and which two are for the led.

One wire from the button wants to be attached to ground. The other wire wants to go to pin 8 which is inPin declared as INPUT_PULLUP.

It's not clear to me if the led in the led/button combination has an internal resistor for the led, but it might not, and you should have at least 220 ohm in series with the led.

Please attach a sketch of your actual setup.

The lights on the board still go out when I push the button. Is this normal?

No. The led needs a resistor. you can not just stick the two legs into the pin 13 header and ground. It is drawing too much current and can damage itself or pin 13.

The button is also not wired correctly. One wire from the button goes directly to ground. The other wire of the button goes directly to pin 7. Pin 7 is in pinMode INPUT_PULLUP.

Nothing goes to 5V?

Correct.

So one leg of the LED goes to ground and the other goes through a resistor to Pin 13.

There is a positive (anode) and negative (cathode) lead to the led. The positive goes to 13, and the negative to ground. The resistor can be on either side. If the led lights up you've got it correct.

The LED is also very dim, which I assume is due to the resistor being a little too beefy for it.

Your code is are toggling the led on/off very fast each time the loop runs, and you won't see full brightness. You might also move the led to a different pin than 13, because it is being shared with the on board led.

First, clean up the code to the minimum to turn the led on and off. If the led is bright enough, you're good. Otherwise try a different pin. If still not bright enough, try a smaller resistor.

int inPin = 7;
byte ledPin = 13; // choose the pin for the LED

void setup() {
  pinMode(inPin, INPUT_PULLUP); // declare pushbutton as input
  pinMode(ledPin, OUTPUT);  // declare LED as output
}

void loop() {
  if (digitalRead(inPin) == LOW) //funcitons based off of button pulling input pin LOW
  {
    digitalWrite(ledPin, HIGH);  // turn LED ON
  }
  else
  {
    digitalWrite(ledPin, LOW);  // turn LED OFF
  }
}

I would prefer it if it came on and stayed on when pushed, and when pushed again it turned off and stayed off until pushed again.

OK. You need to detect if the button was pressed, not if it is pressed. This is covered in the ide example 02Digital > StateChangeDetection. You will need to modify that example for INPUT_PULLUP and the logic of LOW being pressed.

There is a indepth analysis of that example here

I will also have a go at modifying the StateChangeDetection example to see if I can get it to work similar to the code I included in edit 2 above.

Using Button.h takes care of the state change situation. There is no need to change anything if you are happy with the library and the code in edit 2 is working as you intend. If the led is dim, reduce the resistor to 220 ohms,