David
David

Reputation: 402

How to blink an LED on a circuit using an Arduino UNO and a button?

    /*
  Button

 Turns on and off a light emitting diode(LED) connected to digital  
 pin 13, when pressing a pushbutton attached to pin 2. 


 The circuit:
 * LED attached from pin 13 to ground 
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground

 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.


 created 2005
 by DojoDave <http://www.0j0.org>
 modified 30 Aug 2011
 by Tom Igoe

 This example code is in the public domain.

 http://www.arduino.cc/en/Tutorial/Button
 */

// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int led =  11;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int buttonHistory = 0;        //Counting variable for button being pressed

void setup() {
  // initialize the LED pin as an output:
  pinMode(led, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH){
    buttonState++;
  }

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
    if (buttonHistory >= 0 && buttonState >= 0) {  
      // turn LED on:;
      int x = x + (.1*255);
      analogWrite(led, x);  

    }
    else if (buttonState == LOW){
      analogWrite(led, 0);
    }

    if (buttonState == 11){
      buttonState = 0;
    }
    buttonHistory = buttonState;
}

Some of the above code is copied from the Arduino website, but I edited it.

Above is my code. My goal here is to make an LED with a resistor on a non solder breadboard to light when I press button on that breadboard. It is all wired up, and I can get the LED to light, but not when I press the button. I want the LED to get brighter by ten percent for every time I push the button, and then when it is at max brightness, turn off on the next press. My issue is that right now, the LED is constantly on and pressing the button won't do a thing.

Upvotes: 3

Views: 5126

Answers (1)

J.A.I.L.
J.A.I.L.

Reputation: 10794

You have to connect the LED to one of the Arduino's PWM outputs.

PWM outputs can be set to values from 0 to 255, meaning that a proportional time to the value it will be setting current to that output, and the rest of the time it will be at 0 V.

Check the following example at Arduino's official site to fade an LED.

You should also use the function map, as it eases your code mapping values.

As for your code, you could try this (I haven't compiled it, so forgive any mistake):

// Read the state of the pushbutton value, and update the fade LED value:
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH){
    // buttonState++; Probably this was the main bug in your code.
    buttonHistory++;
}

// We are cycling buttonHistory, not buttonState
if (buttonHistory == 11){
    buttonHistory = 0;
}

//if (buttonHistory >= 0 && buttonState >= 0) {
  // Turn LED on at desired intensity:;
  int x = map(buttonHistory, 0, 10, 0, 255); // Similar to doing x=.1*255*buttonHistory
  analogWrite(led, x);

//}
// REDUNDANT:
//else if (buttonState == LOW){
//  analogWrite(led, 0);
//}

You should also think about adding delay() between cycles, or you'll be updating the intensity of the LED too quickly as to notice it (you'll call digitalRead(buttonPin); too many times too quickly). A good place can be after `analogWrite()' (Thanks @mike for the suggestion):

analogWrite(led, x);
delay(500);

Upvotes: 5

Related Questions