Current Input Resets, Cant Figure Out Why

Hi Guys,

Been banging my head against a wall on this one.

The Intention:
When the button is pressed, it takes that number and stores it into the variable until enough inputs have been made (4 in this case) when it will then check the code is correct against the stored code.
So pretty much a standard 4 input alarm panel to deactivate with a 4 digit pin.

The Problem:
My variable to store the characters currently being input is not adding/concatenating etc. it currently just displays the button I just pressed. very annoying.

// constants
const int Button_4 = 5;
const int Button_3 = 4;
const int Button_2 = 2;
const int Button_1 = 3;
const String CorrectCode = "3421";

// Variables
int LastState_4 = LOW;
int LastState_3 = LOW;
int LastState_2 = LOW;
int LastState_1 = LOW;
int CurrentInputCount = 0;
String CurrentInput = "0";
int ButtonPressed ;
String CodeString;
int CurrentState_4;
int CurrentState_3;
int CurrentState_2;
int CurrentState_1;


void setup()
{
  Serial.begin(9600);
  pinMode(Button_4, INPUT_PULLUP);
  pinMode(Button_3, INPUT_PULLUP);
  pinMode(Button_2, INPUT_PULLUP);
  pinMode(Button_1, INPUT_PULLUP);
}

void loop()
{
  // READ DIGITAL STATE OF BUTTONS
  CurrentState_4 = digitalRead(Button_4);
  CurrentState_3 = digitalRead(Button_3);
  CurrentState_2 = digitalRead(Button_2);
  CurrentState_1 = digitalRead(Button_1);

  if (LastState_4 == HIGH && CurrentState_4 == LOW) {
    ButtonPressed = 4;
    PasswordInput();
    delay(100);
  }
  else if (LastState_3 == HIGH && CurrentState_3 == LOW) {
    ButtonPressed = 3;
    PasswordInput();
    delay(100);
  }
  else if (LastState_2 == HIGH && CurrentState_2 == LOW) {
    ButtonPressed = 2;
    PasswordInput();
    delay(100);
  }
  else if (LastState_1 == HIGH && CurrentState_1 == LOW) {
    ButtonPressed = 1;
    PasswordInput();
    delay(100);

  } else {
    ButtonPressed = 0;
    delay(100);
  }

  LastState_4 = CurrentState_4;
  LastState_3 = CurrentState_3;
  LastState_2 = CurrentState_2;
  LastState_1 = CurrentState_1;
}


void PasswordInput()
{
  // SHOW KEY PRESSED
  Serial.println(String(ButtonPressed));
  // DO NOTHING IF NOTHING PRESSED
  if (ButtonPressed == 0) {
  }

  // IF COUNT OF INPUTS BETWEEN 0 AND 4
  if (CurrentInputCount >= 0 && CurrentInputCount <= 4 ) {

    // IF THE CURRENT CODE INPUT IS DEFAULTED
    if (CurrentInput = "99999") {
      // CODE INPUT STARTS AGAIN
      CurrentInput = String(ButtonPressed);
    }
    else {
      // CONCATENATE CURRENT CODE AND NEW INPUT
      CurrentInput = String(CurrentInput) + String(ButtonPressed) ;
    }
    //INCREASE THE COUNT OF INPUT
    ++CurrentInputCount;
  }

  // SHOW CURRENT STORED CODE INPUT
  Serial.println(CurrentInput);
  // IF 4 INPUTS HAVE BEEN MADE, CHECK PASS CODE.
  if (CurrentInputCount == 4) {
    PasswordCheck();
  }
}

//CHECK PASS CODE
void PasswordCheck() {
  // IF THE CURRENT INPUT MATCHES THE STORED CODE
  if (String(CurrentInput) == String(CorrectCode)) {
    //DEACTIVATE ALARM AND DISPLAY MESSAGE
    Serial.println("Alarm Deactivated");
    // SET INPUT COUNT BACK TO 0
    CurrentInputCount = 0;
    // DEFAULT OFF THE CURRENT INPUT
    CurrentInput = "99999";
  } else {
    // DISPLAY MESSAGE TO SHOW BAD CODE
    Serial.println("Bad Code");
    // SET INPUT COUNT BACK TO 0
    CurrentInputCount = 0;
    // DEFAULT OFF THE CURRENT INPUT
    CurrentInput = "99999";
  }
}

I would appreciate any pointers :slight_smile:

Many Thanks
POC

You should not use the String object. In fact, using a numeric value and arrays for button state detection would be much easier. This is a bug:

if (CurrentInput = "99999")

Will assign, not compare.

Booleans have "low" and "high" states. Integers do not.

Paul

LastState_4 = CurrentState_4;
  LastState_3 = CurrentState_3;
  LastState_2 = CurrentState_2;
  LastState_1 = CurrentState_1;

Have you considered using arrays for this?