Toggle Pushbutton To Turn on and off PWM Program

Hey guys, below is my code. What im trying to to is have a single pushbutton start my pwm output. It will go through different brightness level with just clicking the pushbutton one time. If i press the pushbutton again it will stop going through the brightness levels and turn off. It i press the pushbutton again it will start the brightness level cycles again. I feel like im kind of close to accomplishing this but I cant figure out how to make it stop when I click the pushbutton again.

int switchPin = 8;
int ledPin = 11;
boolean lastButton = LOW;
boolean currentButton = LOW;
int ledLevel = 0;

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

boolean debounce(boolean last)
{
 boolean current = digitalRead(switchPin);
 if (last != current)
 {
   delay(5);
   current = digitalRead(switchPin);
 }
 return current;
}

void loop()
{
 currentButton = debounce(lastButton);
 while (lastButton == LOW && currentButton == HIGH)
 {
   ledLevel = ledLevel + 51;
   analogWrite(ledPin, ledLevel);
   delay(1000);
   ledLevel = ledLevel + 51;
   analogWrite(ledPin, ledLevel);
   delay(1000);
   ledLevel = ledLevel + 51;
   analogWrite(ledPin, ledLevel);
   delay(1000);
   ledLevel = ledLevel + 51;
   analogWrite(ledPin, ledLevel);
   delay(1000);
   ledLevel = ledLevel + 51;
   analogWrite(ledPin, ledLevel);
   delay(1000);
   ledLevel = ledLevel + 51;
 }
 
 if (ledLevel > 255) ledLevel = 0;
 analogWrite(ledPin, ledLevel);
 delay(1000);

 if (currentButton == HIGH && lastButton == LOW) ledLevel = 0;
 delay (5);
 analogWrite(ledPin, ledLevel);

}

Please edit your code to use code tags. See How to use the forum.

The main problem is you never set lastButton so the while can never exit. Both lastButton and currentButton are not updated in the while loop.

But even if it did it would work terrible. As always, the delay() is the trouble. NOTHING happens during the delay. Not even button reads.

I would suggest to switch to the Bounce2 library for the button and see Blink without delay for the timing :slight_smile:

Also, passing global variables is a bit of a wast :wink:

Ok I added the code tags. Thank you for the help so far. Where do i need to set my lastButton and currentButton at so that it stops the while loop if the button is pressed again?

Normally you do that after comparing them so you can compare them again in the new loop. But you need to add a variable to remember you're looping or not instead of using the button states directly.

But, remember what I said:

  1. Make life easy and grab Bounce2
  2. Get rid of all the delays() because they will mess with reading the button :wink: With the delays it will never work good.

Sorry, I wish I fully understood what you are talking about. I'm very new to this. Do you have an example or a edit my code so I can understand what you a talking about with a variable to remember my looping?

Thank You

Forget that the delay() function even exists.
Study these:

There is a change in state detection example in the IDE, review how it works.

twobbster:
Do you have an example

septillion:
see Blink without delay for the timing :slight_smile:

And as a;; ypu need for the button: Bounce2