I know this has already been discussed above with regard to beginners guide to mills(), but to be honest, I went through it and I could not find the answer.
I am using mills() instead of delay() for fading my LED, which is ok, it works in the first if(), however, when I try to prolong my "fading" , like someone can easily do with the delay(), somehow, the millis() is not working. Probably I have to code it differently...but I do not know where to tackle this problem. Please open my eyes and let me know what I am doing wrong here?
// +++++++ FADING FUNCTION +++++++
if (OS > HT && currentTime - startTime > 1000) {
if (fadeUp) {
if (brightness <= 254) {
brightness++;
analogWrite(ledPin, pgm_read_byte(&table[brightness]));
startTime += currentTime;
if ( currentTime1 - startTime >= 100 ) {
//delay(5);
Serial.println(" over with ++");
startTime += currentTime1;
}
}
}
else if (!fadeUp) {
if (brightness >= 1) {
brightness--;
analogWrite(ledPin, pgm_read_byte(&table[brightness]));
startTime += currentTime;
if ( currentTime1 - startTime >= 100 ) {
//delay(5);
Serial.println("over with --");
startTime += currentTime1;
}
}
}
Somehow I have deleted the previous post, internet was down, etc. Anyway, below is a working version, I am happy with it, but is there a way to write this better?
if (OS > HT && currentTime - startTime > 1000) {
if (fadeUp) {
if (brightness <= 254) {
currentTime1 = millis();
if (currentTime1 - startTime1 > 5) {
brightness++;
startTime1 = currentTime1;
analogWrite(ledPin, pgm_read_byte(&table[brightness]));
}
}
}
else if (!fadeUp) {
if (brightness >= 1) {
currentTime1 = millis();
if ( currentTime1 - startTime1 > 5) {
brightness--;
startTime1 = currentTime1;
analogWrite(ledPin, pgm_read_byte(&table[brightness]));
}
}
}
when I try to prolong my "fading" ... the millis() is not working.
The usual cause for this sort of problem is not using "long" for the "starttime" (or other variables that hold saved copies of millis())
(can't tell, in your case, since that part of the code wasn't included. Hint, hint.)
The one thing that is easier with the delay() is the fact that there is a function available, where you just put in a number eg. delay(x)... anyway, the above code is ok, at least for now, I have removed the currentTime1, because I can only use one variable, namely currentTime, which is the same
Now I am wondering, if there is a way to reduce the number of variables even further?
Btw how come that no body has not yet already wrote a "NEW" delay with mills() or micros() as a function or am I just not looking good enough ?