Étape 4: Version simplifiée qui n’utilise pas d’EEPROM
Si vous êtes à la recherche d’une solution plus simple pour faire une sélection de menu par un bouton et n’avez pas besoin de conserver la sélection au cours de la réinitialisation ou cycle d’alimentation, c’est ici avec les trucs d’EEPROM supprimé :
#define NUMSELECTIONS 4 // global variables int currentSelection = 0; boolean button1Pressed = 0; boolean previousButton1Pressed = 0; unsigned long debounceCount; unsigned long blinkMillis; unsigned int blinkInterval; void setup() { pinMode(12, INPUT_PULLUP); pinMode(2, OUTPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT); pinMode(5, OUTPUT); pinMode(13, OUTPUT); digitalWrite(2, 1); digitalWrite(3, 1); digitalWrite(4, 1); digitalWrite(5, 1); digitalWrite(13, 1); // turn on the LED for the selection digitalWrite(currentSelection + 2, 0); } // end of setup void loop() { // read the button no more often than once every 20 ms, debouncing if (millis() > 20 && debounceCount < millis() - 20) { debounceCount = millis(); button1Pressed = !digitalRead(12); } // if the button has been pressed, increment and store selection if (previousButton1Pressed != button1Pressed) { previousButton1Pressed = button1Pressed; // only take action when button is pressed, not when released if (button1Pressed) { // turn off the LED for the old selection digitalWrite(currentSelection + 2, 1); currentSelection++; currentSelection %= NUMSELECTIONS; // turn on the LED for the new selection digitalWrite(currentSelection + 2, 0); // Serial.print("Current Selection "); // Serial.println(currentSelection); } } // Here in the loop is where we can do different things depending on // what selection was made with the button. // In this case we blink the Arduino on-board LED at different intervals // depending on the selection. // This also demonstrates main loop processing without delay blocking. if (currentSelection == 0) { // Dim the LED by blinking it rapidly, bit-banging PWM if (digitalRead(13)) { blinkInterval = 0; } else { blinkInterval = 10; } } else if (currentSelection == 1) { // Blink once per second blinkInterval = 500; } else if (currentSelection == 2) { // Blink every 2 seconds blinkInterval = 1000; } else if (currentSelection == 3) { // Quick flash every 1.5 seconds, this is also bit-banging PWM, very slow if (digitalRead(13)) { blinkInterval = 50; } else { blinkInterval = 1450; } } if (millis() > blinkInterval && blinkMillis < millis() - blinkInterval) { blinkMillis = millis(); digitalWrite(13, !digitalRead(13)); } } // end of loop