millis() timer working on first two functions not on others

Hey so you guys helped me along with my project significantly with another issue I was experiencing. At this point I have one last hurtle. I have a total of four millis9) timers setup as function to call in the event of them being needed. The first two are working perfectly. However my second two never get past the first IF statement. I have looked over this code and looked at several examples and I can't find my mistake, which I'm sure is small. Here is my code. To me they look identical and I can't find even the smallest difference. I have even coppied the exact code from blink2 and pasted it into timer1. Any ideas?

int blink2() {
    if (currentMillis - previousMillis2 >= interval) {
    // save the last time you blinked the LED
    previousMillis2 = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState2 == LOW){ 
      ledState2 = HIGH; }
     else {       
      ledState2 = LOW;}
    

    // set the LED with the ledState of the variable:
    digitalWrite(ledpin2, ledState2); 
    }
}

int timer1() {
    if (currentMillis - heater1millis >= five) {
    // save the last time you blinked the LED
    heater1millis = currentMillis;
    Serial.print("timer ran out - resetting");

    // if the relay is off turn it on and vice-versa:
    if (heaterstate1 == LOW){ 
      heaterstate1 = HIGH; }
     else {       
      heaterstate1 = LOW;}
    

    // set the relay with the heaterstate1 of the variable:
    digitalWrite(relay1, heaterstate1);
    Serial.print("writing heaterstate1 to relay1");
    }

Bit of a guess here, but there's already a timer1 afaik, so try to call them something else?

I just did a search through it and its the only one.

Please show us your complete sketch.

BTW, these functions do not return anything so make them 'void'

Edit:

if (heaterstate1 == LOW)
{ 
  heaterstate1 = HIGH; 
}

else 
{       
  heaterstate1 = LOW;
}

Can be replaced by:

heaterstate1 = !heaterstate1;

.

#define buttonPin 9
#define buttonpin2 8
#define ledpin1 7          // digital output pin for LED 1
#define ledpin2 6
#define relay1 12
#define relay2 11

int ledState1 = LOW;
int ledState2 = LOW;
int heaterstate1 = LOW;
int heaterstate2 = LOW;
boolean blinkstate1 = false;
boolean blinkstate2 = false;
boolean blinkfailsafe1 = false;
boolean blinkfailsafe2 = false;

unsigned long previousMillis1 = 0; 
unsigned long previousMillis2 = 0; 
unsigned long heater1millis = 0; 
unsigned long heater2millis = 0; 

const long interval = 1000;
const long five = 300000;

unsigned long currentMillis;




void setup() {
   // Set button input pin
   pinMode(buttonPin, INPUT);
   pinMode(buttonpin2, INPUT);
   pinMode(relay1, OUTPUT);
   pinMode(relay2, OUTPUT);
   pinMode(ledpin1, OUTPUT);  
   pinMode(ledpin2, OUTPUT);

   boolean blinkstate1 = false;
   boolean blinkstate2 = false;
   boolean blinkfailsafe1 = false;
   boolean blinkfailsafe2 = false;
   Serial.begin(9600);

   

   

   

}
 

void loop() {
   currentMillis = millis();
   // Get button event and act accordingly
   int b = checkButton();

   if (b == 1) clickEvent();
   if (b == 2) doubleClickEvent();
   if (b == 3) holdEvent();
   if (b == 4) longHoldEvent();

   int b2 = checkButton2();
   if (b2 == 1) clickEvent2();
   if (b2 == 2) doubleClickEvent2();
   if (b2 == 3) holdEvent2();
   if (b2 == 4) longHoldEvent2();
   
   if ((blinkstate1 == true)&&(blinkfailsafe1 == false)) blink1();
   if ((blinkstate2 == true)&&(blinkfailsafe2 == false)) blink2();
}

//=================================================
// Events to trigger

void clickEvent() {
   blinkstate1 = true;
   digitalWrite(relay1, HIGH);
   int timer1();
   Serial.print("beginning timer1");
  
                         
}
void doubleClickEvent() {
   digitalWrite(relay1, LOW);
   digitalWrite(ledpin1, LOW);
   blinkstate1 = false;
   blinkfailsafe1 = false;
}
void holdEvent() {
   digitalWrite(relay1, HIGH);
   digitalWrite(ledpin1, HIGH);
   blinkfailsafe1 = true;
}
void longHoldEvent() {
   digitalWrite(relay1, LOW);
   digitalWrite(ledpin1, LOW);
   blinkstate1 = false;
   blinkfailsafe1 = false;
}


void clickEvent2() {       //click event 2 for second button
   blinkstate2 = true;
   
}
void doubleClickEvent2() {
   digitalWrite(relay2, LOW);
   digitalWrite(ledpin2, LOW);
   blinkstate2 = false;
   blinkfailsafe2 = false;
}
void holdEvent2() {
   digitalWrite(relay2, HIGH);
   digitalWrite(ledpin2, HIGH);
   blinkfailsafe2 = true;
}
void longHoldEvent2() {
   digitalWrite(relay2, LOW);
   digitalWrite(ledpin2, LOW);
   blinkstate2 = false;
   blinkfailsafe2 = false;
}
//=================================================
//  MULTI-CLICK:  One Button, Multiple Events

part1

// Button timing variables
int debounce = 20;          // ms debounce period to prevent flickering when pressing or releasing the button
int DCgap = 250;            // max ms between clicks for a double click event
int holdTime = 1000;        // ms hold period: how long to wait for press+hold event
int longHoldTime = 3000;    // ms long hold period: how long to wait for press+hold event

// Button variables
boolean buttonVal = HIGH;   // value read from button
boolean buttonLast = HIGH;  // buffered value of the button's previous state
boolean DCwaiting = false;  // whether we're waiting for a double click (down)
boolean DConUp = false;     // whether to register a double click on next release, or whether to wait and click
boolean singleOK = true;    // whether it's OK to do a single click
long downTime = -1;         // time the button was pressed down
long upTime = -1;           // time the button was released
boolean ignoreUp = false;   // whether to ignore the button release because the click+hold was triggered
boolean waitForUp = false;        // when held, whether to wait for the up event
boolean holdEventPast = false;    // whether or not the hold event happened already
boolean longHoldEventPast = false;   // whether or not the long hold event happened already

//second set of button vars

boolean buttonVal2 = HIGH;   // value read from button
boolean buttonLast2 = HIGH;  // buffered value of the button's previous state
boolean DCwaiting2 = false;  // whether we're waiting for a double click (down)
boolean DConUp2 = false;     // whether to register a double click on next release, or whether to wait and click
boolean singleOK2 = true;    // whether it's OK to do a single click
long downTime2 = -1;         // time the button was pressed down
long upTime2 = -1;           // time the button was released
boolean ignoreUp2 = false;   // whether to ignore the button release because the click+hold was triggered
boolean waitForUp2 = false;        // when held, whether to wait for the up event
boolean holdEventPast2 = false;    // whether or not the hold event happened already
boolean longHoldEventPast2 = false;

// blink led for 10 on and 5 off mode
    


int blink1() {
    if (currentMillis - previousMillis1 >= interval) {
    // save the last time you blinked the LED
    previousMillis1 = currentMillis;

    
    if (ledState1 == LOW){ 
      ledState1 = HIGH; }
     else {       
      ledState1 = LOW;}
    

    // set the LED with the ledState of the variable:
    digitalWrite(ledpin1, ledState1);
  }
}
  



int blink2() {
    if (currentMillis - previousMillis2 >= interval) {
    // save the last time you blinked the LED
    previousMillis2 = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState2 == LOW){ 
      ledState2 = HIGH; }
     else {       
      ledState2 = LOW;}
    

    // set the LED with the ledState of the variable:
    digitalWrite(ledpin2, ledState2); 
    }
}

int timer1() {
    if (currentMillis - heater1millis >= five) {
    // save the last time you blinked the LED
    heater1millis = currentMillis;
    Serial.print("timer ran out - resetting");

    // if the LED is off turn it on and vice-versa:
    if (heaterstate1 == LOW){ 
      heaterstate1 = HIGH; }
     else {       
      heaterstate1 = LOW;}
    

    // set the LED with the ledState of the variable:
    digitalWrite(relay1, heaterstate1);
    Serial.print("writing heaterstate1 to relay1");
    }
}







int checkButton() {   
   int event = 0;
   buttonVal = digitalRead(buttonPin);
   // Button pressed down
   if (buttonVal == LOW && buttonLast == HIGH && (millis() - upTime) > debounce)
   {
       downTime = millis();
       ignoreUp = false;
       waitForUp = false;
       singleOK = true;
       holdEventPast = false;
       longHoldEventPast = false;
       if ((millis()-upTime) < DCgap && DConUp == false && DCwaiting == true)  DConUp = true;
       else  DConUp = false;
       DCwaiting = false;
   }
   // Button released
   else if (buttonVal == HIGH && buttonLast == LOW && (millis() - downTime) > debounce)
   {       
       if (not ignoreUp)
       {
           upTime = millis();
           if (DConUp == false) DCwaiting = true;
           else
           {
               event = 2;
               DConUp = false;
               DCwaiting = false;
               singleOK = false;
           }
       }
   }
   // Test for normal click event: DCgap expired
   if ( buttonVal == HIGH && (millis()-upTime) >= DCgap && DCwaiting == true && DConUp == false && singleOK == true && event != 2)
   {
       event = 1;
       DCwaiting = false;
   }
   // Test for hold
   if (buttonVal == LOW && (millis() - downTime) >= holdTime) {
       // Trigger "normal" hold
       if (not holdEventPast)
       {
           event = 3;
           waitForUp = true;
           ignoreUp = true;
           DConUp = false;
           DCwaiting = false;
           //downTime = millis();
           holdEventPast = true;
       }
       // Trigger "long" hold
       if ((millis() - downTime) >= longHoldTime)
       {
           if (not longHoldEventPast)
           {
               event = 4;
               longHoldEventPast = true;
           }
       }
   }
   buttonLast = buttonVal;
   return event;
}
   //second checkbutton

int checkButton2() {   
   int event2 = 0;
   buttonVal2 = digitalRead(buttonpin2);
   // Button pressed down
   if (buttonVal2 == LOW && buttonLast2 == HIGH && (millis() - upTime2) > debounce)
   {
       downTime2 = millis();
       ignoreUp2 = false;
       waitForUp2 = false;
       singleOK2 = true;
       holdEventPast2 = false;
       longHoldEventPast2 = false;
       if ((millis()-upTime2) < DCgap && DConUp2 == false && DCwaiting2 == true)  DConUp2 = true;
       else  DConUp2 = false;
       DCwaiting2 = false;
   }
   // Button released
   else if (buttonVal2 == HIGH && buttonLast2 == LOW && (millis() - downTime2) > debounce)
   {       
       if (not ignoreUp2)
       {
           upTime2 = millis();
           if (DConUp2 == false) DCwaiting2 = true;
           else
           {
               event2 = 2;
               DConUp2 = false;
               DCwaiting2 = false;
               singleOK2 = false;
           }
       }
   }
   // Test for normal click event: DCgap expired
   if ( buttonVal2 == HIGH && (millis()-upTime2) >= DCgap && DCwaiting2 == true && DConUp2 == false && singleOK2 == true && event2 != 2)
   {
       event2 = 1;
       DCwaiting2 = false;
   }
   // Test for hold
   if (buttonVal2 == LOW && (millis() - downTime2) >= holdTime) {
       // Trigger "normal" hold
       if (not holdEventPast2)
       {
           event2 = 3;
           waitForUp2 = true;
           ignoreUp2 = true;
           DConUp2 = false;
           DCwaiting2 = false;
           //downTime2 = millis();
           holdEventPast2 = true;
       }
       // Trigger "long" hold
       if ((millis() - downTime2) >= longHoldTime)
       {
           if (not longHoldEventPast2)
           {
               event2 = 4;
               longHoldEventPast2 = true;
           }
       }
   }
   buttonLast2 = buttonVal2;
   return event2;
}

part2

Its about half ways down the second part.

To trouble shoot this, place some print statements at strategic locations to prove values are what you think they are/should be.

For example only:
if ((blinkstate2 == true) && (blinkfailsafe2 == false)) blink2();

Serial.print("blinkstate2 = ");
Serial.println(blinkstate2");

Serial.print("blinkfailsafe2 =) ";
Serial.println(blinkfailsafe2);

etc.

BTW

if (b == 1) clickEvent();
if (b == 2) doubleClickEvent();
if (b == 3) holdEvent();
if (b == 4) longHoldEvent();

Recommend you use:

if (b == 1) clickEvent();
else if (b == 2) doubleClickEvent();
else if (b == 3) holdEvent();
else if (b == 4) longHoldEvent();

Or use switch/case.

LarryD:
To trouble shoot this, place some print statements at strategic locations to prove values are what you think they are/should be.

For example only:
if ((blinkstate2 == true) && (blinkfailsafe2 == false)) blink2();

Serial.print("blinkstate2 = ");
Serial.println(blinkstate2");

Serial.print("blinkfailsafe2 =) ";
Serial.println(blinkfailsafe2);

etc.

YESSS thanks! I had int in front of my function so I made a new int instead of actually calling my function. I was wondering how to serial print values of variables.

void clickEvent()
{
  blinkstate1 = true;
  digitalWrite(relay1, HIGH);
  int timer1() ;
  Serial.print("beginning timer1");
}

int timer1() ; <-------------<<<< what are you doing here?

Congrats, you are an expert now :wink:

Edit:
In the future, you can attach an .INO file to a post so you don't have to split your sketch into two parts.

Recommend you keep { and } on separate lines, use CTRL T to format your code and use this style for 'if'

if(x == y)
{
z = z +1;
}
Rather then:
if(x == y) z = z +1;

.

Thats a helpful hint. Thanks again I finally finished up this code now my fiance can have her new heated seats!