I want to add a fail safe sort of thing of initial power up.
At the moment I'm only detecting 2 buttons but will need add another 2. So if a button is pressed on power up it stops it from going to the main loop and jumps to error loop where I flash an LED.
I'm only debugging it and just print data to serial port to show that the loop is working.
This is working but was wondering if it could be improved or more of an elegant way of doing it ?
#define FWD_input 3 //forward signal from joystick
#define BCK_input 4 //reverse signal from joystick
#define Error_led A3 //Error LED on power up
int ledState = LOW;
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change:
const long interval = 250;
void setup() {
pinMode(Error_led, OUTPUT);
digitalWrite(Error_led, LOW);
pinMode(FWD_input, INPUT_PULLUP);
pinMode(BCK_input, INPUT_PULLUP);
Serial.begin(9600);
Serial.print("TEST POWER UP");
//Detect if any buttons are pressed on start up fail saafe if so jump to error loop
while ((digitalRead(FWD_input) == LOW) || (digitalRead(BCK_input) == LOW)) {
Error();
}
}
void loop() {
Serial.println("NO ERROR"); // Just print this so you can see loop working
delay(1000);
}
void Error() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(Error_led, ledState);
}
}