I changed things a bit and got a solution
int led = 3;
int buttonpin = 8;
boolean waspressed = false;
int inertia = 10;
void setup (){
pinMode(buttonpin,INPUT);
pinMode(led,OUTPUT);
analogWrite(led,0);
}
void loop(){
if(digitalRead(buttonpin) == HIGH){
waspressed = true;
}else{
if (waspressed == true){
fadeLed(digitalRead(led), inertia);
waspressed = false;
}
}
}
void fadeLed(boolean input, int inertia){
for(int state=0;state<256;state++){
if (input==LOW){
analogWrite(led, state);
}else{
analogWrite(led, 255-state);
}
delay(inertia);
}
}
I created a function called fadeLed
that receives two arguments: The actual state of the led and the delay - in miliseconds - that is going to determine how long the fading is going to last.
You can see that the rest of the code is pretty much the one you got in the other question you asked. This is because we already got a good code to control the behavior of the button, so its a good practice to keep it and just change what you want to be changed: to fade instead of blink. So we just change a single line and create a function to be called by that line. Functions are a good way to keep a clean code and are a good programming practice. They make the code easy to read and maintain.
The function fadeLed()
uses PWM to fade the led. In Arduino Uno, PWM is available in digital pins 3, 5, 6, 9, 10, and 11. This may change according to the model you are using. It's very important to understand PWM so you can know why analogWrite()
is used in a digital pin.
If you check the documentation of analogWrite(), you are going to see that for most of the Arduino boards the default resolution is 8 bits, i.e, 0 to 255. To use all this 256 values to fade the led, a for
loop is used. If you are not used to for loops, I strongly recommend you to read the documentation.
The for loop is programed to go through 256 iterations - the variable state
going from 0 to 255. At each iteration, the value of state
is incremented by 1. When state
gets to be equal to 256, the loop ends itself. Every iteration verifies the value of input
, which is just the state of the led when the button was released. This value is passed to the function only when the button is released, but the function uses it internally at every iteration. If input
is LOW
, it means that the led was off, so analogWrite(led, state);
uses the value of state
- increasing at every iteration - to fade the led, turning it on. If input
is HIGH
, it means that the led was on, so analogWrite(led, 255-state);
will fade the led turning it off. In this case, 255-state
is just a way to set a range starting at 255 and ending at 0 while using a range that we already have - state
- that starts at 0 and ends at 255.
The last line is just to set how long the fade action is going to last. To be more specific, delay(inertia)
determine how long each iteration is going to last. So, when you set inertia to 10, will you see that fading will take 10(miliseconds)*255(iterations) + execution times =~ 2,6 seconds. I chose this name when I was coding and I'm not sure now if it was the better one. Maybe something like duration will have a more clear meaning. If you want to set the duration of the fade on your code - instead of the duration of each iteration - you can to something like
int fadeDuration = 3000; //duration of fade in ms
...
void fadeLed(boolean input, int inertia){
for(int state=0;state<256;state++){
if (input==LOW){
analogWrite(led, state);
}else{
analogWrite(led, 255-state);
}
// delay in every iteration to achieve 3 seconds in total.
delay(fadeDuration/256); //=(total fading duration)/(number of iterations)
}
}
Hope this is clear. Don't forget to check the links I placed, they are all VERY important.
digitalWrite (buttonstate,HIGH);
- What is that supposed to achieve?! – Majenko♦ Mar 22 '16 at 16:44int fadeamount = 51;
- That's a huge amount to fade by... – Majenko♦ Mar 22 '16 at 16:45digitalWrite (buttonstate,HIGH);
i used it to let the button state as high even after i leave the pushbutton so that the LED doesn't get off !! – MrDeepThought Mar 22 '16 at 16:52i used it to let the button state as high even after i leave the pushbutton so that the LED doesn't get off !!
well it doesn't. I don't know how you thought that might work. You're actually writing HIGH to either pin 0 or pin 1. – Majenko♦ Mar 22 '16 at 16:53