Can someone please help me out? I've tried time and again to get a fade to turn on and off by IR remote. I borrowed sketch from bald engineer and I have a basic on/off sketch that I can turn an LED on and off but I just can't get the fade to. Any help would be appreciated.
FADE
// define directions for LED fade
#define UP 0
#define DOWN 1
int pwmLED=5;
// constants for min and max PWM
const int minPWM = 2;
const int maxPWM = 255;
// State Variable for Fade Direction
byte fadeDirection = UP;
// Global Fade Value
// but be bigger than byte and signed, for rollover
int fadeValue = 0;
// How smooth to fade?
byte fadeIncrement = 4;
// millis() timing Variable, just for fading
unsigned long previousFadeMillis;
// How fast to increment?
int fadeInterval = 130;
void setup() {
// put pwmLED into known state (off)
analogWrite(pwmLED, fadeValue);
}
void doTheFade(unsigned long thisMillis) {
// is it time to update yet?
// if not, nothing happens
if (thisMillis - previousFadeMillis >= fadeInterval) {
// yup, it's time!
if (fadeDirection == UP) {
fadeValue = fadeValue + fadeIncrement;
if (fadeValue >= maxPWM) {
// At max, limit and change direction
fadeValue = maxPWM;
fadeDirection = DOWN;
}
} else {
//if we aren't going up, we're going down
fadeValue = fadeValue - fadeIncrement;
if (fadeValue <= minPWM) {
// At min, limit and change direction
fadeValue = minPWM;
fadeDirection = UP;
}
}
// Only need to update when it changes
analogWrite(pwmLED, fadeValue);
// reset millis for the next iteration (fade timer only)
previousFadeMillis = thisMillis;
}
}
void loop() {
// get the current time, for this time around loop
// all millis() timer checks will use this time stamp
unsigned long currentMillis = millis();
doTheFade(currentMillis);
}
REMOTE
/*
source: www.electroschematics.com
You'll need to change the led pins and the codes
accordingly to your configuration and IR remote
*/
#include <IRremote.h>
int RECV_PIN = 12; // the pin where you connect the output pin of TSOP4838
int ledPin = 5;
int itsONled[] = {0,0,0};
/* the initial state of LEDs is OFF (zero)
the first zero must remain zero but you can
change the others to 1's if you want a certain
led to light when the board is powered */
#define code1 41565 // code received from pwr button
#define code2 57885 // code received from mute button
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600); // you can comment this line
irrecv.enableIRIn(); // Start the receiver
pinMode(ledPin, OUTPUT);
}
void loop()
{
if (irrecv.decode(&results)) {
unsigned int value = results.value;
switch(value) {
case code1:
if(itsONled[1] == 1) { // if first led is on then
digitalWrite(ledPin, LOW); // turn it off when button is pressed
itsONled[1] = 0; // and set its state as off
} else { // else if first led is off
digitalWrite(ledPin, HIGH); // turn it on when the button is pressed
itsONled[1] = 1; // and set its state as on
}
}
Serial.println(value); // you can comment this line
irrecv.resume(); // Receive the next value
}
}