password check

I have this setup and working good. now I would like to monitor when a new passcode is entered so that it can not contain a # or *. I am not sure ow to go about it to get this done

#include <Keypad.h>
#include <EEPROM.h>

const unsigned long lockint = 1500;
unsigned long locktimer;
char* pass = "1234";
int pos = 0;
boolean locked = true;

const byte rows = 4;
const byte cols = 3;
char keys[rows][cols] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[rows] = {7, 12, 11, 9};
byte colPins[cols] = {8, 6, 10};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);

#define intRelay 2
#define starterRelay 3
#define redLed 4
#define greenLed 5

char holdKey;
unsigned long tHold;

void setup(){
  Serial.begin(9600);
  pinMode(intRelay, OUTPUT);
  pinMode(starterRelay, OUTPUT);
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  digitalWrite(intRelay, LOW);
  digitalWrite(starterRelay, LOW);
  digitalWrite(redLed, HIGH);
  digitalWrite(greenLed, LOW);
  loadCode();
  flash();
  updateOutputs();
}

void loop(){
  char key = keypad.getKey();
  if (key){
    holdKey = key;
  }
  if ( ! locked && (keypad.getState() == HOLD)){
    if ((millis() - tHold) > 500){
      switch (holdKey){
        case '0':
          digitalWrite(starterRelay, HIGH);
          break;
      }
      tHold = millis();
    }
  }
  if (keypad.getState() == RELEASED){
    digitalWrite(starterRelay, LOW);
  }
  if (key == '*' && ! locked){
    pos = 0;
    getNewCode();
    updateOutputs();
  }
  if (key == '#'){
    locked = true;
    pos = 0;
    updateOutputs();
  }
  if (key == pass[pos]){
    pos ++;
  }
  if (pos == 4){
    locked = false;
    updateOutputs();
  }
  delay(75);
}

void updateOutputs(){
  if (! locked){
    digitalWrite(redLed, LOW);
    digitalWrite(greenLed, HIGH);
    digitalWrite(intRelay, HIGH);
  }
  else{
    digitalWrite(greenLed, LOW);
    digitalWrite(redLed, HIGH);
    digitalWrite(intRelay, LOW);
    digitalWrite(starterRelay, LOW);
  }
}

void getNewCode(){
  flash();
  for (int i = 0; i < 4; i++){
    char key;
    key = keypad.getKey();
    while (key == 0){
      key = keypad.getKey();
    }
    flash();
    pass[i] = key;
  }
  saveCode();
  flash();
  flash();
}

void loadCode(){
  if (EEPROM.read(0) == 1){
    pass[0] = EEPROM.read(1);
    pass[1] = EEPROM.read(2);
    pass[2] = EEPROM.read(3);
    pass[3] = EEPROM.read(4);
  }
}

void saveCode(){
  EEPROM.write(1, pass[0]);
  EEPROM.write(2, pass[1]);
  EEPROM.write(3, pass[2]);
  EEPROM.write(4, pass[3]);
  EEPROM.write(0, 1);
}

void flash(){
  digitalWrite(redLed, HIGH);
  digitalWrite(greenLed, HIGH);
  delay(500);
  digitalWrite(redLed, LOW);
  digitalWrite(greenLed, LOW);
}

void lock(){
  locked = true;
  pos = 0;
  updateOutputs;
}

I am not sure ow to go about it to get this done

Why not? getNewCode() collects the new code. Can't you simply test whether the entered key is a '*' or '#' before saving it?

I have tried only running the saveCode function if # or * is not present but it still seems to save anyway

void getNewCode(){
  flash();
  for (int i = 0; i < 4; i++){
    char key;
    key = keypad.getKey();
    while (key == 0){
      key = keypad.getKey();
    }
    flash();
    pass[i] = key;
  }
  if (pass[0] != '#' || '*'){
    if (pass[1] != '#' || '*'){
      if (pass[2] != '#' || '*'){
        if (pass[3] != '#' || '*'){
          saveCode();
        }
      }
    }
  }
  flash();
  flash();
}
  if (pass[0] != '#' || '*'){

What does '*' evaluate to? Is that true or false?

Hint: it's true.

What you probably want is

  if (pass[0] != '#' && pass[0] != '*'){

If the first character is not '#' AND the first character is not '*', do this.

ok I have changed it and I still have the same issue where it dosen’t keep the old pass and still saves the new one

#include <Keypad.h>
#include <EEPROM.h>

const unsigned long lockint = 1500;
unsigned long locktimer;
char* pass = "1234";
int pos = 0;
boolean locked = true;

const byte rows = 4;
const byte cols = 3;
char keys[rows][cols] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[rows] = {7, 12, 11, 9};
byte colPins[cols] = {8, 6, 10};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);

#define intRelay 2
#define starterRelay 3
#define redLed 4
#define greenLed 5

char holdKey;
unsigned long tHold;

void setup(){
  Serial.begin(9600);
  pinMode(intRelay, OUTPUT);
  pinMode(starterRelay, OUTPUT);
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  digitalWrite(intRelay, LOW);
  digitalWrite(starterRelay, LOW);
  digitalWrite(redLed, HIGH);
  digitalWrite(greenLed, LOW);
  loadCode();
  flash();
  updateOutputs();
}

void loop(){
  char key = keypad.getKey();
  if (key){
    holdKey = key;
  }
  if ( ! locked && (keypad.getState() == HOLD)){
    if ((millis() - tHold) > 500){
      switch (holdKey){
        case '0':
          digitalWrite(starterRelay, HIGH);
          break;
      }
      tHold = millis();
    }
  }
  if (keypad.getState() == RELEASED){
    digitalWrite(starterRelay, LOW);
  }
  if (key == '*' && ! locked){
    pos = 0;
    getNewCode();
    updateOutputs();
  }
  if (key == '#'){
    locked = true;
    pos = 0;
    updateOutputs();
  }
  if (key == pass[pos]){
    pos ++;
  }
  if (pos == 4){
    locked = false;
    updateOutputs();
  }
  delay(75);
}

void updateOutputs(){
  if (! locked){
    digitalWrite(redLed, LOW);
    digitalWrite(greenLed, HIGH);
    digitalWrite(intRelay, HIGH);
  }
  else{
    digitalWrite(greenLed, LOW);
    digitalWrite(redLed, HIGH);
    digitalWrite(intRelay, LOW);
    digitalWrite(starterRelay, LOW);
  }
}

void getNewCode(){
  flash();
  for (int i = 0; i < 4; i++){
    char key;
    key = keypad.getKey();
    while (key == 0){
      key = keypad.getKey();
    }
    flash();
    pass[i] = key;
  }
  if (pass[0] != '#' && pass[0] != '*'){
    if (pass[1] != '#' && pass[1] != '*'){
      if (pass[2] != '#' && pass[2] != '*'){
        if (pass[3] != '#' && pass[3] != '*'){
          saveCode();
        }
      }
    }
  }
  flash();
  flash();
}

void loadCode(){
  if (EEPROM.read(0) == 1){
    pass[0] = EEPROM.read(1);
    pass[1] = EEPROM.read(2);
    pass[2] = EEPROM.read(3);
    pass[3] = EEPROM.read(4);
  }
}

void saveCode(){
  EEPROM.write(1, pass[0]);
  EEPROM.write(2, pass[1]);
  EEPROM.write(3, pass[2]);
  EEPROM.write(4, pass[3]);
  EEPROM.write(0, 1);
}

void flash(){
  digitalWrite(redLed, HIGH);
  digitalWrite(greenLed, HIGH);
  delay(500);
  digitalWrite(redLed, LOW);
  digitalWrite(greenLed, LOW);
}

void lock(){
  locked = true;
  pos = 0;
  updateOutputs;
}

I don't know what you are entering.

  Serial.begin(9600);

Here, you establish the serial port connection.

Here are all the Serial.print() statements:


Perhaps you should add another one or two...

You know, stomping all over pass in the getKeyCode() function doesn't seem like a good idea. pass should be overwritten only in saveCode(). A temporary array in getKeyCode(), passed to saveCode() would be better.