Fitting everything in

I'm working on a RFID, 4x3 keypad, deadbolt with 2x i2c 1602 LCDs. I'm following a tutorial that has everything laid out except how to drive a motor or servo. I'm repurposing a actual digital deadbolt so I have the motor, keypad and housing. I want to use a H bridge only to control the rotation direction, I also would like to use everything included in the code and pin-out currently. I'd like to add the H bridge and a relay for keypad backlight. I'm using a nano, currently have A3 A6 A7 available. Can anything be moved around to make the 3 outputs I need? Thank you in advance. I've included the sketch I'm working with (compiles as is) and my current pin out.

The colors in this pin out are for my exact setup.

I2c 1602 LCDs
5v+ (white) 5v+
GND (black) GND
0x25
SDA (purple) A4
SCL (gray) A5
0x27
SDA (orange) A4
SCL (yellow) A5

RFID
SS-SDA-RX (1) D10
SCK (2) D13
MOSI (3) D11
MISO-SCL-TX (4) D12
IRQ (5) Not Connected
GND (6) GND
RST (7) Not Connected
VCC (8) 3.3v

Keypad
R1 D2
R2 D3
R3 D4
R4 D5
C1 A0
C2 D7
C3 D8

LED
RED - (relay) A1
GREEN- (relay) A2
KP backlight - (relay)
Buzzer
Buzzer+ D6
Buzzer - GND

H Bridge
IN1 ????
IN2 ????
12V+ 5V+ input
GND GND
OUT1 Motor +
OUT2 Motor -

Available pins

A3, A6, A7

Current sketch it compiles and uploads okay but hasn't been tested.

#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <MFRC522.h>
#include <Wire.h>
#include <Keypad.h>
#include <EEPROM.h>

int relPin;
int state=0;
byte COD[10];
byte AUX[10];
int k=0;
String accessCode="*123456#";
String codpairing="*654321#";
//NFC
#define RST_PIN 9
#define SS_PIN 10
MFRC522 mfrc522(SS_PIN, RST_PIN);
#define NEW_UID {0xDE, 0xAD, 0xBE, 0xEF}
MFRC522::MIFARE_Key key;

//LCD
LiquidCrystal_I2C lcd1(0x27,16,2);
LiquidCrystal_I2C lcd2(0x25,16,2);

//KEYPAD
const byte numRows= 4;
const byte numCols= 3;

char keymap[numRows][numCols]=
{
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'},
};

//The keypad connections to the arduino terminals
byte rowPins[numRows] = {2,3,4,5};
byte colPins[numCols] = {A0,7,8,};

Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);

void setup() {
pinMode(A0,OUTPUT);
digitalWrite(A0,HIGH);
pinMode(A3,OUTPUT);
digitalWrite(A3,HIGH);
pinMode(A1,OUTPUT);
digitalWrite(A1,HIGH);
pinMode(A2,OUTPUT);
digitalWrite(A2,LOW);
//NFC
Serial.begin(9600); // Initialize serial communications with the PC
while (!Serial); // Do nothing if no serial port is opened
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522 card

for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
lcd1.init();
lcd2.init();
lcd1.backlight();
lcd2.backlight();
lcd1.setCursor(0,0);
lcd2.setCursor(0,0);
lcd1.clear();
lcd2.clear();
lcd1.print( "BLOCKED" );
lcd2.print( "BLOCKED" );
}

void readNFC(){ // This function will read the code stored on
for (byte i =0; i<(mfrc522.uid.size); i++) { // the UID
COD[i]=mfrc522.uid.uidByte[i];
}
Serial.print("COD");
Serial.print(COD[0]);
Serial.print(COD[1]);
Serial.print(COD[2]);
Serial.print(COD[3]);
}

void pairNFC(){
Serial.println("COD in pair");
Serial.print(COD[0]);
Serial.print(COD[1]);
Serial.print(COD[2]);
Serial.print(COD[3]);
long r=0;
int c=0;
for(int i=1;i<=EEPROM.read(0);i++){ //The UID cannot be stored on
switch(i%4){ // one variable, it was needed to be
case 1 :{AUX[0]=EEPROM.read(i); break;} // split
case 2 :{AUX[1]=EEPROM.read(i); break;}
case 3 :{AUX[2]=EEPROM.read(i); break;}
case 0 :{AUX[3]=EEPROM.read(i); break;}
}
if((i)%4==0)
{Serial.println(r);
if( AUX[0]==COD[0] && AUX[1]==COD[1] && AUX[2]==COD[2] && AUX[3]==COD[3] ){ //Verify if the code is in EEPROM
lcd1.clear();
lcd2.clear();
lcd1.setCursor(0,0);
lcd2.setCursor(0,0);
lcd1.print("CODE ALREADY IN");
lcd2.print("CODE ALREADY IN");
lcd1.setCursor(0,1);
lcd2.setCursor(0,1);
lcd1.print("SYSTEM");
lcd2.print("SYSTEM");
delay(2000);
c=1;
break;}
}
}

if(c==0){int aux2=EEPROM.read(0);
Serial.println("CODE PAIRED");
Serial.print(COD[0]);
Serial.print(COD[1]);
Serial.print(COD[2]);
Serial.print(COD[3]);

EEPROM.write(aux2+1,COD[0]); //Writing code in EEPROM
EEPROM.write(aux2+2,COD[1]);
EEPROM.write(aux2+3,COD[2]);
EEPROM.write(aux2+4,COD[3]);

aux2=aux2+4; // Position for a new code
Serial.println("aux2");
Serial.println(aux2);
EEPROM.write(0,0);
EEPROM.write(0,aux2);    
lcd1.clear();
lcd2.clear();
lcd1.setCursor(0,0);
lcd2.setCursor(0,0);
lcd1.print("CODE PAIRED");
lcd2.print("CODE PAIRED");
delay(2000);

}
}

boolean validationNFC(){
boolean c=false;
for(int i=1;i<=EEPROM.read(0);i++){ //Read the EEPROM
switch(i%4){
case 1 :{AUX[0]=EEPROM.read(i); break;}
case 2 :{AUX[1]=EEPROM.read(i); break;}
case 3 :{AUX[2]=EEPROM.read(i); break;}
case 0 :{AUX[3]=EEPROM.read(i); break;}
}
if((i)%4==0)
{
if( AUX[0]==COD[0] && AUX[1]==COD[1] && AUX[2]==COD[2] && AUX[3]==COD[3])
c=true; //Verify if the code is in EEPROM and make flag=true;
}
}
return c;
}

int compareCODE(String a) //We type a code on keypad and this will be compared
{ //with the accessCode;
if(a.equals(accessCode))
return 1;
else if(a.equals(codpairing)) return 2;
else return 0;
}

String takeCode(char x) //Will display on the LCD the code typed
{ char vec[10];
vec[0]=x;
lcd1.setCursor(0,0);
lcd2.setCursor(0,0);
lcd1.clear();
lcd2.clear();
lcd1.print('X');
lcd2.print('X');
for(int i=1;i<8;i++)
{vec[i]=myKeypad.waitForKey(); //Waits for 8 keys to be pressed and after that
lcd1.print("X");
lcd2.print('X');}
vec[8]=NULL;
String str(vec);
return str;
}

void loop() {

switch(state){
case 0: {
mfrc522.PCD_Init();
if ( mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial() ){

  readNFC(); //It will read the card and it will search for UID in its
  if(validationNFC()) //memory
    { state=1;
      lcd1.clear();
      lcd2.clear();
      lcd1.setCursor(0,0);
      lcd2.setCursor(0,0);
      lcd1.print( "VAILID NFC CODE" );
     lcd2.print( "VAILID NFC CODE" ); //The door will be opened
     delay(1000);
      return;
    }
  else{
    lcd1.clear();
    lcd2.clear();
    lcd1.setCursor(0,0);
    lcd2.setCursor(0,0);
    lcd1.print( "INVAILID NFC CODE" ); 
    lcd2.print( "INVAILID NFC CODE" ); //If the code was wrong blocked
  delay(1000);
   lcd1.setCursor(0,0);
   lcd2.setCursor(0,0);
   lcd1.clear();
   lcd2.clear();
   lcd1.print( "BLOCKED" );
   lcd2.print( "BLOCKED" );
   return;
    }
}

char c=myKeypad.getKey();
if(c != NO_KEY){

  String codcurent=takeCode(c);
  int A=compareCODE(codcurent);
  if(A==0){ //A is a variable that stores the current code
    lcd1.clear();
    lcd2.clear();
    lcd1.print("INVALID CODE");
    lcd2.print("INVALID CODE");
    delay(2000);
    lcd1.setCursor(0,0);
    lcd2.setCursor(0,0);
    lcd1.clear();
    lcd2.clear();
    lcd1.print("BLOCKED");
    lcd2.print("BLOCKED");
    return;
  }
  if(A==1){
    lcd1.setCursor(0,0);
    lcd2.setCursor(0,0);
    lcd1.clear();
    lcd2.clear();
    lcd1.print( "VALID CODE" );
    lcd2.print( "VALID CODE" );
    delay(2000);
    state = 1;
  }
  if(A==2); {
    state=2;
    lcd1.clear();
    lcd2.clear();
    lcd1.setCursor(0,0);
    lcd2.setCursor(0,0);
    lcd1.print( " Pairing..." );
    lcd2.print( " Pairing..." );
    delay(2000);
    return;}
}
break;

}

case 1:{
lcd1.clear();
lcd2.clear();
lcd1.setCursor(0,2);
lcd2.setCursor(0,2);
lcd1.print( "Welcome Home" );
lcd2.print( "Welcome Home" );
lcd1.setCursor(1,2);
lcd2.setCursor(1,2);
lcd2.print( "Door Unlocked" );
digitalWrite(A3,LOW);
digitalWrite(A1,LOW); //Red LED off
digitalWrite(A2,HIGH); //Green LED on
tone(6,3000,5010); //Buzzer will sound
delay(10000); //After 10 seconds the system will be reset
digitalWrite(A3,HIGH);
digitalWrite(A1,HIGH);
digitalWrite(A2,LOW);
state=0;
lcd1.clear();
lcd2.clear();
lcd1.setCursor(0,2);
lcd2.setCursor(0,2);
lcd1.print( "Access Denied" );
lcd2.print( "Access Denied" );
lcd1.setCursor(1,3);
lcd1.setCursor(1,3);
lcd1.print( "DOOR LOCKED" );
lcd2.print( "DOOR LOCKED" );
return;
}

case 2:{
mfrc522.PCD_Init();
if ( mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial() ){
readNFC();
pairNFC();
state=0;
delay(2000);
lcd1.clear();
lcd2.clear();
lcd1.setCursor(0,5);
lcd2.setCursor(0,5);
lcd1.print( "BLOCK" );
lcd2.print( "BLOCK" ); }

break;

}
}
}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Also, please post a schematic of your project. A photo of a hand drawn circuit is good enough

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