Hi there! I'm trying to get an LED to start fading in and out with a button press, with another button press turning the LED off entirely. I've tried a few different avenues, but nothing's seemed to work. With my current build, the LED stays static unless you hold the button down, in which case it fades in and out. I'm not sure what to do, as it seems like any variables I use to try and trigger the "off" state also trigger the "on" state, and I can't seem to unentangle the two.
Any help would be much appreciated!
int run;
int button = 7;
int led = 3;
int brightness = 0;
int fadeAmount = 5;
void setup()
{
run = 0;
pinMode(button, INPUT_PULLUP);
pinMode(led, OUTPUT);
}
void loop()
{
if(digitalRead(button) == LOW)
{
if(run == 0)
{
run = 255;
}
else
{
run = 0;
}
}
if(run > 0)
{
analogWrite(led, brightness);
brightness = brightness + fadeAmount;
if (brightness == 0 || brightness == 250) {
fadeAmount = -fadeAmount ;
}
delay(35);
}
while(run > 0)
{
if(digitalRead(button) == LOW)
{
run = 0;
}
}
}