// Sketch for testing switch between loops for LED patterns v2
unsigned long lastMilli = 0;
byte mode = 0;
unsigned long lastCheck = 0;
int intervalDebounce = 50;
int ledPin = 9; // the number of the LED pin
int pinSwitch = 2;
int ledState = LOW; // ledState used to set the LED
long Mode1OnTime = 500; // milliseconds of on-time
int Mode1OnIncrement = 100; // increment on time in milliseconds
int M1OnTimeMax = 1000 - Mode1OnIncrement; // Mode 1 max on time before cycling back to start
long Mode2OnTime = 1000; // milliseconds of on-time
long Mode2OnIncrement = 1000; // increment on time in milliseconds
int M2OnTimeMax = 5000 - Mode2OnIncrement; // Mode 2 max on time before cycling back to start
const unsigned long MsecLedOff = 500; // milliseconds of off-time
long M1OnTimeCurr = Mode1OnTime; // initialize variable for Mode 1 current OnTime incrementing
long M2OnTimeCurr = Mode2OnTime; // initialize variable for Mode 2 current OnTime incrementing
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(pinSwitch, INPUT_PULLUP);
// initialize serial communication:
Serial.begin(9600);
}
void loop()
{
if (millis() - lastCheck >= intervalDebounce) {
lastCheck = millis();
if (digitalRead(pinSwitch) == HIGH) {
if (mode != 1) {
Serial.println("Mode 1");
mode = 1;
}
}
else {
if (mode != 2) {
Serial.println("Mode 2");
mode = 2;
}
}
}
switch (mode) {
case 1: mode1(); break;
case 2: mode2(); break;
}
}
// mode 1 pattern
void mode1() {
M2OnTimeCurr = Mode2OnTime; // reset Mode 1 cycle for it's next start
// check to see if it's time to change the state of the LED
unsigned long currentMillis = millis();
if((ledState == HIGH) && (millis() - lastMilli >= M1OnTimeCurr))
{
ledState = LOW; // Turn it off
lastMilli = currentMillis; // Remember the time
//Serial.print ("mode 1 off time: ");
//Serial.println (MsecLedOff);
digitalWrite(ledPin, ledState); // Update the actual LED
// increment on time up to Max
if((M1OnTimeCurr <= M1OnTimeMax)) {
M1OnTimeCurr = M1OnTimeCurr + Mode1OnIncrement;
}
else
M1OnTimeCurr = Mode1OnTime; // after Max cycle set back to preset
}
else if ((ledState == LOW) && (currentMillis - lastMilli >= MsecLedOff))
{
ledState = HIGH; // turn it on
lastMilli = currentMillis; // Remember the time
Serial.print ("mode 1 on time: ");
Serial.println (M1OnTimeCurr);
digitalWrite(ledPin, ledState); // Update the actual LED
}
}
// mode 2 pattern
void mode2() {
// check to see if it's time to change the state of the LED
unsigned long currentMillis = millis();
if((ledState == HIGH) && (currentMillis - lastMilli >= M2OnTimeCurr))
{
ledState = LOW; // Turn it off
lastMilli = currentMillis; // Remember the time
//Serial.print ("mode 2 off time: ");
//Serial.println (MsecLedOff);
digitalWrite(ledPin, ledState); // Update the actual LED
// increment on time up to Max
if((M2OnTimeCurr <= M2OnTimeMax)) {
M2OnTimeCurr = M2OnTimeCurr + Mode2OnIncrement;
}
else
M2OnTimeCurr = Mode2OnTime; // after Max cycle set back to preset
}
else if ((ledState == LOW) && (currentMillis - lastMilli >= MsecLedOff))
{
ledState = HIGH; // turn it on
lastMilli = currentMillis; // Remember the time
Serial.print ("mode 2 on time: ");
Serial.println (M2OnTimeCurr);
digitalWrite(ledPin, ledState); // Update the actual LED
}
}