As an experiment for a future project, I wanted to write a sketch that had a led continuously flashing on and off with different on and off periods. Also, a second led, in response to a button press, would fade up a led to a set level and on reaching that level, jump its brightness to max, hold it there for four seconds then extinguish it. Obviously I had to use millis() for the timings.
Well, I had no problem with the first, flashing led and the second requirement I also managed - up to a point. I can't get the second led to switch off after the set interval of four seconds. The daft thing is, I know why but I just can't get my head around how to fix it.
I know that the period timer is being reset every time through the loop. and I just can't work out where to place the reset so it resets only before each time that the max brightness period needs to be started. If I isolate that piece of timing code as a standalone sketch and set the led to HIGH and place the timer = millis() in setup(), the led stays on for the requisite 4 seconds and goes then off - exactly as I want it to as part of the main sketch.
Can anyone throw me a lifeline to indicate what is probably obvious to most of you and help me find a way to resolve my timer reset conundrum?
Here's my code - it's not pretty, but it was only ever supposed to be a working model for part of a bigger project. I guess the error is where I have commented in the "LED FADE PROCEDURE" near the end of the sketch
// Pin assignments
const byte flashLed = 4; // flashing led pin
const byte faderLed = 6; // fading led pin
const byte faderSwitchPin = 12; // fader switch pin (other connection to ground)
// Fader Switch
boolean faderSwitchFlag = false; // fader switch blocker (switch routine not to be re-called during led flash)
byte oldFaderSwitchState = HIGH; // assume switch open because of internal pull-up
const unsigned long faderDebounceTime = 10; // milliseconds debounce setting
unsigned long faderSwitchPressTime; // when the switch last changed state
// Fader Led Parameters
unsigned long timeThen = 0; // time variable
int faderBrightness = 0; // fader led brightness level
int fadeAmount = 2; // fade up increments for fader led
int limit = 100; // max value to fade up to
unsigned long brightInterval = 4000; // amount of time for fader led to be at max brightness
unsigned long timer; // timer for fader led max brightness period
// Flash Led Parameters
int flashTime; // variable for flash interval(s)
void setup () {
// Pins
pinMode (flashLed, OUTPUT);
pinMode (faderLed, OUTPUT);
pinMode (faderSwitchPin, INPUT_PULLUP); // internal resistor utilised
// Debug
Serial.begin (9600); // debug
} // end of setup
void loop ( ) {
flash(); // Do the flash procedure
faderSwitchCall(); // poll the fader switch
if (faderSwitchFlag) // if switch triggered,
fade(); // do the fader process
} // end of loop
// ---PROCEDURES---
// FLASHING LED PROCEDURE
void flash() {
unsigned long flashCurrentTime = millis(); // read current time
if (flashCurrentTime > flashTime) { // is it time to flash on or off?
if (digitalRead(flashLed)) { // if so and led is on
digitalWrite(flashLed, LOW); // turn it off
flashTime = flashCurrentTime + 1500; // add 1.5 secs before changing again
}
else {
digitalWrite (flashLed, HIGH); // else its off so turn it on
flashTime = flashCurrentTime + 500; // add 0.5 secs before changing again
}
}
}
// FADING LED SWITCH PROCEDURE
void faderSwitchCall () {
byte faderSwitchState = digitalRead (faderSwitchPin); // Read the switch state
if (faderSwitchState != oldFaderSwitchState) { // has it changed since last time it was read?
if (millis () - faderSwitchPressTime >= faderDebounceTime) { // has enough time passed with stable reading?
faderSwitchPressTime = millis (); // update the time
oldFaderSwitchState = faderSwitchState; // remember the switch state for next time
if (faderSwitchState == LOW) { // if switch passes debounce test and is pressed
faderBrightness = 0; // set fader led to off
faderSwitchFlag = true; // set flag to enable fader procedure
}
}
}
}
// LED FADE PROCEDURE
void fade () {
if (faderSwitchFlag) { // Ok to fade?
unsigned long timeNow = millis(); // set timer
if (timeNow >= (timeThen + 80)) { // is it time to increase brightness?
analogWrite (faderLed, faderBrightness); // then write new value to led
faderBrightness = faderBrightness + fadeAmount; // increment brightness value
if (faderBrightness >= limit) { // has the brightness reached set limit?
digitalWrite (faderLed, HIGH); // if so, set fader led to max brightness
// THIS IS THE MISTAKE AREA ************************************
timer = millis (); { // start brightness period timer
if ((millis() - timer) > brightInterval) // has enough time passed at max brightness?
digitalWrite (faderLed, LOW); // if so, turn led off
// LED REMAINS LIT FULL BECAUSE TIMER IS CONTINUALLY RESET *****
}
faderSwitchFlag = false; // fade procedure complete
}
timeThen = timeNow; // save current fade time to compare again
}
}
}
Can anyone throw me a lifeline to indicate what is probably obvious to most of you and help me find a way to resolve my timer reset conundrum?
You can add a control variable for starting the time period. Declare a new boolean variable timerStarted = false.
Start the timer if it enters the conditional when false, and then set to true.reset to false when ready for the next cycle.
// Pin assignments
const byte flashLed = 4; // flashing led pin
const byte faderLed = 6; // fading led pin
const byte faderSwitchPin = 12; // fader switch pin (other connection to ground)
// Fader Switch
boolean faderSwitchFlag = false; // fader switch blocker (switch routine not to be re-called during led flash)
byte oldFaderSwitchState = HIGH; // assume switch open because of internal pull-up
const unsigned long faderDebounceTime = 10; // milliseconds debounce setting
unsigned long faderSwitchPressTime; // when the switch last changed state
// Fader Led Parameters
unsigned long timeThen = 0; // time variable
int faderBrightness = 0; // fader led brightness level
int fadeAmount = 2; // fade up increments for fader led
int limit = 100; // max value to fade up to
unsigned long brightInterval = 4000; // amount of time for fader led to be at max brightness
unsigned long timer;// timer for fader led max brightness period
//declare new variable
boolean timerStarted = false;
// Flash Led Parameters
int flashTime; // variable for flash interval(s)
void setup () {
// Pins
pinMode (flashLed, OUTPUT);
pinMode (faderLed, OUTPUT);
pinMode (faderSwitchPin, INPUT_PULLUP); // internal resistor utilised
// Debug
Serial.begin (9600); // debug
} // end of setup
void loop ( ) {
flash(); // Do the flash procedure
faderSwitchCall(); // poll the fader switch
if (faderSwitchFlag) // if switch triggered,
fade(); // do the fader process
} // end of loop
// ---PROCEDURES---
// FLASHING LED PROCEDURE
void flash() {
unsigned long flashCurrentTime = millis(); // read current time
if (flashCurrentTime > flashTime) { // is it time to flash on or off?
if (digitalRead(flashLed)) { // if so and led is on
digitalWrite(flashLed, LOW); // turn it off
flashTime = flashCurrentTime + 1500; // add 1.5 secs before changing again
}
else {
digitalWrite (flashLed, HIGH); // else its off so turn it on
flashTime = flashCurrentTime + 500; // add 0.5 secs before changing again
}
}
}
// FADING LED SWITCH PROCEDURE
void faderSwitchCall () {
byte faderSwitchState = digitalRead (faderSwitchPin); // Read the switch state
if (faderSwitchState != oldFaderSwitchState) { // has it changed since last time it was read?
if (millis () - faderSwitchPressTime >= faderDebounceTime) { // has enough time passed with stable reading?
faderSwitchPressTime = millis (); // update the time
oldFaderSwitchState = faderSwitchState; // remember the switch state for next time
if (faderSwitchState == LOW) { // if switch passes debounce test and is pressed
faderBrightness = 0; // set fader led to off
faderSwitchFlag = true; // set flag to enable fader procedure
}
}
}
}
// LED FADE PROCEDURE
void fade () {
if (faderSwitchFlag) { // Ok to fade?
unsigned long timeNow = millis(); // set timer
if (timeNow >= (timeThen + 80)) { // is it time to increase brightness?
analogWrite (faderLed, faderBrightness); // then write new value to led
faderBrightness = faderBrightness + fadeAmount; // increment brightness value
//add timerStart state to conditional
if (faderBrightness >= limit && timerStarted == false) { // has the brightness reached set limit?
digitalWrite (faderLed, HIGH); // if so, set fader led to max brightness
timerStarted = true;
// THIS IS THE MISTAKE AREA ************************************
timer = millis ();
}
//{ // start brightness period timer
if ((millis() - timer) > brightInterval) // has enough time passed at max brightness?
{
digitalWrite (faderLed, LOW); // if so, turn led off
timerStarted = false; //reset control
}
faderSwitchFlag = false; // fade procedure complete
}
timeThen = timeNow; // save current fade time to compare again
}
}
I thought you'd hit the nail on the head then cattledog, it made perfect sense - but unfortunately something has now screwed the fade up process. The variable faderBrightness is not incrementing and the fader led does not light at all
If I delete the "}" after timer = millis(); and move it after timeThen = timeNow, I end up where I was before with the led brightening to 100 and then going to full brightness - but it doesn't go off .
My "new" code is simply the amendment that cattledog posted.
I simply deleted the "}" after the timer was set (timer = millis(); ) and moved it after timeThen = timeNow like this:
// Pin assignments
const byte flashLed = 4; // flashing led pin
const byte faderLed = 6; // fading led pin
const byte faderSwitchPin = 12; // fader switch pin (other connection to ground)
// Fader Switch
boolean faderSwitchFlag = false; // fader switch blocker (switch routine not to be re-called during led flash)
byte oldFaderSwitchState = HIGH; // assume switch open because of internal pull-up
const unsigned long faderDebounceTime = 10; // milliseconds debounce setting
unsigned long faderSwitchPressTime; // when the switch last changed state
// Fader Led Parameters
unsigned long timeThen = 0; // time variable
int faderBrightness = 0; // fader led brightness level
int fadeAmount = 2; // fade up increments for fader led
int limit = 100; // max value to fade up to
unsigned long brightInterval = 4000; // amount of time for fader led to be at max brightness
unsigned long timer;// timer for fader led max brightness period
//declare new variable
boolean timerStarted = false;
// Flash Led Parameters
int flashTime; // variable for flash interval(s)
void setup () {
// Pins
pinMode (flashLed, OUTPUT);
pinMode (faderLed, OUTPUT);
pinMode (faderSwitchPin, INPUT_PULLUP); // internal resistor utilised
// Debug
Serial.begin (9600); // debug
} // end of setup
void loop ( ) {
flash(); // Do the flash procedure
faderSwitchCall(); // poll the fader switch
if (faderSwitchFlag) // if switch triggered,
fade(); // do the fader process
} // end of loop
// ---PROCEDURES---
// FLASHING LED PROCEDURE
void flash() {
unsigned long flashCurrentTime = millis(); // read current time
if (flashCurrentTime > flashTime) { // is it time to flash on or off?
if (digitalRead(flashLed)) { // if so and led is on
digitalWrite(flashLed, LOW); // turn it off
flashTime = flashCurrentTime + 1500; // add 1.5 secs before changing again
}
else {
digitalWrite (flashLed, HIGH); // else its off so turn it on
flashTime = flashCurrentTime + 500; // add 0.5 secs before changing again
}
}
}
// FADING LED SWITCH PROCEDURE
void faderSwitchCall () {
byte faderSwitchState = digitalRead (faderSwitchPin); // Read the switch state
if (faderSwitchState != oldFaderSwitchState) { // has it changed since last time it was read?
if (millis () - faderSwitchPressTime >= faderDebounceTime) { // has enough time passed with stable reading?
faderSwitchPressTime = millis (); // update the time
oldFaderSwitchState = faderSwitchState; // remember the switch state for next time
if (faderSwitchState == LOW) { // if switch passes debounce test and is pressed
faderBrightness = 0; // set fader led to off
faderSwitchFlag = true; // set flag to enable fader procedure
}
}
}
}
// LED FADE PROCEDURE
void fade () {
if (faderSwitchFlag) { // Ok to fade?
unsigned long timeNow = millis(); // set timer
if (timeNow >= (timeThen + 80)) { // is it time to increase brightness?
analogWrite (faderLed, faderBrightness); // then write new value to led
faderBrightness = faderBrightness + fadeAmount; // increment brightness value
//add timerStart state to conditional
if (faderBrightness >= limit && timerStarted == false) { // has the brightness reached set limit?
digitalWrite (faderLed, HIGH); // if so, set fader led to max brightness
timerStarted = true;
// THIS IS THE MISTAKE AREA ************************************
timer = millis ();
// } REMOVED
//{ // start brightness period timer
if ((millis() - timer) > brightInterval) // has enough time passed at max brightness?
{
digitalWrite (faderLed, LOW); // if so, turn led off
timerStarted = false; //reset control
}
faderSwitchFlag = false; // fade procedure complete
}
timeThen = timeNow; // save current fade time to compare again
}
}
} // Added
but unfortunately neither versions work as hoped. Cattledogs version doesn't fade up or light the fader led at all whereas my original code and change to cattledog's just results in the fade up of the led and full brightness applied but no switch off of the led after any time period.
The reset of the faderSwitchflag = false, ready for the next button press, is not at the right level.
I don't have time to look at it in detail right now, but I'm sure that's where the problem lies.
The line may need to go here, but I think there needs to be some control code so that once the led times out, you don't reenter this block.
if ((millis() - timer) > brightInterval) // has enough time passed at max brightness?
{
digitalWrite (faderLed, LOW); // if so, turn led off
timerStarted = false; //reset control
faderSwitchFlag = false;
}
//faderSwitchFlag = false;
// LED FADE PROCEDURE
void fade () {
if (faderSwitchFlag) { // Ok to fade?
unsigned long timeNow = millis(); // set timer
if (timeNow >= (timeThen + 80)) { // is it time to increase brightness?
analogWrite (faderLed, faderBrightness); // then write new value to led
faderBrightness = faderBrightness + fadeAmount; // increment brightness value
//add timerStart state to conditional
if (faderBrightness >= limit && timerStarted == false) { // has the brightness reached set limit?
digitalWrite(faderLed, HIGH); // if so, set fader led to max brightness
timer = millis(); //start brightness period timer
timerStarted = true; //control variable
}
// test for completion of brightness period timer only when timerStarted
if ((millis() - timer) > brightInterval && timerStarted == true) // has enough time passed at max brightness?
{
digitalWrite (faderLed, LOW); // if so, turn led off
timerStarted = false; //reset control
faderSwitchFlag = false; // eacape fade loop and enable another switch press
}
timeThen = timeNow; // save current fade time to compare again
}
}
}
flashTime is overflowing and the flash stops after some time. Is that what you want?
//int flashTime;
unsigned long flashTime;// variable for flash interval(s)
Hmmmm, many thanks for that cattledog. Its certainly almost working now, but whats happening now is that the led starts to fade up then goes to max brightness momentarily before continuing its fade up, ie fadeup, flash, fadeup. If I change the value of the variable "limit" then the time at which the flash occurs changes.
At the point where the brightness "limit" is reached (ie "faderBrightness" should be 100) and the led written HIGH, a Serial.read gives a "faderBrightness" value of 200 and when the LED gets written LOW, the value of "faderBrightness" has increased to 302. Obviously the fading loop is not stopping when "faderBrightness" reaches 100 - but I don't see why.
The current iteration of the code is as follows:
// Pin assignments
const byte flashLed = 4; // flashing led pin
const byte faderLed = 6; // fading led pin
const byte faderSwitchPin = 12; // fader switch pin (other connection to ground)
// Fader Switch
boolean faderSwitchFlag = false; // fader switch blocker (switch routine not to be re-called during led flash)
byte oldFaderSwitchState = HIGH; // assume switch open because of internal pull-up
const unsigned long faderDebounceTime = 10; // milliseconds debounce setting
unsigned long faderSwitchPressTime; // when the switch last changed state
// Fader Led Parameters
unsigned long timeThen = 0; // time variable
int faderBrightness = 0; // fader led brightness level
int fadeAmount = 2; // fade up increments for fader led
int limit = 200; // max value to fade up to
unsigned long brightInterval = 4000; // amount of time for fader led to be at max brightness
unsigned long timer; // timer for fader led max brightness period
boolean timerStarted = false;
// Flash Led Parameters
unsigned long flashTime; // variable for flash interval(s)
void setup () {
// Pins
pinMode (flashLed, OUTPUT);
pinMode (faderLed, OUTPUT);
pinMode (faderSwitchPin, INPUT_PULLUP); // internal resistor utilised
// Debug
Serial.begin (9600); // debug
} // end of setup
void loop ( ) {
flash(); // Do the flash procedure
faderSwitchCall(); // poll the fader switch
if (faderSwitchFlag) // if switch triggered,
fade(); // do the fader process
} // end of loop
// ---PROCEDURES---
// FLASHING LED PROCEDURE
void flash() {
unsigned long flashCurrentTime = millis(); // read current time
if (flashCurrentTime > flashTime) { // is it time to flash on or off?
if (digitalRead(flashLed)) { // if so and led is on
digitalWrite(flashLed, LOW); // turn it off
flashTime = flashCurrentTime + 1500; // add 1.5 secs before changing again
}
else {
digitalWrite (flashLed, HIGH); // else its off so turn it on
flashTime = flashCurrentTime + 500; // add 0.5 secs before changing again
}
}
}
// FADING LED SWITCH PROCEDURE
void faderSwitchCall () {
byte faderSwitchState = digitalRead (faderSwitchPin); // Read the switch state
if (faderSwitchState != oldFaderSwitchState) { // has it changed since last time it was read?
if (millis () - faderSwitchPressTime >= faderDebounceTime) { // has enough time passed with stable reading?
faderSwitchPressTime = millis (); // update the time
oldFaderSwitchState = faderSwitchState; // remember the switch state for next time
if (faderSwitchState == LOW) { // if switch passes debounce test and is pressed
faderBrightness = 0; // set fader led to off
faderSwitchFlag = true; // set flag to enable fader procedure
}
}
}
}
// LED FADE PROCEDURE
void fade () {
if (faderSwitchFlag) { // Ok to fade?
unsigned long timeNow = millis(); // set fader interval timer
if (timeNow >= (timeThen + 80)) { // is it time to increase brightness?
analogWrite (faderLed, faderBrightness); // then write new value to led
faderBrightness = faderBrightness + fadeAmount; // increment brightness value
if (faderBrightness >= limit && timerStarted == false) { // has the brightness reached set limit and is timer started?
digitalWrite (faderLed, HIGH); // if so, set fader led to max brightness
Serial.println (faderBrightness); // debug
timer = millis (); // start brightness period timer
timerStarted = true; // set brightness period timer flag to started
}
if ((millis() - timer) > brightInterval && timerStarted == true) { // has enough time passed at max brightness?
digitalWrite (faderLed, LOW); // if so, turn led off
Serial.println (faderBrightness); // debug
timerStarted = false; // set brightness period timer flag to not started
faderSwitchFlag = false; // fade procedure complete
}
timeThen = timeNow; // save current fade time to compare again
}
}
}
I have rearranged some lines, and put a cap on the incrementing of faderBrightness. There is still the brief flash between two constant levels at the limit. Is that what you want?
// Pin assignments
const byte flashLed = 4; // flashing led pin
const byte faderLed = 6; // fading led pin
const byte faderSwitchPin = 12; // fader switch pin (other connection to ground)
// Fader Switch
boolean faderSwitchFlag = false; // fader switch blocker (switch routine not to be re-called during led flash)
byte oldFaderSwitchState = HIGH; // assume switch open because of internal pull-up
const unsigned long faderDebounceTime = 10; // milliseconds debounce setting
unsigned long faderSwitchPressTime; // when the switch last changed state
// Fader Led Parameters
unsigned long timeThen = 0; // time variable
int faderBrightness = 0; // fader led brightness level
int fadeAmount = 2; // fade up increments for fader led
int limit = 200; // max value to fade up to
unsigned long brightInterval = 4000; // amount of time for fader led to be at max brightness
unsigned long timer; // timer for fader led max brightness period
boolean timerStarted = false;
// Flash Led Parameters
unsigned long flashTime; // variable for flash interval(s)
void setup () {
// Pins
pinMode (flashLed, OUTPUT);
pinMode (faderLed, OUTPUT);
pinMode (faderSwitchPin, INPUT_PULLUP); // internal resistor utilised
// Debug
Serial.begin (9600); // debug
} // end of setup
void loop ( ) {
flash(); // Do the flash procedure
faderSwitchCall(); // poll the fader switch
if (faderSwitchFlag) // if switch triggered,
fade(); // do the fader process
} // end of loop
// ---PROCEDURES---
// FLASHING LED PROCEDURE
void flash() {
unsigned long flashCurrentTime = millis(); // read current time
if (flashCurrentTime > flashTime) { // is it time to flash on or off?
if (digitalRead(flashLed)) { // if so and led is on
digitalWrite(flashLed, LOW); // turn it off
flashTime = flashCurrentTime + 1500; // add 1.5 secs before changing again
}
else {
digitalWrite (flashLed, HIGH); // else its off so turn it on
flashTime = flashCurrentTime + 500; // add 0.5 secs before changing again
}
}
}
// FADING LED SWITCH PROCEDURE
void faderSwitchCall () {
byte faderSwitchState = digitalRead (faderSwitchPin); // Read the switch state
if (faderSwitchState != oldFaderSwitchState) { // has it changed since last time it was read?
if (millis () - faderSwitchPressTime >= faderDebounceTime) { // has enough time passed with stable reading?
faderSwitchPressTime = millis (); // update the time
oldFaderSwitchState = faderSwitchState; // remember the switch state for next time
if (faderSwitchState == LOW) { // if switch passes debounce test and is pressed
faderBrightness = 0; // set fader led to off
faderSwitchFlag = true; // set flag to enable fader procedure
}
}
}
}
// LED FADE PROCEDURE
void fade () {
if (faderSwitchFlag) { // Ok to fade?
unsigned long timeNow = millis(); // set fader interval timer
if (timeNow >= (timeThen + 80)) // is it time to increase brightness?
{
faderBrightness = faderBrightness + fadeAmount; // increment brightness value
if (faderBrightness >= limit)
faderBrightness = limit;
analogWrite (faderLed, faderBrightness); // then write new value to led
if (faderBrightness == limit && timerStarted == false) { // has the brightness reached set limit and is timer started?
digitalWrite (faderLed, HIGH); // if so, set fader led to max brightness
Serial.println (faderBrightness); // debug
timer = millis (); // start brightness period timer
timerStarted = true; // set brightness period timer flag to started
}
if ((millis() - timer) > brightInterval && timerStarted == true) { // has enough time passed at max brightness?
digitalWrite (faderLed, LOW); // if so, turn led off
Serial.println (faderBrightness); // debug
timerStarted = false; // set brightness period timer flag to not started
faderSwitchFlag = false; // fade procedure complete
}
timeThen = timeNow; // save current fade time to compare again
}
}
}
Many grateful thanks for your patience and perseverance cattledog
I'm at work currently so can't test the code, but I'll load it up at home later and report back.
Regarding the brief flash, just to clarify what I ultimately want is, for the fader led, when triggered by a pushbutton, to gradually fade up from zero to 100, then instantly jump to 255, or HIGH, and then to stay there for 4 seconds before switching back to zero, or LOW, and to be then ready to be triggered again when required. However if your latest code change does exactly as above, but with a spurious brief flash at the level change, that's no big deal, I'll live with it.
On the face of it, it seems such a simple process, but it certainly isn't!!
As I say, I'll test drive it this evening. Thanks again cattledog and Kudos
I apologize. I misunderstood the requirements and the previous code does not not do what you want. Here's a version which I think is correct. The fade routine is now called unconditionally, and there are three conditions which advance within that routine--fading, holding bright, and turning off.
// Pin assignments
const byte flashLed = 4; // flashing led pin
const byte faderLed = 6; // fading led pin
const byte faderSwitchPin = 12; // fader switch pin (other connection to ground)
// Fader Switch
boolean faderSwitchFlag = false; // fader switch blocker (switch routine not to be re-called during led flash)
byte oldFaderSwitchState = HIGH; // assume switch open because of internal pull-up
const unsigned long faderDebounceTime = 10; // milliseconds debounce setting
unsigned long faderSwitchPressTime; // when the switch last changed state
// Fader Led Parameters
unsigned long timeThen = 0; // time variable
int faderBrightness = 0; // fader led brightness level
int fadeAmount = 2; // fade up increments for fader led
int limit = 100; // max value to fade up to
unsigned long brightInterval = 4000; // amount of time for fader led to be at max brightness
unsigned long timer; // timer for fader led max brightness period
boolean timerStarted = false;
// Flash Led Parameters
unsigned long flashTime; // variable for flash interval(s)
void setup () {
// Pins
pinMode (flashLed, OUTPUT);
pinMode (faderLed, OUTPUT);
pinMode (faderSwitchPin, INPUT_PULLUP); // internal resistor utilised
// Debug
Serial.begin (9600); // debug
} // end of setup
void loop ( ) {
flash(); // Do the flash procedure
faderSwitchCall(); // poll the fader switch
// if (faderSwitchFlag) // if switch triggered,
fade(); // do the fader process
} // end of loop
// ---PROCEDURES---
// FLASHING LED PROCEDURE
void flash() {
unsigned long flashCurrentTime = millis(); // read current time
if (flashCurrentTime > flashTime) { // is it time to flash on or off?
if (digitalRead(flashLed)) { // if so and led is on
digitalWrite(flashLed, LOW); // turn it off
flashTime = flashCurrentTime + 1500; // add 1.5 secs before changing again
}
else {
digitalWrite (flashLed, HIGH); // else its off so turn it on
flashTime = flashCurrentTime + 500; // add 0.5 secs before changing again
}
}
}
// FADING LED SWITCH PROCEDURE
void faderSwitchCall () {
byte faderSwitchState = digitalRead (faderSwitchPin); // Read the switch state
if (faderSwitchState != oldFaderSwitchState) { // has it changed since last time it was read?
if (millis () - faderSwitchPressTime >= faderDebounceTime) { // has enough time passed with stable reading?
faderSwitchPressTime = millis (); // update the time
oldFaderSwitchState = faderSwitchState; // remember the switch state for next time
if (faderSwitchState == LOW) { // if switch passes debounce test and is pressed
faderBrightness = 0; // set fader led to off
faderSwitchFlag = true; // set flag to enable fader procedure
}
}
}
}
// LED FADE PROCEDURE
void fade () {
if (faderSwitchFlag) // Ok to fade?
{
unsigned long timeNow = millis(); // set fader interval timer
if (timeNow >= (timeThen + 80)) // is it time to increase brightness?
{
timeThen = timeNow;
faderBrightness = faderBrightness + fadeAmount; // increment brightness value
if (faderBrightness >= limit)
faderBrightness = limit;
analogWrite (faderLed, faderBrightness); // then write new value to led
}
}
if (faderBrightness == limit && timerStarted == false) // has the brightness reached set limit and is timer started?
{
digitalWrite (faderLed, HIGH); // if so, set fader led to max brightness
faderBrightness = 0; // no longer needed
timer = millis(); // start brightness period timer
timerStarted = true; // set brightness period timer flag to started
faderSwitchFlag = false; //fade complete
}
if ((millis() - timer) > brightInterval && timerStarted == true) // has enough time passed at max brightness?
{
digitalWrite (faderLed, LOW); // if so, turn led off
timerStarted = false; // set brightness period timer flag to not started
}
}
cattledog - you are a genious and I shall be ever grateful
That latest version is absolutely perfect, so I will do it the justice it deserves and examine your methodology closely.
At first glance, and to paraphrase an old British comedian it seems that all the code was there, it was not necessarily in the right order. I know thats not strictly true, but thats the excuse my addled brain has made for itself! I do tend to lose track of which instructions should be within the same set of braces and those which should not! I also need to understand where and when to use conditional statements more efficiently.
Once again many thanks, I can now move on with the main sketch at last
That latest version is absolutely perfect, so I will do it the justice it deserves and examine your methodology closely.
The methodology of cascading conditional tests with added control variables is kind of ugly, and came about from the initial state of the sketch. You are correct that the getting the instructions within the proper set of braces gets complicated.
It would probably be cleaner and easier to understand if the fade() function which now contains fade, bright hold, and off was recrafted as a "state machine" with switch case statements which progressed as sections were completed.
It would probably be cleaner and easier to understand if the fade() function which now contains fade, bright hold, and off was recrafted as a "state machine" with switch case statements which progressed as sections were completed.
You could well be right, state machines is a subject I keep promising myself to master. Mebe this is the push I need to venture outside my comfort zone .