const byte PinBut = A1;
byte butState;
const byte PinLeds [] = { 13, 12, 11, 10 };
const int Nled = sizeof(PinLeds);
enum { Off = LOW, On = HIGH };
// -------------------------------------------------------------
void mode0() {
for (int n = 0; n < Nled; n++)
digitalWrite (PinLeds [n], Off);
}
void mode1() {
for (int n = 0; n < Nled; n++)
digitalWrite (PinLeds [n], ! digitalRead (PinLeds [n]));
}
void mode2() {
static int idx = 0;
digitalWrite (PinLeds [idx], Off);
if (Nled <= ++idx)
idx = 0;
digitalWrite (PinLeds [idx], On);
}
// -------------------------------------------------------------
int mode;
const int Nmode = 3;
unsigned long msec0;
void loop () {
unsigned long msec = millis();
if (msec - msec0 >= 500) {
msec0 = msec;
switch (mode) {
case 2:
mode2 ();
break;
case 1:
mode1 ();
break;
default:
mode0 ();
break;
}
}
byte but = digitalRead (PinBut);
if (butState != but) {
butState = but;
delay (20);
if (LOW == but) {
if (Nmode <= ++mode)
mode = 0;
Serial.println (mode);
}
}
}
void setup() {
Serial.begin (9600);
pinMode (PinBut, INPUT_PULLUP);
butState = digitalRead (PinBut);
for (int n = 0; n < Nled; n++) {
pinMode (PinLeds [n], OUTPUT);
}
}