Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8533083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:52:29+00:00 2026-06-11T09:52:29+00:00

/* Button Turns on and off a light emitting diode(LED) connected to digital pin

  • 0
    /*
  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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T09:52:31+00:00Added an answer on June 11, 2026 at 9:52 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my app i have a button that turns bluetooth on and off, something
I'd like to make my webpage highly compatible; so when a user turns off
I am developing a widget for turning on/off camera led of phone. I have
I've seen many web pages with a simple sound on /sound off button which
When downloading my app from Market, when I tap on the Open button to
I have an activity with a button start measuring: pressing it will register this
From reading the docs I can tell that allowForceBuild will turn off the force
Is there any way to turn off the monitor screen light using java code
I have a button that when you click it, it turns on a WakeLock,
How do I turn the below code into a button on my page? It

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.