Hello Everyone. I am new here and new to Arduino programming. I am experimenting with millis() to better understand it. I found a sketch at The Bald Engineer to fade an LED up and then down. I think I understand this pretty much. I also read over the Tute here.
I thought I might try altering the code so the led will fade in, then stay on a few seconds, then turn off. However, now I get nothing. Could someone please take a look at the this code and tell me where I am going wrong.
#define UP 0
#define DOWN 1
const int minPWM = 0;
const int maxPWM = 255;
byte fadeDirection = UP;
int fadeValue = 0;
byte fadeIncrement = 5;
unsigned long previousFadeMillis;
unsigned long previousMillis;
int fadeInterval = 50;
int onInterval = 3000;
void setup() {
analogWrite(pwmLED, fadeValue);
}
void doTheFade(unsigned long thisMillis) {
if (thisMillis - previousFadeMillis >= fadeInterval) {
if (fadeDirection == UP) {
fadeValue = fadeValue + fadeIncrement;
if (fadeValue >= maxPWM) {
}
}
fadeValue = maxPWM;
if (pwmLED, maxPWM) {
} analogWrite(pwmLED, HIGH);
if (thisMillis - previousMillis >= onInterval) {
} analogWrite(pwmLED, LOW);
}
previousMillis = thisMillis;
previousFadeMillis = thisMillis;
}
void loop() {
// get the current time, for this time around loop
unsigned long currentMillis = millis(); // all millis() timer checks will use this time stamp
doTheFade(currentMillis);
}
Well guys, still not working. Here is the corrected code. Still does nothing.
// Example Fading LED with analogWrite and millis()
// See baldengineer.com/fading-led-analogwrite-millis-example.html for more information
// Created by James Lewis
#define UP 0
#define DOWN 1
int pwmLED = 5;
const int minPWM = 0;
const int maxPWM = 255;
byte fadeDirection = UP;
int fadeValue = 0;
byte fadeIncrement = 5;
unsigned long previousFadeMillis;
unsigned long previousMillis;
int fadeInterval = 50;
int onInterval = 3000;
void setup() {
analogWrite(pwmLED, fadeValue);
}
void doTheFade(unsigned long thisMillis) {
if (thisMillis - previousFadeMillis >= fadeInterval) {
if (fadeDirection == UP) {
fadeValue = fadeValue + fadeIncrement;
if (fadeValue >= maxPWM) {
fadeValue = maxPWM;
}
}
if (pwmLED == maxPWM) {
analogWrite(pwmLED, HIGH);
}
if (thisMillis - previousMillis >= onInterval) {
analogWrite(pwmLED, LOW);
}
previousMillis = thisMillis;
previousFadeMillis = thisMillis;
}
}
Well, I did all the corrections suggestions, still see nothing but a dimly lit LED haunting me. Thanks for all your help. It back t the tutorial boards for me.
// Example Fading LED with analogWrite and millis()
// See baldengineer.com/fading-led-analogwrite-millis-example.html for more information
// Created by James Lewis
#define UP 0
#define DOWN 1
int pwmLED = 3;
const int minPWM = 0;
const int maxPWM = 255;
byte fadeDirection = UP;
int fadeValue = 0;
byte fadeIncrement = 5;
unsigned long thisMillis2;
unsigned long previousFadeMillis;
unsigned long previousMillis;
int fadeInterval = 50;
int onInterval = 3000;
void setup() {
analogWrite(pwmLED, fadeValue);
}
void doTheFade(unsigned long thisMillis) {
if (thisMillis - previousFadeMillis >= fadeInterval) {
if (fadeDirection == UP) {
fadeValue = fadeValue + fadeIncrement;
if (fadeValue >= maxPWM) {
fadeValue = maxPWM;
if (fadeValue == maxPWM) {
analogWrite(pwmLED, HIGH);
}
}
}
if (pwmLED == HIGH) {
if (thisMillis - previousMillis >= onInterval) {
analogWrite(pwmLED, LOW);
}
previousMillis = thisMillis2;
previousFadeMillis = thisMillis;
}
}
}
void loop() {
// get the current time, for this time around loop
unsigned long currentMillis = millis(); // all millis() timer checks will use this time stamp
doTheFade(currentMillis);
}
Hey guys.
My original idea was to fade in a red LED, then hold it high for a for a while. After it has been high for a bit , then fade in a yellow to make orange. Then fade out the red to make yellow only for a time. Then continue with green and blue to simulate a rainbow. To me, it's quite daunting, after learning how to fade without delay.
Thing is, I am getting really frustrated now so I think the best thing is to leave it and go on to some other projects for a while.