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);
}