I was playing around with structures a while back.
Maybe it will give you some ideas or you can incorporate this into your sketch.
//Blink without Delay skeleton (using a structure)
//LarryD
struct timer
{
//lastMillis = the time this "timer" started
//waitMillis = delay time (mS) we are looking for
//restart = do we start this "timer" again
//enableFlag = is this "timer" (enabled) allowed to be accessed
//**********************
//For each timer needed:
//Example:
// timer myTimer =
// {
// 0, 200, true, true //lastMillis, waitMillis, restart, enableFlag
// };
//public access to: myTimer.lastMillis, myTimer.waitMillis, myTimer.restart, myTimer.enableFlag
//**********************
unsigned long lastMillis;
unsigned long waitMillis;
bool restart;
bool enableFlag;
bool CheckTime() //Delay time expired function "CheckTime()"
{
//is the time up for this task?
if (enableFlag && millis() - lastMillis >= waitMillis)
{
//should this start again?
if(restart)
{
//get ready for the next iteration
lastMillis += waitMillis;
}
//time was reached
return true;
}
//time was not reached
return false;
} //END of CheckTime()
}; //END of structure
//**********************************************************************
//Creating all my timer objects
timer pin13 =
{
0, 200, true, true //lastMillis, waitMillis, restart, enableFlag
};
//**********************
timer pin12 =
{
0, 3000, true, true //lastMillis, waitMillis, restart, enableFlag
};
//**********************
timer pin11 =
{
0, 10000, true, true //lastMillis, waitMillis, restart, enableFlag
};
//**********************
timer pin10 =
{
0, 6000, true, true //lastMillis, waitMillis, restart, enableFlag
};
//**********************
timer Toggle10 =
{
0, 50, true, true //lastMillis, waitMillis, restart, enableFlag
};
//**********************
timer checkSwitches =
{
0, 50, true, true //lastMillis, waitMillis, restart, enableFlag
};
//**********************************************************************
byte lastMySwitchState = 1; //Pin 9
byte counter = 0;
const byte Pin13 = 13;
const byte Pin12 = 12;
const byte Pin11 = 11;
const byte Pin10 = 10;
const byte Pin9 = 9;
const byte mySwitch = 2;
//**********************************************************************
void setup()
{
Serial.begin(9600);
pinMode(Pin13,OUTPUT);
pinMode(Pin12,OUTPUT);
pinMode(Pin11,OUTPUT);
pinMode(Pin10,OUTPUT);
pinMode(Pin9, OUTPUT);
digitalWrite(Pin13,LOW); //Start out OFF/LOW
digitalWrite(Pin12,LOW);
digitalWrite(Pin11,LOW);
digitalWrite(Pin10,LOW);
digitalWrite(Pin9, LOW);
pinMode(mySwitch,INPUT_PULLUP);
} // >>>>>>>>>>>>>> E N D O F s e t u p ( ) <<<<<<<<<<<<<<<<<
void loop()
{
//Below are examples demonstrating different timing situations
//***************************
//toggle Pin13 every 200ms
if (pin13.CheckTime())
{
//Toggle Pin13
digitalWrite(Pin13,!digitalRead(Pin13));
//if you only want this section of code to happen once
//uncomment the next line
//pin13.enableFlag = false;
}
//***************************
//after 3 seconds, Pin12 goes and stays HIGH
if (pin12.CheckTime())
{
//Pin12 HIGH now
digitalWrite(Pin12,HIGH);
pin12.enableFlag = false;
}
//***************************
//Pin11 is HIGH for 10 seconds, then goes and stays LOW
if (pin11.enableFlag && !pin11.CheckTime())
{
digitalWrite(Pin11,HIGH);
}
//10 seconds is now up now, leave the Pin11 LOW
else
{
digitalWrite(Pin11,LOW);
//disable above code
pin11.enableFlag = false;
}
//***************************
//for 6 seconds, toggle Pin10
if (pin10.enableFlag && !pin10.CheckTime())
{
//Toggling Pin10 every 50mS
if(Toggle10.CheckTime())
{
digitalWrite(Pin10,!digitalRead(Pin10));
}
}
//6 seconds is now up, stop toggling, leave Pin10 LOW
else
{
digitalWrite(Pin10,LOW);
//disable
pin10.enableFlag = false;
}
//***************************
//is it time to check the switches?
if (checkSwitches.CheckTime())
{
Switches();
}
//**********************************
//Put other non-blocking stuff here
//**********************************
} // >>>>>>>>>>>>>> E N D O F l o o p ( ) <<<<<<<<<<<<<<<<<
//======================================================================
// F U N C T I O N S
//======================================================================
//**********************************************************************
//no minimum switch press time is validated with this code (i.e. No glitch filter)
void Switches()
{
boolean thisState; //re-usable for all the switches
//*************************** Pin 9 code
//check if this switch has changed state
thisState = digitalRead(mySwitch);
if (thisState != lastMySwitchState)
{
//update the switch state
lastMySwitchState = thisState;
//This switch position has changed so do some stuff
//"HIGH condition code"
//switch goes from LOW to HIGH
if(thisState == HIGH)
{
//example: LED on Pin9 is Push ON, Push OFF
digitalWrite(Pin9,!digitalRead(Pin9));
}
//"LOW condition code"
//switch goes from HIGH to LOW
else
{
//example: display the current switch push count
Serial.println(++counter);
}
} //END of mySwitch (Pin 9) code
//******************************************
//similar code for other switches goes here
//******************************************
} //END of Switches()
//**********************************************************************
//======================================================================
// E N D O F C O D E
//======================================================================