Stap 4: Vereenvoudigde versie die geen EEPROM gebruikt
Als u op zoek bent naar een eenvoudigere oplossing voor het maken van een keuze in een menu met een knop hoeft te behouden van de selectie tijdens het opnieuw instellen van of macht cyclus, is hier het met de spullen van de EEPROM verwijderd:
#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