#include<EEPROM.h>
#define EEPROM_ADDR 0
const int switchPin = 2; // mode selection switch pin
const int modePin = 3; // selects momentary switch or toggle switch
const int ledPin = 13; // Onboard LED for Testing
int ledState = HIGH; // the current state of the output pin
int buttonState = HIGH; // the current reading from the input pin
int lastButtonState = HIGH; // the previous reading from the input pin
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 20; // the debounce time; increase if the output flickers
unsigned long lastRunTime = 0;
unsigned long stateMachineTime = 100;
bool switchState = false;
bool redraw = false;
byte stateCount = 0;
void setup() {
Serial.begin(115200);
pinMode(switchPin, INPUT_PULLUP);
pinMode(modePin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
// set initial LED state
digitalWrite(ledPin, ledState);
buttonState = digitalRead(switchPin);
lastButtonState = digitalRead(switchPin);
//Serial.print("EEPROM length: ");
//Serial.println(EEPROM.length());
stateCount = EEPROM.read(EEPROM_ADDR);
redraw = true;
}
void loop() {
Check_Switch();
State_Machine();
}
void State_Machine(){
if (millis() - lastRunTime > stateMachineTime) {
lastRunTime = millis();
switch (stateCount) {
case 0:
if (redraw) {
EEPROM.update(EEPROM_ADDR,stateCount);
Serial.println("oil");
redraw = false;
}
Serial.println("test oil data");
break;
case 1:
if (redraw) {
EEPROM.update(EEPROM_ADDR,stateCount);
Serial.println("Water");
redraw = false;
}
Serial.println("test water data");
break;
case 2:
if (redraw) {
EEPROM.update(EEPROM_ADDR,stateCount);
Serial.println("Transmission");
redraw = false;
}
Serial.println("test transmission data");
break;
case 3:
if (redraw) {
EEPROM.update(EEPROM_ADDR,stateCount);
Serial.println("Other");
redraw = false;
}
Serial.println("test other data");
break;
default: //revert to 0
stateCount = 0;
}
}
}
void Check_Switch()
{
int reading = digitalRead(switchPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
lastButtonState = reading;
}
if ((millis() - lastDebounceTime) > debounceDelay && !switchState) {
if (reading != buttonState) {
buttonState = reading;
ledState = !ledState;
redraw = true;
stateCount++;
digitalWrite(ledPin, ledState);
//Serial.println(stateCount);
}
}
}