I made a circuit which in my intentions would allow me to toggle a LED dimming loop.
Problem is that once I push the button the first time pushing it a second time doesn't toggle the LED loop off.
Here is the code:
const int LED = 9; // the pin for the LED
const int BUTTON = 7;
int val = LOW;
int old_val = LOW;
int state = 0;
int i = 0;
void setup()
{
pinMode(LED, OUTPUT);
pinMode(BUTTON, INPUT);
}
void loop()
{
val = digitalRead(BUTTON);
if ((val == HIGH) && (old_val==LOW))
{
state = 1 - state;
delay(10);
}
old_val = val;
if (state == 1)
{
for (i = 0; i < 255; i++) // loop from 0 to 254 (fade in)
{
analogWrite(LED, i); // set the LED brightness
delay(10); // Wait 10ms because analogWrite
// is instantaneous and we would
// not see any change
}
for (i = 255; i > 0; i--) // loop from 255 to 1 (fade out)
{
analogWrite(LED, i); // set the LED brightness
delay(10); // Wait 10ms
}
} else
{
analogWrite(LED, 0);
}
if ((val == LOW) && (old_val == HIGH))
{
delay(10);
}
}
Here is the circuit scheme:
circuit schematics
I would be interested both in learning what the problem is AND learning any mental methods I can apply to debug in such situations.
-
Is the 10kohm resistor really connected to A5, A4, A3, A2, A1, A0, VIN, and GND?Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2014年12月14日 09:09:14 +00:00Commented Dec 14, 2014 at 9:09
-
No, only to GND1. It is a problem with the rendering in 123d circuits.Fabrizio Bianchi– Fabrizio Bianchi2014年12月14日 09:11:59 +00:00Commented Dec 14, 2014 at 9:11
-
Then you should consider fixing your wiring diagram.Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2014年12月14日 09:12:38 +00:00Commented Dec 14, 2014 at 9:12
-
There! I fixed it.Fabrizio Bianchi– Fabrizio Bianchi2014年12月14日 09:22:24 +00:00Commented Dec 14, 2014 at 9:22
-
While the led is fading in and out (which takes 5 seconds), the state of the button is never checked. So you'd need to have the button released for at least 5 seconds (so old_val becomes LOW), and then pressed exactly we the led is faded out (again, 5 seconds later). So try keeping the button pressed for at least 5 seconds. PS you probably want to rewrite your code, so the button is more 'responsive'.Gerben– Gerben2014年12月14日 14:32:50 +00:00Commented Dec 14, 2014 at 14:32
1 Answer 1
While the led is fading in and out (which takes 5 seconds), the state of the button is never checked. So you'd need to have the button released for at least 5 seconds (so old_val becomes LOW), and then pressed exactly we the led is faded out (again, 5 seconds later). So try keeping the button pressed for at least 5 seconds.
You probably want to rewrite your code, so the button is more 'responsive'. Below I added an example of how to do this.
const int LED = 9; // the pin for the LED
const int BUTTON = 7;
int val = LOW;
int old_val = LOW;
bool fadeLed = false;
int brightness = 0;//current pwm value
int direction = 1;// 1 is brightness is increasing, -1 if brightness is decreasing
void setup()
{
pinMode(LED, OUTPUT);
pinMode(BUTTON, INPUT);
}
void loop()
{
/* check the button */
val = digitalRead(BUTTON);
if ((val == HIGH) && (old_val==LOW))
{
// button pressed ( called only once per press )
fadeLed = !fadeLed;// invert boolean
}
old_val = val;
/* update the leds brightness */
if( fadeLed )
{
// fade led in and out
if( brightness==0 )
direction = 1;//change direction is minimum value reached
if( brightness==255 )
direction = -1;//change direction is maximum value reached
brightness += direction;// increase/decrease brightness
}
else
{
// fadeLed is disabled, so only decrease brightness till it reaches zero
if( brightness>0 )
brightness--;
}
analogWrite(LED, brightness); // set the LED brightness
delay(10); // Wait 10ms because analogWrite
// is instantaneous and we would
// not see any change
}
Any questions about the code; just let me know.