So this is pretty much explanatory. I am new to arduino, and i am learning the best i can. I need to sketch a code in which will cycle through 5 different brightness settings of an led. One push will cycle through the five, another push of the button will turn the led off, and another push will restart the cycle.

const int led =11;
const int BUTTON = 7;
boolean lastButton = LOW;
boolean currentButton = LOW;
int ledLevel = 0;

void setup()
boolean debounce(boolean last)
  boolean current = digitalRead(BUTTON);
  if (last != current)
  current = digitalRead(BUTTON);
return current;

void loop()
  currentButton = debounce(lastButton);
  if (lastButton == LOW && currentButton == HIGH)
    ledLevel = ledLevel + 51;
  lastButton = currentButton;
  if (ledLevel > 255) ledLevel = 0;
  analogWrite(led, ledLevel);
  analogWrite(led, ledLevel + 51);
  analogWrite(led, ledLevel + 102);
  analogWrite(led, ledLevel + 153);
  analogWrite(led, ledLevel + 204);
  analogWrite(led, ledLevel + 255);

The code above turns the led on and cycles brightness, like it should it however for some reason it does disable the button. Take the analogWrites out except for the first one, and the button will cycle the led. any suggestions on what i could do? Im very new at this.

  • When you are not sure how to continue, split the problem into smaller problems and confirm that a part is working before using it with the other parts. You can split your sketch into different parts. I can see four parts: read button with debounce, notify that a button has been pressed, process that information for the three states (brightness, turn off, restart), and set the brightness for the led. If you show clearly the four parts in your sketch, then it is easy to print a message when a button has been pressed. That makes it easier to test that part.
    – Jot
    Sep 24, 2018 at 5:44

2 Answers 2


Something like this, with variable declarations & setup() left to you. All time elements are of type unsigned long.

pwmValue {} = [51, 102, 153, 204, 255];

void loop(){
  if (digitalRead(startButton) == LOW && pwmCycling == 0){ 
    // start button pressed, and not already running
    startTime = millis(); // capture the time of the button press
    pwmCycling = 1; // keep track of the running state. 0 for off, 1 for on.
  if (pwmCycling == 1){ // cycling is running
    // this is where the blink without delay is:
    currentTime = millis(); // capture the time now
    elapsedTime = currentTime - startTime; // see how much time went by
    if (elapsedTime >= eventDuration){  // enough?  start next brightness
      currentTime = startTime; // set up for next event time
      brightNumber = brightNumber +1; // keep track of brightness level
      if (brightNumber >= 5){ brightNumber = 0; } // reset after the last level
      analogWrite (ledPin, pwmValue[brightNumber]); // write the LED pin
  if (digitalRead(startButton) == LOW && pwmCycling == 1){ 
    // start button pressed while LEDs are cycling, stop them
    pwmCycling = 0;
    analogWrite (ledPin, 0);
    brightNumner = 4; //reset the brightness level to the end
    delay (100); // little bit of dumb debounce delay
  • Ive managed to write the declarations and the setup. int pwmValue[] = {51, 102, 153, 204, 255}; const int ledPin = 13; int startButton = 2; int pwmCycling; int startTime = 0; int elapsedTime; int currentTime; int eventDuration; int brightNumber; void setup(){ pinMode(ledPin, OUTPUT); pinMode(startButton, INPUT); startButton = millis(); I am not getting an errors when compiling the sketch. However the button does not turn on the led at all. Im assuming the problem is within the setup(). what should i be looking for?
    – Steven
    Sep 24, 2018 at 14:32
  • also dont know why the 4 space thing doesnt work for me. I tried keeping the lastbutton and currentbutton lines of code, still no errors, still not working.
    – Steven
    Sep 24, 2018 at 14:43
  • changed to unsigned long and still nothing. Not sure what im missing.
    – Steven
    Sep 24, 2018 at 14:45
  • I can't do much more until I get home to try it myself.
    – CrossRoads
    Sep 24, 2018 at 14:47

All the delay(200) lines keep the sketch from reading button presses.

You need to learn about Blink Without Delay so your sketch can do things other than sit there blindly letting time pass.

  • So I am now reading up on blink without delay. If i were to remove the additional analogWrite(led, ledLevel) portions of the code, how would I implement it. First press of button will turn the led on, and cycle through the following PWMs (51, 102, 153, 204, 255) continously. Second push of the button will turn the led off no matter what PWM the led is in. while a third push of the button will restart the process. If it were to implement millis() how could i do that with analogWrite() to be able to phase through the led brightness? only examples I get is with digitalWrite.
    – Steven
    Sep 24, 2018 at 3:53
  • What you are doing is checking how much time is elapsed, and when enough has gone by you do another something. Just like watching a clock on the wall when baking a cake. Capture the time now to start, then capture the time every pass thru loop() after that, and see if enough time has elapsed to do something.
    – CrossRoads
    Sep 24, 2018 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.