Hi, i'm trying to do two things. The first of which i have working. It is a single button press, lighting an LED fully and then fading off. What i would like to do now is, upon button press have the LED fade in before it fades out.
Yeah i've looked through that tutorial and many others, trouble is when i try and implement it within the 'button is pressed' part the LED just lights in small increments every time the button is pressed. I want to press the button once and have the LED do one fade on and one fade off. I thought it would be a case of making a small modification to the original code i posted. In the original code i posted (just fade off) the LED is retriggered every time the button is pressed too which i'd like to retain for the fade on+fade off
Thanks for the replies. I tried doing what you suggested but i guess i'm not doing it right as the LED still only steps incrementally brighter with each button press
#include <ezButton.h>
const byte buttonPin = 2;
const byte ledPin = 9;
byte buttonState = LOW;
boolean wasPressed = false;
int brightness = 0;
float fadeAmount = 3;
ezButton button(buttonPin);
void setup()
{
pinMode(ledPin, OUTPUT);
analogWrite(ledPin, 0);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop()
{
button.loop(); // MUST call the loop() function first
if (button.isPressed()) {
wasPressed = true;
if (wasPressed == true);
analogWrite(ledPin, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
else (wasPressed == false);
}
Got this working on my UNO. Fade sequence happens once per button press. Note a few changes/additions in the variables used.
#include <ezButton.h>
const byte buttonPin = A3; // button on A3 on my setup
const byte ledPin = 9;
byte buttonState = LOW;
boolean wasPressed = false;
int brightness = 0;
int lastBrightness;
int fadeAmount = 3; // was a float
ezButton button(buttonPin);
void setup()
{
pinMode(ledPin, OUTPUT);
analogWrite(ledPin, 0);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop()
{
button.loop(); // MUST call the loop() function first
if (button.isPressed()) { // set starting conditions
// this code executed once at beginning of fade sequence
wasPressed = true;
lastBrightness = 0;
brightness = 0;
fadeAmount = 3;
}
if (wasPressed == true) {
analogWrite(ledPin, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount = fadeAmount * -1;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
if (brightness <= 0 ) { // time to end the sequence?
wasPressed = false;
}
lastBrightness = brightness;
}
Thank you so much for that. I will try it out tomorrow as soon as I can. So I needed to track the brightness and reset that at the end of the loop? Now I just need to hook up a pot to control the fade time but hopefully that shouldn't be too tricky.
Works very well, thank you so much. I did have to analogWrite the LED to 0 at the end to get it to turn off properly, i assume there's no issue with that.
I need to hook up a pot to control the length of these fade in/outs but there doesn't seem to be a very satisfying way of adjusting the timing. Changing the delay affects the response of the button and changing the fadeAmount makes it get a bit glitchy. Is there anything you would suggest for controlling the time of fades?
Thanks again for your help
When i change the fadeAmount to make the fade in/out slower or faster there is a slight blink at the end of the fade in, before the fade out. I'm guessing this is when the code hits the delay and it's less noticable when i reduce the delay time down to 5 or so. I'm thinking i need to replace that delay with the millis() function but i've spent all day trying to work it out and have got nowhere.
I got this far then gave up coz i knew it was screwed up pretty bad
// LED fade on then fade off
// button pin is pin 2
#include <ezButton.h>
const byte buttonPin = 2; // button on A3 on my setup
const byte ledPin = 9;
byte buttonState = LOW;
boolean wasPressed = false;
int brightness = 0;
int lastBrightness;
int fadeAmount = 3; // was a float
int potVal;
int potPin = A0;
int dt = 15;
unsigned long previousMillis = 0;
const long interval = 1000;
ezButton button(buttonPin);
void setup()
{
pinMode(ledPin, OUTPUT);
analogWrite(ledPin, 0);
pinMode(buttonPin, INPUT_PULLUP);
pinMode (potPin, INPUT);
}
void loop()
{
unsigned long currentMillis = millis();
button.loop(); // MUST call the loop() function first
if (button.isPressed()) { // set starting conditions
// this code executed once at beginning of fade sequence
wasPressed = true;
lastBrightness = 0;
brightness = 0;
fadeAmount = 4;
}
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
if (wasPressed == true) {
analogWrite(ledPin, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount = fadeAmount * -1;
}
}
if (brightness <= 0 ) { // time to end the sequence?
wasPressed = false;
analogWrite (ledPin, 0);
}
lastBrightness = brightness;
}
}
Any pointers would be much appreciated. I've read loads about millis() but still haven't work out how to do it on my own
Thank you that worked very nicely. I still feel like i may need to get rid of the delay at some point though. Whilst the smooth fade works very nicely i also want to be able to fade up the LED in steps so it increments in steps of brightness, maybe about 10 steps from off to bright to back off. Whilst i can achieve this by adjusting the delay time and the fadeAmount i have to set the delay time at about 250ms which affects the switch. Given that this is going to be triggered by something else and won't actually be a button it needs to respond to fast triggers that probably don't last as long as 250ms.
Would replacing the delay function with millis() resolve this problem?