Here you go, I found it here. Go down to BulldogLowell’s post:
One Button Dimmer
I’m using this MOSFET fromAdafruit:
IRLB8721PbF
/*
/*
* Fade.h
* by BulldogLowell (BulldogLowell@gmail.com)
* Created 17-June-2014
*
* adapted from: http://stackoverflow.com/questions/10963194/timed-fading-in-arduino/10966231#10966231
*
* Non-blocking Fade library allows you to set fade level of a pin and the fade speed
* Useful for projects involving LEDs that you use to illuminate around the house
*
* Create a fade object:
*
* fade fadePin(pin, timestep, min, max)
* timeStep is by default 10ms
* min is by default 0
* max is by default 255
*
* example
* fade fadePin(5) is the same as fade fadePin(5, 10, 0, 255)
*
* to change the speed once declared.
* fadePin.writeSpeed(new speed);
* to read the current speed
* fadePin.readSpeed();
*
* fadePin.read() returns the current fade level (_pwmRate);
* fadePin.write(to) defines the new endpoint of the fader;
*
* fadePin.update(); needs to called in the loop
*
* foo.update(time); is for saving time variable if you want to sync Fade objects:
*
* unsigned long a = millis();
* foo.update(a);
* bar.update(a);
* foobar.update(a);
* it is faster than reading millis() and redefining the time every update for each Fade object
*/
#include "Fade.h"
#include <Bounce2.h>
const byte ledPin = 5;
const byte buttonPin = 6;
Fade myLed(ledPin, 15, 0, 255, MILLIS_TIMER);// led1 speed 10 milliseconds(microSeconds)/step; pwm at 10 min and 255 max (MILLIS_TIMER or MICROS_TIMER)
Bounce myButton = Bounce(); // Instantiate a Bounce object
void setup()
{
Serial.begin(9600);
//pinMode(13, OUTPUT);
myButton.attach(buttonPin);
myButton.interval(10); // interval in ms
pinMode(buttonPin, INPUT_PULLUP);
myLed.begin();
Serial.println("Setup Complete...");
}
void loop()
{
static bool activeFading = false;
static byte lastPressState = HIGH;
static unsigned long lastMillis = 0;
static unsigned long displayMillis = 0;
static byte storedSetpoint = 255;
static int led1Increment = 5;
myLed.update();
myButton.update();
byte thisPressState = myButton.read();
if (thisPressState == LOW && lastPressState == HIGH)
{
lastMillis = millis(); // start timer when button is pressed
Serial.println("Pressed");
}
else if(thisPressState == HIGH && lastPressState == LOW) //What to do when button is released?
{
Serial.println("Released");
if (!activeFading)
{
myLed.setTarget(myLed.getSetpoint() == myLed.getMin()? storedSetpoint : myLed.getMin()); // on off with a simple quick button press
}
else
{
activeFading = false; // simply stop fading
}
}
if(thisPressState == LOW && millis() - lastMillis > 1000UL && activeFading == false)
{
activeFading = true; // if you are holding down the button for > 1 sec, start fading
}
if (activeFading)
{
fadeLight(&myLed, storedSetpoint, led1Increment);
}
lastPressState = thisPressState;
if (millis() - displayMillis > 1000)
{
Serial.print("Current Briteness:");
Serial.println(myLed.getCurrent());
Serial.print("Current Setpoint:");
Serial.println(myLed.getSetpoint());
Serial.println();
displayMillis += 1000;
}
}
void fadeLight(Fade* myFade, byte &savedSetting, int &increment)
{
static unsigned long lastUpdateMillis = 0;
if (millis() - lastUpdateMillis > 125UL)
{
myFade->setTarget(constrain((myFade->getSetpoint() + increment), myFade->getMin(), myFade->getMax()));
if(myFade->getSetpoint() == myFade->getMin() || myFade->getSetpoint() == myFade->getMax())
{
unsigned long catchUpDelay = millis();
Serial.println("FLIP");
increment *= -1;
Serial.println(increment);
while(millis() - catchUpDelay < 1000UL) // let's hold here at the peak and trough in order to be able to set to min/max easily
{
myFade->update();
}
}
savedSetting = myFade->getSetpoint();
Serial.println(myFade->getSetpoint());
lastUpdateMillis = millis();
}
}
Fade.h:
#ifndef Fade_h
#define Fade_h
#include <Arduino.h>
enum timer{
MILLIS_TIMER,
MICROS_TIMER
};
class Fade
{
public:
Fade() {};
Fade(int pin, uint32_t timeStep = 15, uint8_t minVal = 0, uint8_t maxVal = 255, timer timerSelect = MILLIS_TIMER);
void begin();
void setTarget(int to);
void update();
void update(uint32_t time);
uint8_t getMin();
uint8_t getMax();
uint8_t getCurrent();
uint32_t readSpeed();
uint32_t writeSpeed(uint32_t time);
uint8_t getSetpoint();
private:
uint8_t _min;
uint8_t _max;
uint8_t _targetFade;
uint8_t _pwmRate;
uint32_t _time;
uint32_t _last;
uint8_t _pin;
bool _microsTimer;
};
#endif
Fade.cpp:
#include "Fade.h"
#include <Arduino.h>
Fade::Fade(int pin, uint32_t timeStep, uint8_t minVal, uint8_t maxVal, timer timerSelect)
{
_pin = pin;
_time = timeStep;
_min = minVal;
_max = maxVal;
analogWrite(_pin, _min);
_pwmRate = _min;
_microsTimer = timerSelect;
}
void Fade::begin()
{
analogWrite(_pin, _min);
}
void Fade::setTarget(int to)
{
_targetFade = (uint8_t) constrain(to, _min, _max);
this->update();
}
void Fade::update()
{
this->update(_microsTimer? micros() : millis());
}
void Fade::update(uint32_t time)
{
if (time - _time > _last)
{
_last = time;
if (_pwmRate > _targetFade) analogWrite(_pin, --_pwmRate);
if (_pwmRate < _targetFade) analogWrite(_pin, ++_pwmRate);
}
}
uint8_t Fade::getSetpoint()
{
return _targetFade;
}
uint8_t Fade::getCurrent()
{
return _pwmRate;
}
uint32_t Fade::readSpeed()
{
return _time;
}
uint32_t Fade::writeSpeed(uint32_t time)
{
_time = time;
}
uint8_t Fade::getMin()
{
return _min;
}
uint8_t Fade::getMax()
{
return _max;
}