One button turning light on and off

Hi I have been trying to get one button turn a LED on and off. i.e. Press once, LED turns on, press again, LED turns off. i have been trying using if statements with no luck Any help would be greatly appreciated Thanks

Post the code you've been trying and we can help, otherwise it look like we'll be writing it for you, and that would be no fun for you.

Read the post at the top of this page.

int ledPin = 13; 
int inputPin = 2; 
int buttonState = 0; 
int LedOn = 0;

void setup() {
pinMode(ledPin, OUTPUT); 
pinMode(inputPin, INPUT); 
}

void loop(){

LedOn = digitalRead(ledPin);
buttonState = digitalRead(inputPin); 

if ((buttonState == HIGH) && (LedOn == HIGH)) {
   digitalWrite(ledPin, LOW); 
} else {
   if (buttonState == HIGH) {
      digitalWrite(ledPin, HIGH); 
   }            
}

// delay(1000);  

}

That's the code I was trying to use Thanks

Consider:

  1. loop() executes over and over again, really fast, perhaps several thousand times per second.
  2. The act of pressing and releasing a button will span several iterations of loop().

Therefore, when the button is pressed, the code will change the state of the LED, but then loop() immediately executes again, and it will change it back because the button is still pressed. This will happen again and again until the button is released, and then it's a toss up whether the LED will end up matching its original state or not. Note that if the button is held down, the LED glows at partial brightness. It's actually being turned on and off very rapidly.

You were right to comment out delay(), that has no place here.

Additionally, buttons need to be debounced, are you familiar with that concept?

Finally, and this is mostly a matter of style, replacing else {if...} with "else if" makes things easier to read and eliminates a set of braces. This code is equivalent:

int ledPin = 13; 
int inputPin = 2; 
int buttonState = 0; 
int LedOn = 0;

void setup()
{
    pinMode(ledPin, OUTPUT); 
    pinMode(inputPin, INPUT); 
}

void loop()
{
    LedOn = digitalRead(ledPin);
    buttonState = digitalRead(inputPin); 

    if ((buttonState == HIGH) && (LedOn == HIGH)) {
        digitalWrite(ledPin, LOW); 
    } 
    else if (buttonState == HIGH) {
        digitalWrite(ledPin, HIGH); 
    }
}

Instead of detecting if the button is down or up, you need to look for a transition. That is, keep track of the last state you read, if It's different than the current state, a transition occurred. Determine which transition (HIGH to LOW OR lOW to HIGH) and change your LED state.

Simple toggle code that you might modify for your light control.

//zoomkat servo button toggle test 4-28-2012

#include <Servo.h>
int button = 5; //button pin, connect to ground to move servo
int press = 0;
Servo servo;
boolean toggle = true;

void setup()
{
  pinMode(button, INPUT); //arduino monitor pin state
  servo.attach(7); //pin for servo control signal
  digitalWrite(5, HIGH); //enable pullups to make pin high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      servo.write(160);
      toggle = !toggle;
    }
    else
    {
      servo.write(20);
      toggle = !toggle;
    }
  }
  delay(500);  //delay for debounce
}