Checking time and password algorithm

Hi, i am using arduino uno and trying to make a "mask deliver machine" . for this purpose my device;
1)check the password ,which is entered by user with keyboard,
2)deliver mask if pasword correct and defined user has not get any other mask within 1 day.

i am using
*arduino uno
*rtc 1302
*a4998 motor driver
*nema 17 stepper
*4x4 keypad
*i2c 16x2 lcd

my question is how to compare both pasword and time simultanously. i write a code with has 10 if statement that is very unconvenient when user counts become bigger. is there any simplier way than this. and i am very open to another commets.

Thanks for helping.

releted part of my code;

void checkPassword(){
  time_t nextMakeTime;
  tm.Second = myRTC.seconds; 
  tm.Hour = myRTC.hours;
  tm.Minute = myRTC.minutes;
  tm.Day = myRTC.dayofmonth;
  tm.Month = myRTC.month;
  tm.Year = myRTC.year - 1970;   // offset from 1970;
  nextMakeTime =  makeTime(tm)+(86400); // convert time elements into time_t
  Serial.println(nextMakeTime);   
  Serial.println(day(nextMakeTime));   
  Serial.println(month(nextMakeTime));   
  Serial.println(year(nextMakeTime)); 

  //compare date
  int mm=month(nextMakeTime);
  int dd=day(nextMakeTime);
  int hh=hour(nextMakeTime);

  unsigned long mmddhh=long(mm)*10000+long(dd)*100+hh ;                                                                                                                           
  Serial.print("mmddhh: ");Serial.println(mmddhh);

  
  if (!strcmp(Data, Master2)) {           
      if (Master2date<mmddhh){
        lcd.print("Correct");
        Master2date=mmddhh;
        stepDon();
        }
      else {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Dunluk Kullanim");
        lcd.setCursor(0,1);
        lcd.print("Hakkiniz Dolmustur");
        delay(2000);
        }      
      }

  if (!strcmp(Data, Master3)) {           
      if (Master3date<mmddhh){
        lcd.print("Correct");
        Master3date=mmddhh;
        stepDon();
        }
      else {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Dunluk Kullanim");
        lcd.setCursor(0,1);
        lcd.print("Hakkiniz Dolmustur");
        delay(2000);
        }      
      }
  if (!strcmp(Data, Master4)) {           
      if (Master4date<mmddhh){
        lcd.print("Correct");
        Master4date=mmddhh;
        stepDon();
        }
      else {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Dunluk Kullanim");
        lcd.setCursor(0,1);
        lcd.print("Hakkiniz Dolmustur");
        delay(2000);
        }      
      }
  if (!strcmp(Data, Master5)) {           
      if (Master5date<mmddhh){
        lcd.print("Correct");
        Master5date=mmddhh;
        stepDon();
        }
      else {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Dunluk Kullanim");
        lcd.setCursor(0,1);
        lcd.print("Hakkiniz Dolmustur");
        delay(2000);
        }      
      }
  if (!strcmp(Data, Master6)) {           
      if (Master6date<mmddhh){
        lcd.print("Correct");
        Master6date=mmddhh;
        stepDon();
        }
      else {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Dunluk Kullanim");
        lcd.setCursor(0,1);
        lcd.print("Hakkiniz Dolmustur");
        delay(2000);
        }      
      }
  if (!strcmp(Data, Master7)) {           
      if (Master7date<mmddhh){
        lcd.print("Correct");
        Master7date=mmddhh;
        stepDon();
        }
      else {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Dunluk Kullanim");
        lcd.setCursor(0,1);
        lcd.print("Hakkiniz Dolmustur");
        delay(2000);
        }      
      }
  if (!strcmp(Data, Master8)) {           
      if (Master8date<mmddhh){
        lcd.print("Correct");
        Master8date=mmddhh;
        stepDon();
        }
      else {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Dunluk Kullanim");
        lcd.setCursor(0,1);
        lcd.print("Hakkiniz Dolmustur");
        delay(2000);
        }      
      }
  if (!strcmp(Data, Master9)) {           
      if (Master9date<mmddhh){
        lcd.print("Correct");
        Master9date=mmddhh;
        stepDon();
        }
      else {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Dunluk Kullanim");
        lcd.setCursor(0,1);
        lcd.print("Hakkiniz Dolmustur");
        delay(2000);
        }      
      }
  if (!strcmp(Data, Master10)) {           
      if (Master10date<mmddhh){
        lcd.print("Correct");
        Master10date=mmddhh;
        stepDon();
        }
      else {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Dunluk Kullanim");
        lcd.setCursor(0,1);
        lcd.print("Hakkiniz Dolmustur");
        delay(2000);
        }      
      }



      
  
}

all of my code is like this;

#include <virtuabotixRTC.h>   
virtuabotixRTC myRTC(11, 12, 13);

#include <TimeLib.h>
tmElements_t tm;

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#include <Keypad.h>
#define Password_Length 8
// Character to hold password input
char Data[Password_Length];
// Password
char Master[Password_Length] = "123A456";

char Master2[Password_Length] = "1234567";
char Master3[Password_Length] = "1234568";
char Master4[Password_Length] = "1234569";
char Master5[Password_Length] = "1234561";
char Master6[Password_Length] = "1234562";
char Master7[Password_Length] = "1234563";
char Master8[Password_Length] = "1234564";
char Master9[Password_Length] = "1234565";
char Master10[Password_Length] = "1234566";


unsigned long Master2date,Master3date,Master4date,Master5date,
            Master6date,Master7date,Master8date,Master9date,Master10date;

// Pin connected to lock relay input
int lockOutput = 13;
 
// Counter for character entries
byte data_count = 0;
 
// Character to hold key input
char customKey;
 
// Constants for row and column sizes
const byte ROWS = 4;
const byte COLS = 4;
 
// Array to represent keys on keypad
char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
 
// Connections to Arduino
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
 
// Create keypad object
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
 
// Create LCD object
LiquidCrystal_I2C lcd(0x27, 16, 2);

//-----stepper
#define dirPin A0
#define stepPin A1
const int stepsPerRevolution = 200;


void setup() {
  Serial.begin(9600);
  
 //lcd keypad
 // Setup LCD with backlight and initialize
  lcd.backlight();
  lcd.init();
 
  // Set lockOutput as an OUTPUT pin
  pinMode(lockOutput, OUTPUT);

//stepper
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  
}

void loop() {
  
  myRTC.updateTime();     
  Serial.print("Current Date / Time: ");                                                                 //| 
  Serial.print(myRTC.dayofmonth);                                                                        //| 
  Serial.print("/");                                                                                     //| 
  Serial.print(myRTC.month);                                                                             //| 
  Serial.print("/");                                                                                     //| 
  Serial.print(myRTC.year);                                                                              //| 
  Serial.print("  ");                                                                                    //| 
  Serial.print(myRTC.hours);                                                                             //| 
  Serial.print(":");                                                                                     //| 
  Serial.print(myRTC.minutes);                                                                           //| 
  Serial.print(":");                                                                                     //| 
  Serial.println(myRTC.seconds);  
  
//sil-------

Serial.println("--------");

  time_t nextMakeTime;
  tm.Second = myRTC.seconds; 
  tm.Hour = myRTC.hours;
  tm.Minute = myRTC.minutes;
  tm.Day = myRTC.dayofmonth;
  tm.Month = myRTC.month;
  tm.Year = myRTC.year - 1970;   // offset from 1970;
  nextMakeTime =  makeTime(tm)+(86400); // convert time elements into time_t
  Serial.println(nextMakeTime);   
  Serial.println(day(nextMakeTime));   
  Serial.println(month(nextMakeTime));   
  Serial.println(year(nextMakeTime));
  Serial.println(hour(nextMakeTime));

int mm=month(nextMakeTime);
  int dd=day(nextMakeTime);
  int hh=hour(nextMakeTime);

  unsigned long mmddhh=long(mm)*10000+long(dd)*100+hh ;                                                                                                                           
  Serial.print("mmddhh: ");Serial.println(mmddhh);

  delay(3000);

//sil------------------
   
                                                                                                         //| 
                                                                                                        
//lcd keypad

 // Initialize LCD and print
  lcd.setCursor(0, 0);
  lcd.print("Enter Password:");
 
  // Look for keypress
  customKey = customKeypad.getKey();
  if (customKey) {
    // Enter keypress into array and increment counter
    Data[data_count] = customKey;
    lcd.setCursor(data_count, 1);
    lcd.print(Data[data_count]);
    data_count++;
  }
 
  // See if we have reached the password length
  if (data_count == Password_Length - 1) {
    lcd.clear();
 
    if (!strcmp(Data, Master)) {
      // Password is correct
      lcd.print("Correct-Master");
      stepDon();    
    }  
    checkPassword();
 
    }
 
    // Clear data and LCD display
    lcd.clear();
    clearData();
} 
                                                                                         //| 
 

void clearData() {
  // Go through array and clear data
  while (data_count != 0) {
    Data[data_count--] = 0;
  }
  return;
}  

void stepDon(){
    digitalWrite(dirPin, LOW);
    for(int x = 0; x < stepsPerRevolution; x++)
  {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(2000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(2000);
  }
  delay(2000);
}




void checkPassword(){
  time_t nextMakeTime;
  tm.Second = myRTC.seconds; 
  tm.Hour = myRTC.hours;
  tm.Minute = myRTC.minutes;
  tm.Day = myRTC.dayofmonth;
  tm.Month = myRTC.month;
  tm.Year = myRTC.year - 1970;   // offset from 1970;
  nextMakeTime =  makeTime(tm)+(86400); // convert time elements into time_t
  Serial.println(nextMakeTime);   
  Serial.println(day(nextMakeTime));   
  Serial.println(month(nextMakeTime));   
  Serial.println(year(nextMakeTime)); 

  //compare date
  int mm=month(nextMakeTime);
  int dd=day(nextMakeTime);
  int hh=hour(nextMakeTime);

  unsigned long mmddhh=long(mm)*10000+long(dd)*100+hh ;                                                                                                                           
  Serial.print("mmddhh: ");Serial.println(mmddhh);

  
  if (!strcmp(Data, Master2)) {           
      if (Master2date<mmddhh){
        lcd.print("Correct");
        Master2date=mmddhh;
        stepDon();
        }
      else {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Dunluk Kullanim");
        lcd.setCursor(0,1);
        lcd.print("Hakkiniz Dolmustur");
        delay(2000);
        }      
      }

  if (!strcmp(Data, Master3)) {           
      if (Master3date<mmddhh){
        lcd.print("Correct");
        Master3date=mmddhh;
        stepDon();
        }
      else {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Dunluk Kullanim");
        lcd.setCursor(0,1);
        lcd.print("Hakkiniz Dolmustur");
        delay(2000);
        }      
      }
  if (!strcmp(Data, Master4)) {           
      if (Master4date<mmddhh){
        lcd.print("Correct");
        Master4date=mmddhh;
        stepDon();
        }
      else {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Dunluk Kullanim");
        lcd.setCursor(0,1);
        lcd.print("Hakkiniz Dolmustur");
        delay(2000);
        }      
      }
  if (!strcmp(Data, Master5)) {           
      if (Master5date<mmddhh){
        lcd.print("Correct");
        Master5date=mmddhh;
        stepDon();
        }
      else {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Dunluk Kullanim");
        lcd.setCursor(0,1);
        lcd.print("Hakkiniz Dolmustur");
        delay(2000);
        }      
      }
  if (!strcmp(Data, Master6)) {           
      if (Master6date<mmddhh){
        lcd.print("Correct");
        Master6date=mmddhh;
        stepDon();
        }
      else {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Dunluk Kullanim");
        lcd.setCursor(0,1);
        lcd.print("Hakkiniz Dolmustur");
        delay(2000);
        }      
      }
  if (!strcmp(Data, Master7)) {           
      if (Master7date<mmddhh){
        lcd.print("Correct");
        Master7date=mmddhh;
        stepDon();
        }
      else {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Dunluk Kullanim");
        lcd.setCursor(0,1);
        lcd.print("Hakkiniz Dolmustur");
        delay(2000);
        }      
      }
  if (!strcmp(Data, Master8)) {           
      if (Master8date<mmddhh){
        lcd.print("Correct");
        Master8date=mmddhh;
        stepDon();
        }
      else {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Dunluk Kullanim");
        lcd.setCursor(0,1);
        lcd.print("Hakkiniz Dolmustur");
        delay(2000);
        }      
      }
  if (!strcmp(Data, Master9)) {           
      if (Master9date<mmddhh){
        lcd.print("Correct");
        Master9date=mmddhh;
        stepDon();
        }
      else {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Dunluk Kullanim");
        lcd.setCursor(0,1);
        lcd.print("Hakkiniz Dolmustur");
        delay(2000);
        }      
      }
  if (!strcmp(Data, Master10)) {           
      if (Master10date<mmddhh){
        lcd.print("Correct");
        Master10date=mmddhh;
        stepDon();
        }
      else {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Dunluk Kullanim");
        lcd.setCursor(0,1);
        lcd.print("Hakkiniz Dolmustur");
        delay(2000);
        }      
      }



      
  
}






















Use an array of character strings, and step through all passwords, comparing with the entered one, in one simple loop. Example of declaring/defining array elements:

#define Password_Length 8
char Master[][Password_Length] = {"123A456","1234567","1234568"};

void setup() {
Serial.begin(115200);
for (int i=0; i<3; i++) { //compare passwords
  Serial.print("i = ");
  Serial.print(i);
  Serial.print(" result: ");
  Serial.println(strcmp(Master[i],"123A456"));
}
}
void loop() {}
1 Like

thanks a lot it worked

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