Remote Keyless Entry - RKE

 We at CruizByte Mobility Solution (www.cruizbyte.co.in) implemented RKE logic based on RKE requirements from https://cruizbyte.co.in/training-blog/f/remote-keyless-entry


The source for the project is given below.


 

//Created by SREEDHARAN S

//Created on date: 27/09/2022

//Software – Arduino 

 

//======Program for Remote Keyless Entry=====================

 

#include <SPI.h>            // communicates using SPI, so include the library:

#include <mcp2515.h>   //CAN interface library

 

struct can_frame canMsg;    //Message used to receive a CAN message

 

//Message used to transmit an CAN message

struct can_frame canMsg1;        //Transmission-cylinder  OFF

struct can_frame canMsg2;        //T- cylinder  Acc

struct can_frame canMsg3;        //T- cylinder  ON

struct can_frame canMsg4;        //T- cylinder  Crank

struct can_frame canMsg5;        //T-DoorStatus_DriverDoor

struct can_frame canMsg6;        //T-DoorStatus_FrontPassenger

struct can_frame canMsg7;        //T-DoorStatus_RearRight

struct can_frame canMsg8;        //T-DoorStatus_RearLeft

struct can_frame canMsg9;        //T-Key Fob Lock

struct can_frame canMsg10;      //T-Key Fob  UnLock

struct can_frame canMsg11;      //T-Key Fob Trunk

struct can_frame canMsg12;      //T- Key Fob  Search

struct can_frame canMsg13;      //For Auto Relock Function

struct can_frame canMsg14;      //Auto Lock Function

struct can_frame canMsg15;      //Auto Unlock Function

struct can_frame canMsg16;      //Panic Alarm Function

struct can_frame canMsg17;      //Center Lock Function

struct can_frame canMsg18;      //Theft Alarm Function

struct can_frame canMsg19;      //Emergency unlocking Function

struct can_frame canMsg20;       //Special Circumstances

struct can_frame canMsg21;       //Mute Function

struct can_frame canMsg22;      //Beep Function

 

MCP2515 mcp2515(10);            //Setting to Arduino UNO

 

//Declaration of ID for CAN communication

bool id_200_value[1];     //for cylinder status

bool id_220_value[1];     //for door switch status

bool id_240_value[1];     //for Key Fob status

 

//Initializing the variable for remote keyless entry Button

bool keyfob;

bool k_lock;                    //Keyfob lock

bool k_unlock;                //Keyfob Unlock

bool k_trunk;                  //Keyfob Trunk

 

//Initializing the variable for Cylinder Status

//Cylinder Status

bool CylinderStatus;

bool CylinderStatus_Off;

bool CylinderStatus_Acc;

bool CylinderStatus_ON ;

bool CylinderStatus_Crank ;

 

//Initializing the variable for the Door Switch

//Door switch status

bool d_door;

bool fl_door;

bool rr_door;

bool rl_door;

bool Tr_door;

 

//Initializing the variable for the Solenoid Valves

//solenoid valve status

bool D_sv;

bool FL_sv;

bool RR_sv;

bool RL_sv;

bool Trunk_sv;

 

int ledPin = 13;      //variable declaration of Ledpin

 

//Declaration of initialize stages

bool Door_Switch_Status = LOW;       //1 = LOCK

bool Door_valve_status   = HIGH;        //1 = HIGH

bool KeyFobs_status;

 

//For timer

int k_unlock_timer = 3;   //3sec

//variable declaration for vehicle speed

int VehicleSpeed;

 

//Output device variable declaration

bool Airbag_ECU = 1;

//==========================================

void setup() {

 

  Serial.begin(9600);       //To initialize serial communication at 9600bps

 

//declaration of CAN ID, CAN data length and CAN data

  canMsg1.can_id  = 0x100;      //T-cylinder  Off

  canMsg1.can_dlc = 1;

  canMsg1.data[0] = 0x01;

 

  canMsg2.can_id  = 0x105;      //T- cylinder  Acc

  canMsg2.can_dlc = 1;

  canMsg2.data[0] = 0x02;

 

  canMsg3.can_id  = 0x110;      //T- cylinder  ON

  canMsg3.can_dlc = 1;

  canMsg3.data[0] = 0x04;

 

  canMsg4.can_id  = 0x115;      //T- cylinder  Off

  canMsg4.can_dlc = 1;

  canMsg4.data[0] = 0x08;

 

  canMsg5.can_id  = 0x120;      //T-DoorStatus_DD

  canMsg5.can_dlc = 1;

  canMsg5.data[0] = 0x01;

 

  canMsg6.can_id  = 0x125;      //T-DoorStatus_FL

  canMsg6.can_dlc = 1;

  canMsg6.data[0] = 0x02;

 

  canMsg7.can_id  = 0x130;      //T-DoorStatus_RR

  canMsg7.can_dlc = 1;

  canMsg7.data[0] = 0x04;

 

  canMsg8.can_id  = 0x135;      //T-DoorStatus_RL

  canMsg8.can_dlc = 1;

  canMsg8.data[0] = 0x08;

 

  canMsg9.can_id  = 0x140;      //T-KeyFob Lock

  canMsg9.can_dlc = 1;

  canMsg9.data[0] = 0x01;

 

  canMsg10.can_id  = 0x145;    //T- Key Fob  UnLock

  canMsg10.can_dlc = 1;

  canMsg10.data[0] = 0x02;

 

  canMsg11.can_id  = 0x150;    //T- Key Fob  Trunk

  canMsg11.can_dlc = 1;

  canMsg11.data[0] = 0x04;

 

  canMsg12.can_id  = 0x155;    //T- Key Fob  Search

  canMsg12.can_dlc = 1;

  canMsg12.data[0] = 0x08;

 

  canMsg13.can_id  = 0x160;    //T-Auto Relock Function.

  canMsg13.can_dlc = 1;

  canMsg13.data[0] = 0x01;

 

  canMsg14.can_id  = 0x165;    //T-Auto Lock Function.

  canMsg14.can_dlc = 1;

  canMsg14.data[0] = 0x01;

 

  canMsg15.can_id  = 0x170;    //T-Auto Unlocking Function

  canMsg15.can_dlc = 1;

  canMsg15.data[0] = 0x01;

 

  canMsg16.can_id  = 0x175;    //T-Panic Alarm Function

  canMsg16.can_dlc = 1;

  canMsg16.data[0] = 0x01;

 

  canMsg17.can_id  = 0x180;    //T-Center Lock Function

  canMsg17.can_dlc = 1;

  canMsg17.data[0] = 0x01;

 

  canMsg18.can_id  = 0x185;    //T-Theft Alarm Function

  canMsg18.can_dlc = 1;

  canMsg18.data[0] = 0x01;

 

  canMsg19.can_id  = 0x190;    //T-Emergency Unlocking Function

  canMsg19.can_dlc = 1;

  canMsg19.data[0] = 0x01;

 

  canMsg20.can_id  = 0x205;    //Special Circumstances

  canMsg20.can_dlc = 1;

  canMsg20.data[0] = 0x1;

 

  canMsg21.can_id  = 0x210;    //Mute Function

  canMsg21.can_dlc = 1;

  canMsg21.data[0] = 0x1;

 

  canMsg22.can_id  = 0x215;    //Beep Function

  canMsg22.can_dlc = 1;

  canMsg22.data[0] = 0x1;

 

  mcp2515.reset();

  mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ);          //setting bitrate for can protocol

  mcp2515.setNormalMode();              //initialization of normal mode

 

}

// ==================Main Loop =======================

void loop() {

//To read CAN message

  if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) {

 

    switch (canMsg.can_id) {

      //=======CAN MSG_ID - 200====CYLINDER STATUS================

      case (0x200):

        for (int i = 0; i < canMsg.can_dlc; i++)  {

          id_200_value[i] = (canMsg.data[i]);

          Serial.println("This is CAN Message id_200");

          //Serial.println(id_200_value[i]);

           }

        switch (id_200_value[0]) {

          case 1:

            CylinderStatus = CylinderStatus_Off;         //assigning cylinder status to cylinder off

            Serial.println("CylinderStatus_Off");

            Serial.println();         //Empty line

            AutoUnlocking();      //calling Auto locking function

            mcp2515.sendMessage(&canMsg1);    //declaration of a CAN message

            break;

 

          case 2:

            CylinderStatus = CylinderStatus_Acc;        //assigning cylinder status to cylinder Acc

            Serial.println("CylinderStatus_Acc");

            PanicAlarm();     //calling PanicAlarm function

            CenterLock();     //calling CenterLock function

            Serial.println();   //Empty line

            mcp2515.sendMessage(&canMsg2);           //declaration of a CAN message

            break;

 

          case 4:

            CylinderStatus = CylinderStatus_ON;         //assigning cylinder status to cylinder ON

            Serial.println("CylinderStatus_ON");

            Serial.println();   //Empty line

 

            AutoLock();        //calling AutoLock function

            mcp2515.sendMessage(&canMsg3);           //declaration of a CAN message

            break;

 

          case 8:

            CylinderStatus = CylinderStatus_Crank;     //assigning cylinder status to cylinder Crank

            Serial.println("CylinderStatus_Crank");

            Serial.println();

            mcp2515.sendMessage(&canMsg4);

            break;

 

          default:

            //Serial.println("Invalid");

            Serial.println();

        }

        Serial.println();

        break;

 

      //======CAN MSG_ID - 220===DOOR-SWITCH============

      case (0x220):

 

        for (int i = 0; i < canMsg.can_dlc; i++)  {

          id_220_value[i] = (canMsg.data[i]);

          Serial.println("This is CAN Message id_220");

        }

        switch (id_220_value[0]) {

          case 1:

            d_door = LOW;             //initialize driver door is closed

            Serial.println("DoorStatus_DD");

            Serial.println();

            mcp2515.sendMessage(&canMsg5);

            break;

          case 2:

            fl_door = LOW;            //initialize front passenger door is closed

            Serial.println("DoorStatus_FL");

            Serial.println();

            mcp2515.sendMessage(&canMsg6);

            break;

          case 4:

            rr_door = LOW;            //initialize rear right door is closed

            Serial.println("DoorStatus_RR");

            Serial.println();

            mcp2515.sendMessage(&canMsg7);

            break;

          case 8:

            rl_door = LOW;            //initialize rear left door is closed

            Serial.println("DoorStatus_RL");

            Serial.println();

            mcp2515.sendMessage(&canMsg8);

            break;

 

          default:

            Serial.println("Invalid");

            Serial.println();

        }

        Serial.println();

        break;

 

      //========CAN MSG_ID - 240==KEYFOB==================

      case (0x240):

 

        for (int i = 0; i < canMsg.can_dlc; i++)  {

          id_240_value[i] = (canMsg.data[i]);

          Serial.println("This is CAN Message id_240");

      }

        switch (id_240_value[0]) {

          case 1:

            k_lock = 1;                            //When lock button is pressed

            Lock();                                  //Activates lock function

            TheftAlarm();                        //Activates Theft Alarm function

            Emergency_UnLock();            //Activates Emergency UnLock function

            Special_Circumstances ();      //Activates Special Circumstances function

            Serial.println("Lock");

            Serial.println();

            break;

 

          case 2:

            k_unlock = 1;               //When unlock button is pressed

            UnLock();                    //Activates Unlock function

            AutoRelock();               //Activates AutoRelock function

            Search();                     //Activates Search function

            Serial.println("Unlock");

            Serial.println();

            break;

 

          case 4:

            k_trunk = 1;                //When unlock button is pressed

            Trunk();                       //Activates Trunk function

            Serial.println("Trunk");

            Serial.println();

            break;

 

          default:

            Serial.println("Invalid");

            Serial.println();

        }

        Serial.println();

        break;

    }

  }

}

//*=========Function to lock all the door ==================

 

void Lock() {     //To lock the vehicle

  D_sv     = HIGH;

  FL_sv    = HIGH;

  RR_sv    = HIGH;

  RL_sv    = HIGH;

  Trunk_sv = HIGH;

 

  Door_Switch_Status = LOW;    // Door Closed Stage

  Door_valve_status  = HIGH;     // In Valve lock stage

  Serial.println("1.All doors are locked successfully");

  mcp2515.sendMessage(&canMsg9);

          //Below functions are checked simultaneously

          //TheftAlarm();

          //Emergency_UnLock();

          //Special_Circumstances ();

  delay (10);

  Hazard_Lamp();            //Hazard function activated

}

 

//*============Function to Unlock all the door ==================

 

void UnLock () {    //To Unlock the vehicle

  D_sv  = LOW;

  FL_sv = LOW;

  RR_sv = LOW;

  RL_sv = LOW;

  Hazard_Lamp();

  Serial.println("02 All doors are Unlocked successfully ");

 

          //Below functions are checked simultaneously

          //AutoRelock();

          //Search();

  mcp2515.sendMessage(&canMsg10);

  Door_Switch_Status = LOW;   // In Door closed stage

  Door_valve_status  = LOW;   // Valve locked stage

}

 

//*==========Function to Unlock the trunk door =================

 

void Trunk () {                //To Unlock the Trunk door

  Trunk_sv = LOW;          //trunk door valve is low

  Hazard_Lamp();

  Serial.println("03 Trunk unlock");

  mcp2515.sendMessage(&canMsg11);

  Door_Switch_Status = LOW;             // In Unlock stage

  Door_valve_status  = LOW;              // Switch in Low status

}

//*===========Function to Search the vehicle ===================

 

void Search () {                        //To Search the vehicle

  if (k_unlock_timer >= 3) {      //pressing keyfob_unlock for 3seconds

    Hazard_Lamp();                   //Hazard functions activated for 30sec

    Serial.println("04 Search function ON");

    mcp2515.sendMessage(&canMsg12);

    Mute_Function(); //Used to mute the alarm  by pressing key fob lock for 3 sec when needed

  }

}

//*=======Function to AutoRelock the vehicle ===============

void AutoRelock() {                   //Vehicle get unlocked after 45 sec if no doors are opened

    if (mills >= 45000 ) {           //assume as 45 sec

    if (Door_Switch_Status = LOW)       // In closed stage

      if (Door_valve_status = HIGH)      // valve in Low

        Serial.println("5 AutoRelock Activated");

    mcp2515.sendMessage(&canMsg13);

    return;

  }

//If any door are opened

  else if (d_door == HIGH || fl_door == HIGH || rr_door == HIGH || rl_door == HIGH || Trunk_sv == HIGH) {

    Serial.println("5 NO AutoRelock");

    mcp2515.sendMessage(&canMsg13);

    Door_Switch_Status = HIGH;

    Door_valve_status  = LOW;

    return;

  }

}

//*=========Function to AutoLock the vehicle ==================

void AutoLock() {

  if (CylinderStatus == CylinderStatus_ON) {

    if (VehicleSpeed >= 20) { //vehicle speed is more than 20kmph

      Door_valve_status = HIGH;     //valve is high (Door is Locked)

      mcp2515.sendMessage(&canMsg14);

      Serial.println("6 Autolock Activated");

    }

  }

}

//*=======Function to AutoUnLock the vehicle =================

void AutoUnlocking() {

  if (CylinderStatus == CylinderStatus_Off) {

    if (VehicleSpeed == 0) {

      Door_valve_status = LOW;    //valve in low condition(doors are closed)

      mcp2515.sendMessage(&canMsg15);

      Serial.println(" 7 AutoUnlocking Activated");

    }

  }

}

//*========Function to Panic Alarm the vehicle ============

void PanicAlarm() {

  if (CylinderStatus == CylinderStatus_Acc) {

                   //Should initilize timer for >= 3 sec while pressing unlock button

    if (k_unlock_timer >= 3) {             //pressing keyfob_unlock for 3seconds

      Serial.println("8 PanicAlarm Activated");

      Hazard_Lamp();       //Hazard functions activated

                  //Mute function activated if lock button is pressed for 3 sec to mute the alarm

      Mute_Function();

      mcp2515.sendMessage(&canMsg16);

    }

  }

}

//*=========Function to CenterLock the vehicle =================

void CenterLock() {

  if (Door_Switch_Status == LOW) {   //All doors are closed

    if (D_sv == HIGH)                          //if driver door is locked

      Door_valve_status = HIGH;          //All the valves become high

    Serial.println("9 CenterLock is Activated");

    mcp2515.sendMessage(&canMsg17);       // CenterLock CAN message

  }

  if (Door_Switch_Status == LOW) {             //All doors are closed

    if (D_sv == LOW)                                    //if driver door is Unlocked

      Door_valve_status = LOW;                     //All the valves become low

    Serial.println("9 CenterLock");

    mcp2515.sendMessage(&canMsg17);

  }

}

//*==========Function to TheftAlarm the vehicle ==================

void TheftAlarm() {

  if  (k_lock == HIGH ) {           //when keyfob is high

          //Anti-theft alarm activation while opening of any doors after vehicle armed

 if (D_sv == LOW || FL_sv == LOW || RR_sv == LOW || RL_sv == LOW || Trunk_sv == LOW || d_door == HIGH || fl_door == HIGH || rr_door == HIGH || rl_door == HIGH || Trunk_sv == HIGH) {

       Hazard_Lamp ();

      Serial.println("10 TheftAlarm Activated");

      mcp2515.sendMessage(&canMsg18);

         Mute_Function();

      //Beep_Function();

    }

  }

}

//===============Emergency Unlocking function===============

void Emergency_UnLock() {

  if (Airbag_ECU == 1) {

    Door_valve_status = LOW;    //valve in low condition

    //Harzard lamp blinks

    Hazard_Lamp();

    Serial.println("11 Emergency_UnLock");

    mcp2515.sendMessage(&canMsg19);

    Mute_Function();

  }

}

//Special Circumstances     

/*Special conditions if any doors opened with RKE locked hazard lamp flash for five times with alarm */

void Special_Circumstances () {

  if (k_lock == HIGH) {

    if (d_door == HIGH || fl_door == HIGH || rr_door == HIGH || rl_door == HIGH || Tr_door == HIGH) {

      Beep_Function();

      Hazard_Lamp();

      Serial.println("12 SpecialCircumstances");

      mcp2515.sendMessage(&canMsg20);

      Mute_Function();

    }

//Condition to lock the door if driver door closed but any other door opened ======

    if (d_door == LOW) {

      D_sv == HIGH;

    }

    if (fl_door == LOW) {

      FL_sv == HIGH;

    }

    if (rr_door == LOW) {

      RR_sv == HIGH;

    }

    if (rl_door == LOW) {

      RL_sv == HIGH;

    }

    if (Tr_door == LOW) {

      Trunk_sv == HIGH;

    }

  }

}

//=================Mute Function========================

void Mute_Function() {

  if (k_lock >= 3) { //Holding lock button for 3sec

    //Beep and Alarm muted

    Serial.println("13 Mute_Function");

    mcp2515.sendMessage(&canMsg21);

  }

}

//================Beep Function========================

void Beep_Function() {

  //if (CylinderStatus == CylinderStatus_Acc) {

  // if (Vehicle_Speed == 0) {    // stops after a drive

  if (d_door == HIGH || fl_door == HIGH || rr_door == HIGH || rl_door == HIGH || Tr_door == HIGH) {

    Serial.print("14 Beep ON");

    mcp2515.sendMessage(&canMsg22);

    // }

    //}

  }

  else

    Door_Switch_Status == LOW;        // No Beep

  Serial.print("14 NO Beep");

  mcp2515.sendMessage(&canMsg22);

}

 

//============ Hazard and Alarm function ===================

void Hazard_Lamp() {     

 while (millis() <= 30000)

  digitalWrite(ledPin, HIGH);

  delay (500);

  digitalWrite(ledPin, LOW);

  delay (500);

  Serial.println("15 Hazard lamp with alarm horn activated for 30s");

}

//=========End=========================

Test Report