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");
}
// 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;
}
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.