One button / two functions...almost. Need help.

// Visual timer for lab operation. written 08042017 TEM

// First function initiated by short button press.
// Fading green light for 14 minutes.
// Yellow light for one minute>
// Persistant red light at fifteen minutes.
// Resettable cycle at any time by button press.

// Second function initiated by long button press.
// Second function blinks red light until short press restarts first function.


#include <Arduino.h>
int  redPin = 3 ;
int  grnPin = 5 ;
int  bluPin = 6 ;
int resetButton = 7;
int resetButtonVal = 0;
const long interval = 500; // 60000 for use as minute timer / 500 for testing
unsigned long previousMillis = 0;
int minuteCounterVal = 0;
int blinkCounterVal = 0;

void setup()
{
  pinMode(resetButton, INPUT);
  pinMode(redPin, OUTPUT);
  pinMode(grnPin, OUTPUT);
  pinMode(bluPin, OUTPUT);
  pinMode(4, OUTPUT);
  digitalWrite (4, HIGH);
}

void countDown()
{
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= (interval))
  {
    minuteCounterVal++;
    previousMillis = currentMillis;
  }
  if (minuteCounterVal < 5) {
    greenLight();
  }
  if ((minuteCounterVal >= 5) &&  (minuteCounterVal < 10)) {
    lightGreenLight();
  }
  if ((minuteCounterVal >= 10) &&  (minuteCounterVal < 15)) {
    veryLightgGreenLight();
  }
  if (minuteCounterVal == 15) {
    orangeLight();
  }
  if (minuteCounterVal > 16) {
    redLight();
  }
}
void greenLight()
{
  analogWrite(redPin, 255);
  analogWrite(grnPin, 0);
  analogWrite(bluPin, 255);
}
void lightGreenLight()
{
  analogWrite(redPin, 255);
  analogWrite(grnPin, 215);
  analogWrite(bluPin, 255);
}
void veryLightgGreenLight()
{
  analogWrite(redPin, 255);
  analogWrite(grnPin, 240);
  analogWrite(bluPin, 255);
}
void orangeLight()
{
  analogWrite(redPin, 230);
  analogWrite(bluPin, 255);
  analogWrite(grnPin, 230);
}
void redLight()
{
  analogWrite(redPin, 230);
  analogWrite(grnPin, 255);
  analogWrite(bluPin, 255);
}
void blueLight()
{
  analogWrite(redPin, 255);
  analogWrite(grnPin, 255);
  analogWrite(bluPin, 0);
}
void noLight()
{
  analogWrite(redPin, 255);
  analogWrite(bluPin, 255);
  analogWrite(grnPin, 255);
}
void loop()
{
  unsigned long currentMillis = millis();
  resetButtonVal = digitalRead (resetButton);

 // blinkCounterVal = digitalRead (resetButton);
 if ((resetButton == HIGH) && ((millis() - lastDebounceTime) > debounceDelay))
//  if (currentMillis - previousMillis >= (debounceDelay))
  {
    blueLight(); // Second function test stand-in.
    previousMillis = currentMillis;
  }
  
  if (resetButton == HIGH){
    resetButtonVal = 1;
    noLight();
  }
  else if (resetButton == LOW){
    resetButtonVal = 0;
  }
  if (resetButtonVal == 1){
    minuteCounterVal = 0;
    currentMillis = 0;
  }
  countDown(); // First function
}

I think you forgot to add something very important in your post: the question you have with this.

Question is. Although function one performs as expected, I have been on able to get function to working. Could someone help me do this?

wefinish:
What is your button default status? without pressed - High or Low?

may be you need this in your countdown function?

previousMillis = currentMillis;

My reset button is tide low with a 10 K resistor.

The currentMillis variable in the countdown function is a different variable to the currentMillis in the rest of the sketch. It goes out of scope when ever the that function ends.

All your testing of the time and changing the LEDs happens all the time, you only want it to happen when your timers change.

I just don't see what I need to change...

What you most need to change is HOW you look at code. Take a break, walk away and don't come back all set to get right back into the groove that failed you last time. Let your head rest and de-fixate on what or where you THINK the problems is, re-examine what you are doing if need be or just do something else until your left-brain lets go.

There is an example of only doing something on change only. If minute does not change then return.
This below still needs work because it sets the same colors more than once in a row but that may do no harm depending on what the color setting routines do especially with time variables.

void countDown()
{
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= (interval))
  {
    minuteCounterVal++;
    previousMillis = currentMillis;
  }
  else
  {
    return;
  }

  if (minuteCounterVal < 5) {
    greenLight();
  }
  else if ((minuteCounterVal >= 5) &&  (minuteCounterVal < 10)) 
  {
    lightGreenLight();
  }
  else   if ((minuteCounterVal >= 10) &&  (minuteCounterVal < 15)) 
  {
    veryLightgGreenLight();
  }
  else if (minuteCounterVal >= 15) 
  {
    orangeLight();
  }
  else
  {
    redLight();
  }
}

I have incorporated switchmanager.h in order to handle button presses. The button behavior now works. However I have lost the timer functionality allowing the LED to run through the color stages. It appears I am not able to use a function inside this format. Could someone recommend a fix?

#include <Arduino.h>
#include <SwitchManager.h>

// pin assignments
int resetButton = 6;
int  redPin = 3 ;
int  grnPin = 10 ;
int  bluPin = 11 ;

SwitchManager mySwitch;

// newState will be LOW or HIGH (the is the state the switch is now in)
// interval will be how many ms between the opposite state and this one
// whichPin will be which pin caused this change (so you can share the function amongst multiple switches)

void handleSwitchPress (const byte newState, const unsigned long interval, const byte resetButton)
{
  if (newState == LOW)
  {
    //     noLight();
    // digitalWrite (bluPin, LOW);
    //  digitalWrite (grnPin, LOW);
    return;
  }

  // switch must be HIGH
  if (interval >= 1000)
    orangeLight();
  //  digitalWrite (bluPin, HIGH);  //countDown
  else
    countDown();
  //veryLightgGreenLight();
  //  digitalWrite (grnPin, HIGH);  // blinkRed

}  // end of handleSwitchPress

unsigned long previousMillis = 0;        // will store last time LED was updated
int minuteCounterVal = 0;           // interval at which to blink (milliseconds)









void setup ()
{
  // pinMode(redPin, OUTPUT);
  pinMode(grnPin, OUTPUT);
  pinMode(bluPin, OUTPUT);
}
void greenLight()
{
  analogWrite(redPin, 255);
  analogWrite(grnPin, 0);
  analogWrite(bluPin, 255);
}
void lightGreenLight()
{
  analogWrite(redPin, 255);
  analogWrite(grnPin, 215);
  analogWrite(bluPin, 255);
}
void veryLightgGreenLight()
{
  analogWrite(redPin, 255);
  analogWrite(grnPin, 240);
  analogWrite(bluPin, 255);
}
void orangeLight()//yep
{
  analogWrite(redPin, 230);
  analogWrite(bluPin, 255);
  analogWrite(grnPin, 180);
}
void redLight()//yep
{
  analogWrite(redPin, 230);
  analogWrite(grnPin, 255);
  analogWrite(bluPin, 255);
}
void noLight()
{
  analogWrite(redPin, 255);
  analogWrite(bluPin, 255);
  analogWrite(grnPin, 255);
}
void countDown()
{
  minuteCounterVal = 0;
  const long interval = 10;           // interval at which to blink (milliseconds) was 1000
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= (500))
  {
    minuteCounterVal++;
    previousMillis = currentMillis;
  }
  if (minuteCounterVal < 5) {
    greenLight();
  }
  if ((minuteCounterVal >= 5) &&  (minuteCounterVal < 10)) {
    lightGreenLight();
  }
  if ((minuteCounterVal >= 10) &&  (minuteCounterVal < 15)) {
    veryLightgGreenLight();
  }
  if (minuteCounterVal == 15) {
    orangeLight();
  }
  if (minuteCounterVal > 16) {
    redLight();
  }
}

void loop ()
{
  mySwitch.begin (resetButton, handleSwitchPress);
  mySwitch.check ();  // check for presses

  // do other stuff here
}

The countdown function only runs when?

Here.

void handleSwitchPress (const byte newState, const unsigned long interval, const byte resetButton)
{
  if (newState == LOW)
  {
    //     noLight();
    // digitalWrite (bluPin, LOW);
    //  digitalWrite (grnPin, LOW);
    return;
  }

  // switch must be HIGH
  if (interval >= 1000)
    orangeLight();
  //  digitalWrite (bluPin, HIGH);  //countDown
  else
    countDown();                                      <<<<<<<<<<<<<<<<<<<<
  //veryLightgGreenLight();
  //  digitalWrite (grnPin, HIGH);  // blinkRed

}  // end of handleSwitchPress

I saw your code above the first time. That question was to get you to THINK.

The countdown function has to run over and over to work. It always needs to start with previous millis and the minute counter initialized.

And why do you have that begin() function in loop()? Shouldn't it be in setup()?

void loop ()
{
mySwitch.begin (resetButton, handleSwitchPress);