I had a working code on the test bench and went and installed it in machine and I cant seem to get it to work. Ive double checked my wiring. I did make some edits to the file earlier but I cant figure out what I might have done? Did I accidently delete a bracket or something? Or is it hardware related
#include <JC_Button.h>
const int ButtonCount = 6;
const byte ButtonPins[ButtonCount] = {2, 3, 4, 5, 6, 7}; //first button is on pin 2
const int relay1 = 10; //relay1 signal is pin 10
int TurnOnCode[] = {6, 5, 5, 4, 3, 2};
const int TurnOnCodeCount = sizeof TurnOnCode / sizeof TurnOnCode[0];
int TurnOnCorrectCount = 0; // How many have been entered
int TurnOffCode[] = {1, 1, 1};
const int TurnOffCodeCount = sizeof TurnOffCode / sizeof TurnOffCode[0];
int TurnOffCorrectCount = 0; // How many have been entered
boolean ButtonWasPressed[ButtonCount]; // Defaults to 'false'
unsigned long ButtonStateChangeTime = 0; // Debounce timer common to all buttons
const unsigned long DebounceTime = 30;
void setup() //run once at sketch startup
{
Serial.begin(9600); //begin Serial
for (int i = 0; i < ButtonCount; i++)
pinMode(ButtonPins[i], INPUT_PULLUP);
pinMode(relay1, OUTPUT); // relay1 is an output
Serial.print("Turn On code: ");
for (int i = 0; i < TurnOnCodeCount; i++)
{
Serial.print(TurnOnCode[i]); //print each digit of the code
}
Serial.println();
Serial.print("Turn Off code: ");
for (int i = 0; i < TurnOffCodeCount; i++)
{
Serial.print(TurnOffCode[i]); //print each digit of the code
}
Serial.println();
}
void loop() //run repeatedly
{
unsigned long currentTime = millis();
// Check each button
for (int i = 0; i < ButtonCount; i++)
{
boolean buttonIsPressed = digitalRead(ButtonPins[i]) == LOW;
boolean buttonHasBounced = currentTime - ButtonStateChangeTime > DebounceTime;
if (buttonIsPressed != ButtonWasPressed[i] && buttonHasBounced)
{
ButtonStateChangeTime = currentTime;
ButtonWasPressed[i] = buttonIsPressed;
if (ButtonWasPressed[i])
{
checkEntered(i + 1); //call checkEntered and pass it the button number
}
}
}
}
void checkEntered(int button) //check the button press
{
// Check against turn-on code
if (button == TurnOnCode[TurnOnCorrectCount])
{
TurnOnCorrectCount++;
if (TurnOnCorrectCount == TurnOnCodeCount)
{
// Success!
digitalWrite(relay1, HIGH); //turn relay1 on
// Start over
TurnOnCorrectCount = 0;
}
}
else // Not the correct button
{
TurnOnCorrectCount = 0; // Start over
}
// Check against turn-off code
if (button == TurnOffCode[TurnOffCorrectCount])
{
TurnOffCorrectCount++;
if (TurnOffCorrectCount == TurnOffCodeCount)
{
// Success!
digitalWrite(relay1, LOW); //turn relay1 off
// Start over
TurnOffCorrectCount = 0;
}
}
else // Not the correct button
{
TurnOffCorrectCount = 0; // Start over
}
}