I write a code for a museum kind off expo that have to stay there a long time ( 7-8 year), so I need a code free of bug that wont lets me down after a couple month, if someone can go over my code and tell me if this is good enough please.
This is a mechanical game as they call it , a couple of button (6) link to a question , then a true and a false button. You hold the question button and press true or false then some leds light up under a drawing, there are 6 drawing… 1 per answer. When all leds are light up, the game reset and when no button pressed for X second the game reset.
I test the code on my arduino mega but this code will be on a UNO so pin 30-31 will be replace by analog0 and analog1 pin as digital ( pin 14-15 I think) and the led I use right now will be replace with led stipe of 15 Led for each box so I get a couple of those mosfet : N-Channel MOSFET 60V 30A - COM-10213 - SparkFun Electronics
because of the leds requirement of 12v I will be runing the arduino on 12V ( I wanted to run it at 7.5 vcd to make sure the regulator can have that 8 year life but using 2 power suply with the mosfet look more complicate)
here the code:
int ledPins[] = {8,9,10,11,12,13}; // an array of pin numbers to which LEDS are attached
int pinCount1 = 6; // the number of pins (i.e. the length of the array)
int butPins[] = {2,3,4,5,6,7,30,31}; // an array of pin numbers to which BUTTONS are attached
int pinCount2 = 8; // the number of pins (i.e. the length of the array)
// states
int buttonStateT = LOW;
int buttonStateF = LOW;
int buttonState1 = LOW;
int buttonState2 = LOW;
int buttonState3 = LOW;
int buttonState4 = LOW;
int buttonState5 = LOW;
int buttonState6 = LOW;
int ledState1 = LOW;
int ledState2 = LOW;
int ledState3 = LOW;
int ledState4 = LOW;
int ledState5 = LOW;
int ledState6 = LOW;
// Inactivity Timer
unsigned long Timer;
void setup(){
// use a for loop to initialize each LED pin as an output
for (int lPin = 0; lPin < pinCount1; lPin++) {
pinMode(ledPins[lPin], OUTPUT);
}
// use a for loop to initialize each BUTTON pin as an input
for (int bPin = 0; bPin < pinCount2; bPin++) {
pinMode(butPins[bPin], INPUT);
}
}
void loop() {
// check if the pushbutton is pressed.
buttonStateT = digitalRead(butPins[7]);
buttonStateF = digitalRead(butPins[6]);
buttonState1 = digitalRead(butPins[0]);
buttonState2 = digitalRead(butPins[1]);
buttonState3 = digitalRead(butPins[2]);
buttonState4 = digitalRead(butPins[3]);
buttonState5 = digitalRead(butPins[4]);
buttonState6 = digitalRead(butPins[5]);
// When both buttons are pressed the ledState change then write to the output
// Buttons combination for question 1
if (buttonState1 == HIGH && buttonStateT == HIGH){
ledState1 = HIGH;
digitalWrite(ledPins[0],ledState1);
}
// Buttons combination for question 2
if (buttonState2 == HIGH && buttonStateT == HIGH){
ledState2 = HIGH;
digitalWrite(ledPins[1],ledState2);
}
// Buttons combination for question 3
if (buttonState3 == HIGH && buttonStateT == HIGH){
ledState3 = HIGH;
digitalWrite(ledPins[2],ledState3);
}
// Buttons combination for question 4
if (buttonState4 == HIGH && buttonStateF == HIGH){
ledState4 = HIGH;
digitalWrite(ledPins[3],ledState4);
}
// Buttons combination for question 5
if (buttonState5 == HIGH && buttonStateF == HIGH){
ledState5 = HIGH;
digitalWrite(ledPins[4],ledState5);
}
// Buttons combination for question 6
if (buttonState6 == HIGH && buttonStateF == HIGH){
ledState6 = HIGH;
digitalWrite(ledPins[5],ledState6);
}
// Game is over, reset the game
// Turn the leds OFF after X seconds when all leds are ON
if (ledState1 == HIGH && ledState2 == HIGH && ledState3 == HIGH && ledState4 == HIGH && ledState5 == HIGH && ledState6 == HIGH){
ledState1 = LOW ;
ledState2 = LOW;
ledState3 = LOW;
ledState4 = LOW;
ledState5 = LOW;
ledState6 = LOW;
delay(15000);
// set the LED with the ledState of the variable:
digitalWrite(ledPins[0], ledState1);
digitalWrite(ledPins[1], ledState2);
digitalWrite(ledPins[2], ledState3);
digitalWrite(ledPins[3], ledState4);
digitalWrite(ledPins[4], ledState5);
digitalWrite(ledPins[5], ledState6);
}
// Inactivity Timer, when X seconds pass without any buttons pressed the game reset
if (buttonState1 == HIGH || buttonState2 == HIGH || buttonState3 == HIGH || buttonState4 == HIGH || buttonState5 == HIGH || buttonState6 == HIGH || buttonStateT == HIGH || buttonStateF == HIGH){
Timer = millis();
}
if (millis() - Timer >= 10000UL){
ledState1 = LOW;
ledState2 = LOW;
ledState3 = LOW;
ledState4 = LOW;
ledState5 = LOW;
ledState6 = LOW;
digitalWrite(ledPins[0], ledState1);
digitalWrite(ledPins[1], ledState2);
digitalWrite(ledPins[2], ledState3);
digitalWrite(ledPins[3], ledState4);
digitalWrite(ledPins[4], ledState5);
digitalWrite(ledPins[5], ledState6);
}
}
thanks