Simple passcode modifier ?

I am trying to make a sketch whereby a four digit passcode can be modified at any time via the serial monitor window. As can be seen the sketch accepts the number after I press enter but processes it one digit at a time, also it remembers the digits pressed previously. the following is from pressing 1234E5678E --- Set Pin --- Type in Box above, . Password Set To :1 Password Set To :12 Password Set To :123 Password Set To :1234 Password Set To :1234

Password Set To :1234

Password Set To :1234 5 Password Set To :1234 56 Password Set To :1234 567 Password Set To :1234 5678 Password Set To :1234 5678

Password Set To :1234 5678

This makes the string "inputString" grow each time instead of amending it.

I intend to use "inputString" later in the sketch for entry exit

Can anyone see where my problem is please

boolean stringComplete = false;
String inputString = "";
int ByteReceived;
void setup()   /****** SETUP: RUNS ONCE ******/
{
  Serial.begin(9600);
  Serial.println("--- Set Pin ---");
  Serial.println(" Type in Box above, . ");

}


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  if (Serial.available() > 0)
  {
    char inChar = Serial.read();
    inputString += inChar;
    if (inChar > 4 ) {  
      stringComplete = true;
      if (inChar == 'C') {  
        inputString = "0000";
      }
      if (stringComplete) {
        Serial.print("Pin Set To :");
        Serial.println(inputString);
        Serial.flush();
      }
    }
  }

I don’t understand what you asking your program to do, if you enter 1234 and you seem to get 1234, you enter 5678 and you seem to get 5678.

What do you want to get after entering 1234?

    if (inChar > 4 ) {

This appears to be a mistake. Did you intend to check the NUMBER of characters accumulated in inputString? You are checking the ASCII value of the character you just read! I think you probably meant     if (inputString.length() > 4 ) {   Although if you are looking for a 4-digit PIN you probably meant '>= 4' rather than '>4'.

inChar is the character that you read from serial monitor. With normal ascii text, that will in general be greater than the number 4. So

if (inChar > 4 )

will (more than likely) always be true.

You probably want to check if you have received 4 characters.

You should also reset stringComplete to false once you have processed the 4 characters.

johnwasser:     if (inChar > 4 ) {

This appears to be a mistake. Did you intend to check the NUMBER of characters accumulated in inputString? You are checking the ASCII value of the character you just read! I think you probably meant     if (inputString.length() > 4 ) {  Although if you are looking for a 4-digit PIN you probably meant '>= 4' rather than '>4'.

I replaced with == I now get the desired effect. Thank you so much for your input, I don't suppose you know how to add numeric keypad support in the USBHost library by any chance.

sterretje: inChar is the character that you read from serial monitor. With normal ascii text, that will in general be greater than the number 4. So

if (inChar > 4 )

will (more than likely) always be true.

You probably want to check if you have received 4 characters.

You should also reset stringComplete to false once you have processed the 4 characters.

I have now added

inputString = "";

as setting it to false caused the string to continue to grow in size.

Thank you for your input.

Now here is my full code.
It is on a Due with a numeric USB keypad connected to the native port.
The passcode is set via the serial monitor running on a pc ( I plan to add Blynk and set remotely)
The numeric usb keypad is for a door entry system, visitors enter a code and in they go.
The sketch as it stands hangs and misses keys entered in serial monitor when setting the passcode, however the USBHost side works flawlessly if you ignore the fact the keys are not mapped.
I have tried higher and lower baud rates without any success.

// Y=1
// Z=2
// [=3
// \=4
// ]=5
// ^=6
// _=7
// `=8
// a=9
// b=0
#include <KeyboardController.h>
String PassCode = "";
String inputString1 = "";         // a string to hold incoming data
boolean stringComplete1 = false;  // whether the string is complete
boolean stringComplete = false;
String inputString = "";
int ByteReceived;
USBHost usb;

// Attach Keyboard to native USB socket
KeyboardController keyboard(usb);

void setup() {
  Serial.begin(115200);
  Serial.println(" Enter Password  ");

}

void keyPressed() {
  char inChar1 = keyboard.getOemKey();

  Serial.write(keyboard.getOemKey());
  Serial.println();
  printKey();
  inputString1 += inChar1;
  if (inChar1 == 'X') {  // If carriage return
    stringComplete1 = true;
  }

}
void printKey() {}
void loop() {
  usb.Task();

  if (stringComplete1) {
    Serial.print(" You entered this Password:");
    Serial.println(inputString1);
    if (PassCode == inputString1) {
      // clear the string:


      Serial.println(" Password OK ---> Unlock the door ");
      // TODO : Set a PIO to open the door
    }
    else {
      Serial.println(" Password not OK ---> door remains closed ");


    }
    stringComplete1 = false;
    Serial.println(" Enter Password  ");
    inputString1 = "";
  }



  if (Serial.available() > 0)
  {
    char inChar = Serial.read();
    inputString += inChar;

    if (inputString.length() == 4 ) {  // If carriage return
      stringComplete = true;
      if (inChar == 'C') {  // If carriage return
        inputString = "";

      }
      if (stringComplete) {

        Serial.print("Pin Set To : ");
        Serial.println(inputString);
        PassCode = inputString;
        inputString = "";
        
      }
    }
  }
}
  char inChar1 = keyboard.getOemKey();
  Serial.write(keyboard.getOemKey());

It looks like you are reading two separate keystrokes from the keyboard: one to process and one to display. You should probably display the one you are going to process.

johnwasser:   char inChar1 = keyboard.getOemKey();   Serial.write(keyboard.getOemKey());

It looks like you are reading two separate keystrokes from the keyboard: one to process and one to display. You should probably display the one you are going to process.

I have tried removing them in turn and both together, with no success. That side of it works ok but guess it is taking up resources on the serial monitor and causing the missing data and lag

After a lot of failures, I am starting from scratch.
I am using this example as a building block along with password library

I am trying, and failing to change the password 1234 to a string from further in the code but the ide will not allow this, is there a work around ?

Password password = Password( "1234" );
#include <Password.h> //http://www.arduino.cc/playground/uploads/Code/Password.zip
#include <Keypad.h> //http://www.arduino.cc/playground/uploads/Code/Keypad.zip

Password password = Password( "1234" );

const byte ROWS = 4; // Four rows
const byte COLS = 4; //  columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

byte rowPins[ROWS] = {10, 9, 8, 7}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 5, 4, 3};//connect to the column pinouts of the keypad

// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){

  Serial.begin(9600);
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad
}

void loop(){
  keypad.getKey();
}

//take care of some special events
void keypadEvent(KeypadEvent eKey){
  switch (keypad.getState()){
    case PRESSED:
 Serial.print("Pressed: ");
 Serial.println(eKey);
 switch (eKey){
  case '*': checkPassword(); break;
  case '#': password.reset(); break;
  default: password.append(eKey);
     }
  }
}

void checkPassword(){
  if (password.evaluate()){
    Serial.println("Success");
    //Add code to run if it works
  }else{
    Serial.println("Wrong");
    //add code to run if it did not work
  }
}