// Sequencing counter, save count after power outage
// https://forum.arduino.cc/t/sequencing-counter-save-count-after-power-outage/1414317
const char * compiledOn = "\"" __FILE__ "\" compiled the " __DATE__ " at " __TIME__;
#include<EEPROM.h>
const int countPin = 2;
const int resetPin = 3;
const int LED0 = 13;
const int LED1 = 12;
const int LED2 = 11;
const int LED3 = 10;
const int LED4 = 9;
const int LED5 = 8;
const int LED6 = 7;
const int LED7 = 6;
const int LED8 = 5;
const int LED9 = 4;
#define nLED 10
byte LEDPinArray[nLED] = { LED0, LED1, LED2, LED3, LED4,
LED5, LED6, LED7, LED8, LED9
};
uint8_t ledIndex = 0;
int resetState = 0;
int lastResetState = 0;
int buttonState = 0;
int lastButtonState = 0;
//void initLeds
void initLeds()
{
for (int i = 0; i < nLED; i++) {
pinMode(LEDPinArray[i], OUTPUT);
}
}
// void select
void Select(uint8_t index)
{
EEPROM.put(10, index);
Serial.print("Seleted LED index: ");
Serial.println(index);
for (uint8_t i = 0; i < nLED; i++) {
// if (i == index) {
// digitalWrite(LEDPinArray[i], HIGH);
// }
// else {
// digitalWrite(LEDPinArray[i], LOW);
// }
digitalWrite(LEDPinArray[i], i == index);
}
}
// void setup
void setup()
{
initLeds();
Serial.begin(115200);
Serial.println(compiledOn);
pinMode(countPin, INPUT);
pinMode(resetPin, INPUT);
EEPROM.get(10, ledIndex);
Serial.print("Retrieved LED index: ");
Serial.println(ledIndex);
if (ledIndex < 0 || ledIndex >= nLED) {
ledIndex = 0;
}
Serial.print("Adjuted LED index: ");
Serial.println(ledIndex);
Select(ledIndex);
}
//void loop
void loop()
{ // reset function
resetState = digitalRead(resetPin);
if (resetState != lastResetState) {
lastResetState = resetState;
Serial.println("Reset Btn Pressed");
if (resetState == HIGH) {
ledIndex = 0;
Select(ledIndex);
}
}
//button press function
buttonState = digitalRead(countPin);
if (buttonState != lastButtonState) {
lastButtonState = buttonState;
Serial.println("Count Btn Pressed");
if (buttonState == HIGH) {
ledIndex++;
if (ledIndex >= nLED) {
ledIndex = 0;
}
Select(ledIndex);
}
delay(50);
}
}