const byte pinSwitch = 8;
const byte pinsLED[] = {3, 4, 5, 6};
const size_t numLEDs = sizeof pinsLED / sizeof pinsLED[0];
const unsigned long functionIntervals[numLEDs] = {7900, 4800, 2700, 1100};
bool functionState[numLEDs] = {false};
unsigned long timestamp[numLEDs] = {0};
byte selection = 0;
byte lastSelection = 0;
bool lastSwitchState = HIGH;
void setup()
{
Serial.begin(9600);
for (byte i = 0; i < numLEDs; i++)
{
pinMode(pinsLED[i], OUTPUT);
}
pinMode (pinSwitch, INPUT_PULLUP);
}
void loop()
{
checkSelection();
setSelectionLED();
checkSwitch();
setFunction();
}
void setFunction()
{
for (byte i = 0; i < numLEDs; i++)
{
if (millis() - timestamp[i] >= functionIntervals[i])
{
functionState[i] = false;
}
if (functionState[i] == true)
{
switch (i)
{
case 0: function0(); break;
case 1: function1(); break;
case 2: function2(); break;
case 3: function3(); break;
}
}
}
}
void setSelectionLED()
{
if (lastSelection != selection)
{
for (byte i = 0; i < numLEDs; i++)
{
digitalWrite(pinsLED[i], LOW);
}
digitalWrite(pinsLED[selection], HIGH);
lastSelection = selection;
}
}
void checkSelection()
{
int readingPotentiometer = analogRead(A0);
switch (readingPotentiometer)
{
case 0 ... 255: selection = 0; break;
case 256 ... 511: selection = 1; break;
case 512 ... 767: selection = 2; break;
case 768 ...1023: selection = 3; break;
}
}
void checkSwitch()
{
bool switchState = digitalRead (pinSwitch);
if (switchState != lastSwitchState)
{
if (switchState == HIGH)
{
functionState[selection] = true;
timestamp[selection] = millis();
}
lastSwitchState = switchState;
}
}
void function0()
{
Serial.println("function0");
}
void function1()
{
Serial.println("function1");
}
void function2()
{
Serial.println("function2");
}
void function3()
{
Serial.println("function3");
}