Hello Everyone,
I have a perplexing issue. Trying to fade an LED using a Pro Mini....now it works just fine on the Mega board...not on the mini> My code is below and thanks in advance for the help - Scott
const byte pwmLED = A1;
// define directions for LED fade
#define UP 0
#define DOWN 1
// constants for min and max PWM
const int minPWM = 0;
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 = 5;
// millis() timing Variable, just for fading
unsigned long previousFadeMillis;
// How fast to increment?
int fadeInterval = 50;
void setup() {
// put pwmLED into known state (off)
analogWrite(pwmLED, fadeValue);
}
void doTheFade(unsigned long thisMillis) {
if (thisMillis - previousFadeMillis >= fadeInterval) {
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;
}
}
analogWrite(pwmLED, fadeValue);
previousFadeMillis = thisMillis;
}
}
void loop() {
unsigned long currentMillis = millis();
doTheFade(currentMillis);
}
Now I've also used the below code on a Mega to fade an led and it works:
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
unsigned long currentTime;
unsigned long loopTime;
void setup() {
// declare any pin to be an output:
pinMode(A1, OUTPUT);
currentTime = millis();
loopTime = currentTime;
}
void loop() {
currentTime = millis();
if(currentTime >= (loopTime + 35)){ // 20
// set the brightness of any:
analogWrite(A1, 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 ;
}
loopTime = currentTime; // Updates loopTime
}
}
I will add that both sketches will Fade the LED using an UNO and Mega....however when uploaded to a Pro Mini the LED only blinks.