Help Needed: How To Convert Serial input to String

Hi,
I am trying to build a project that will act as a lock. I want to have the string "pin" (which is the code to unlock the lock) be changed to the value of whatever numbers come through the serial, but i need it to be a string. is there any way to accomplish this?
any help would be great.

Here is my code:

#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
#include <Keypad.h>
const byte ROWS = 4; // rows
const byte COLS = 3; // columns
char hexaKeys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'},
  };
  
byte rowPins[ROWS] = {A0, A1, A2, A3}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 9, 10}; //connect to the column pinouts of the keypad
Keypad myKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
char keypressed;
String pinentered;
String pin = "1234";
char newpin;
void setup(){
  lcd.begin(16, 2);
  Serial.begin(9600);
  
  
}


void loop(){
 char keypressed = myKeypad.getKey();

 if (keypressed == '#'){
      if (pinentered == pin) {
           lcd.setCursor(0,1);
           lcd.print("Access Granted!          ");
      }
     if (pinentered != pin) {
         lcd.setCursor(0,1);
           lcd.print("Access Denied!          ");
     }
    }
else if (keypressed == '*'){
   pinentered = "";
   lcd.setCursor(0,0);
   lcd.print("                ");
   lcd.setCursor(0,1);
   lcd.print("CLEARED!           "); 
 }
 
else if (keypressed){
   pinentered = pinentered + keypressed;
   lcd.setCursor(0,1);
   lcd.print("                ");
   lcd.setCursor(0,0);
   lcd.print(pinentered);
   
 
 }
 
 
  if (Serial.available()) {
    // wait a bit for the entire message to arrive
    delay(100);
    // read all the available characters
    while (Serial.available() > 0) {   
     pin = String(Serial.read());
    
} 
  }
}

Thanks,
Bay
:slight_smile:

but i need it to be a string. is there any way to accomplish this?

The only thing that can come from the Serial Monitor application is characters.

A string is a NULL terminated array of characters.

So, yes, storing the data in a string is trivial.

Storing the data in a String is a different, and unnecessary, issue which I won't help you with.

Look at the demo here

...R

You really shouldn't be using the string class. It has so many issues.

I'd also suggest you store your pin in EEprom so that once it's been updated, it won't get lost once power has been lost.

Another thing I'd suggest is that you don't lock your code into a closed loop once serial is detected. Instead just take a note of the current character being recieved and move on.

With these aspects in mind I've knocked up a rough sketch. It'll doubtless need debugging as it's been written while attempting to follow Murdoch Mysteries on the TV, having a discussion with my grand-daughter about the importance of her education and preparing breakfast.

and yes, it's totally untested :slight_smile:

#include <EEPROM.h>

#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
#include <Keypad.h>
const byte ROWS = 4; // rows
const byte COLS = 3; // columns
char hexaKeys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'},
  };
  
byte rowPins[ROWS] = {A0, A1, A2, A3}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 9, 10}; //connect to the column pinouts of the keypad
Keypad myKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
char keypressed;
String pinentered;
char pin[10] = "1234";
char userPin[10];   //used to note users entry on keypad
int pinIndex=0;     //how many digits read in so far
char updatePin[10];  //used to note updated pin from serial
int updateIndex=0;  //how many digits have been recieved from serial so far
void setup(){
  lcd.begin(16, 2);
  Serial.begin(9600);

//if a pin has been saved in rom
//read it now
if(EEPROM.read(0)!=0xff)
  getPin();
}


void loop(){
 char keypressed = myKeypad.getKey();

//if a number is pressed append it to userPin
 if(keypressed >='0' && keypressed <= '9')
   {
    userPin[pinIndex]=keypressed;
    lcd.print(keypressed);
    if(pinIndex<9)
     pinIndex++;
   } 

//if # pressed check userPin against pin
 if (keypressed == '#')
   {
    userPin[pinIndex]=0;
    pinIndex=0;
    if(strcmp (userPin,pin)==0)
       {lcd.setCursor(0,1);
        lcd.print("Access Granted!          ");
       }
      else
        {
         lcd.setCursor(0,1);
         lcd.print("Access Denied!          ");
       }
    lcd.setCursor(0,0);
    lcd.print("                ");
    lcd.setCursor(0,0);
    }

if (keypressed == '*')
  {
   pinIndex=0;
   lcd.setCursor(0,0);
   lcd.print("                ");
   lcd.setCursor(0,1);
   lcd.print("CLEARED!           "); 
   lcd.setCursor(0,0);
 }
 
if (Serial.available()) 
  {
  char sChar=Serial.read();     
  if(sChar >='0' && sChar <= '9')
     {
      updatePin[updateIndex]=sChar;
      if(updateIndex < 9)
       updateIndex++;
     } 

  if((sChar=='\r' || sChar=='\n') && updateIndex > 0 )
     {updatePin[updateIndex]=0;
     savePin();
     }
   } 

if(keypressed) 
  delay(400);//for debounce
}

void savePin()
{
int n;
//copy updatePin into working copy of pin
sprintf(pin,updatePin);
updateIndex=0;

//write the new pin to EEPROM
for(n=0;pin[n]!=0;n++)
  EEPROM.write(n,pin[n]);
EEPROM.write(n,0);
}

void getPin()
{
int n;
for(n=0;EEPROM.read(n)!=0;n++)
    pin[n]=EEPROM.read(n);
pin[n]=0;
}