Hi Guys
I hope someone can offer me some pointers here. I have a sketch which pulses a set of leds whilst fading up and then flashing another pair in response to a button press. That part works ok.
What I'm trying to figure out is how to, in response to the single button push, call the frontLed() function 3 times with a 2 second gap between each call, then wait 20 seconds and then call another function (not yet written). All this while not interfering with the pulsing leds.
I've been wrestling with various millis based timing routines, but nothing seems to achieve what I'm after. Do I have to time the whole function then somehow build in a millis based pause, but how to do that without messing up the other functions?
Does anyone have any suggestions? The code so far follows:
// ALLOCATIONS
// ========================================================================
// Pins
const byte frontRedLed = 3; //
const byte frontOrangeLed = 4;
const byte indLed = 6;
const byte ledSwitchPin = 8; // switch pin (other connection to ground)
// ========================================================================
// led Switch
boolean ledSwitchFlag = false; // torper switch blocker (switch routine not to be called during led flash)
byte oldLedSwitchState = HIGH; // assume switch open because of pull-up resistor
const unsigned long ledDebounceTime = 100; // milliseconds debounce setting
unsigned long ledSwitchPressTime; // when the switch last changed state
// ========================================================================
// Fader Parameters
unsigned long timeThen; // time variable during ramp up unsigned long flashTimeNow = millis();
int ledBrightness = 0; // initial torp ramp up brightness (ie off)
int ledFadeAmount = 1; // fade up increments for torper
int ledBlip = 40 ; // max value to fade up to (45 max level)
unsigned long ledTimer; // timer for fader led max brightness period
boolean ledTimerStarted = false; // torp flash flag
int minBeamOnTime = 100; // minimum on time for led
int maxBeamOnTime = 6000; // maximum on time for led
// ========================================================================
// Pulsing Leds
static float pulseAm = 1.57;
float pulseInc;
int swing = 70;
// ========================================================================
// SETUP
void setup ()
{
// Pins
pinMode (frontRedLed, OUTPUT);
pinMode (frontOrangeLed, OUTPUT);
pinMode (indLed, OUTPUT);
pinMode (ledSwitchPin, INPUT_PULLUP);
// ========================================================================
} // end of setup
// ========================================================================
// MAIN LOOP
void loop ( )
{
indPulse();
ledSwitchCall(); // Poll switch
frontLed();
} // end of loop
// ========================================================================
// FUNCTIONS
void indPulse()
{
pulseAm = pulseAm + 0.0005; //speed of pulse
if (pulseAm > 7.853)
pulseAm = 1.57; // position on sine wave
pulseInc = sin(pulseAm) * swing + 127 ; // swing = amount of swing down from max brightness i.e. a value 128 is off at bottom of swing
analogWrite(indLed, pulseInc);
}
void ledSwitchCall ()
{
byte ledSwitchState = digitalRead (ledSwitchPin); // if it has, see if switch is open or closed
if (ledSwitchState != oldLedSwitchState) // has it changed since last time it was read?
{
if (millis () - ledSwitchPressTime >= ledDebounceTime) { // has enough time passed with stable reading?
ledSwitchPressTime = millis (); // update the time
oldLedSwitchState = ledSwitchState; // remember the switch state for next time
if (ledSwitchState == LOW) // if switch passes debounce test and is pressed
{
ledBrightness = 0; // set brightness variable back to zero for next firing
ledSwitchFlag = true; // ok to do flash
}
}
}
}
void frontLed ()
{
if (ledSwitchFlag) // Ok to fade?
{
unsigned long timeNow = millis(); // set fader interval timer
if (timeNow >= (timeThen + 50)) // is it time to increase brightness?
{
timeThen = timeNow;
ledBrightness = ledBrightness + ledFadeAmount; // increment brightness value
if (ledBrightness >= ledBlip) // is the led bright enough to flash?
ledBrightness = ledBlip; // if so, set ledbrightness to flash value
analogWrite (frontRedLed, ledBrightness); // write new value to led
}
}
if (ledBrightness >= ledBlip && ledTimerStarted == false) // has the brightness reached set limit and is timer started?
{
digitalWrite (frontOrangeLed, HIGH); // if so, set red fader led to max brightness
digitalWrite (frontRedLed, HIGH); // and switch on orange led
ledBrightness = 0; // reset brightness back to zero for next time
ledTimer = millis(); // start brightness period timer
ledTimerStarted = true; // set brightness period timer flag to started
ledSwitchFlag = false; //fade complete
}
if ((millis() - ledTimer) > (random(minBeamOnTime, maxBeamOnTime)) && ledTimerStarted == true) {// has enough time passed at max brightness?
digitalWrite (frontOrangeLed, LOW); // if so, turn leds off
digitalWrite (frontRedLed, LOW);
ledTimerStarted = false; // set brightness period timer flag to not started
}
}
Many Thanks