What am I missing in my code?

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
  }
}

At a glance it looks like you need to enter the buttons in order on e each.

Do you mean to have 5 twice?

Oh, wait, still figuring out how the combo works.

a7

i just figured it out it was a wire not making contact in terminal cleaned insulation and now its working

Please mark the thread as solved so that other members that wish to help will not waste their time opening the thread only to find that you no longer need help.

I hate when that happens. :wink:

Now while you are thinking about it, do whatever it is that you do to keep a copy of a known functioning sketch available for occasions like you might have messed with something a little bit, or a little bit too much.

I do rely on a backup system that lets me step back in time but I also zip project documnets and make my own snapshots. I toss everything I think I'd need to do it all over in there.

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.