Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //****************************************************************************************
- //Define Machine States (Begin)
- //Define and label the number of states in the FSM
- //****************************************************************************************
- #define mSTATE_1 1 //S1 - L_CHOICE (LOCK CHOICE)
- #define mSTATE_2 2 //S2 - G_PASS (GUEST PASSWORD)
- #define mSTATE_3 3 //S3 - R_PASS (RENTAL PASSWORD)
- #define mSTATE_4 4 //S4 - M_PASS (MASTER PASSWORD)
- #define mSTATE_5 5 //s5 - D_UNLOCK (DOOR UNLOCK)
- #define mSTATE_6 6 //s6 - ALARM (BUZZER RING)
- #define mSTATE_7 7 //s7 - M_CHECK (PROMPT USER TO ENTER MASTER PASSWORD)
- #define mSTATE_8 8 //s8 - O_PASS (PASSWORD OPTION)
- #define mSTATE_9 9 //s9 - N_PASS (ENTER NEW PASSWORD SCREEN)
- #define mSTATE_10 10 //s10 - RN_PASS (RE_ENTER NEW PASSWORD SCREEN)
- #define mSTATE_11 11 //s11 - S_PASS (PASSWORD CHANGE SUCCESS)
- #define mSTATE_12 12 //s12 - F_PRINT (FINGERPRINT LOCK)
- #define mSTATE_13 13 //s13 - F_CHANGE (FINGERPRINT CHANGE)
- #define mSTATE_14 14 //s14 - F_CONFIRM (FINGERPRINT CONFIRMATION)
- #define mSTATE_15 15 //s15 - UNLOCK? (UNLOCK THE DOOR OT NOT)
- #define mSTATE_16 16 //s16 - DOOR_O (DOOR OPEN)
- #define mSTATE_18 17 //s17 - L_ON (LIGHTS ON)
- #define mSTATE_19 18 //s18 - F_ON (CELING FAN ON)
- #define mSTATE_20 19 //s19 - AC_ON (AIR-CONDITIONAL ON)
- //****************************************************************************************
- //Define Machine States (End)
- //****************************************************************************************
- //****************************************************************************************
- //Define GPIO Labels (Begin)
- //Define and label all GPIOs with meaningful names.
- //****************************************************************************************
- #define BUZZER A2 // GPIO2 (Output) for Grove Buzzer Control
- #define RLY_LOCK 6 // GPIO6 (Output) for Relay Control (Door Lock)
- #define DOOR_SENSOR 12 // GPIO4 (Input) for Door Position Sensing
- #define M_SENSOR1_PIN 8 // GPIO2 (Input) for Parallex PIR Motion
- #define M_SENSOR2_PIN 10 // GPIO2 (Input) for Parallex PIR Motion
- #define TMP_SENSOR_PIN A0 // A0 (Input) for Grove Temperature Sensor Module.
- #define RLY_LIGHT 2 // GPIO2 for Relay Control (LIGHTS)
- #define RLY_CFAN 3 // GPIO3 for Relay Control (CELING FAN)
- #define RLY_VFAN 4 // GPIO4 for Relay Control (VANDILATION FAN)
- #define RLY_AC 5 // GPIO5 for Relay Control (AIR-CONDITION)
- //****************************************************************************************
- //Define GPIO Labels (End)
- //****************************************************************************************
- //****************************************************************************************
- //Define Signals, Messages and Functions (Begin)
- //Define and label control signals and messages.
- //****************************************************************************************
- #define TT1 25 // Temperature Threshold
- #define TT2 32 // Temperature Threshold
- #define LT 300 // Light Intensity Threshold
- #define ON 1 // Turn on buzzer (Buzzer On)
- #define OFF 0 // Turn of buzzer (Buzzer Off)
- #define DETECTED 1
- #define NONE 0
- //#define DISPLAY_DT DisplayDistance(DISTANCE) // Function to Display Measured Distance
- #define DOOR_L 1 // Door Lock (Lock Position)
- #define DOOR_U 0 // Door Magnetic Lock (Unlock Position)
- #define DOOR_O 0 // Door Sensor is in Open Position
- #define DOOR_C 1 // Door Sensor is in Close Posotion
- #define W_PASS 0 // Wait for User to Input Password
- #define M_PASS 1 // Master Password
- #define R_PASS 2 // Rental Password
- #define G_PASS 3 // Guest Password
- #define O_PASS 4 // Password Option
- #define W_KEY 0 // Wait for User to Input Key
- #define PASS_L6 6 // Password Length to 6
- #define PASS_L16 16 // Password Length to 6
- #define R_L 3 // Password Retry Limit to 3
- #define PASS_RG "123456" // Default Password
- #define PASS_M "1234567890ABCD11" // Default Password
- #define R_CT 0 // Password Retry Counter to 0
- #define DOOR_L digitalWrite(RLY_LOCK, HIGH)
- #define DOOR_U digitalWrite(RLY_LOCK, LOW)
- #define BUZZER_ON digitalWrite(BUZZER, HIGH)
- #define BUZZER_OFF digitalWrite(BUZZER, LOW)
- #define KEY_E KEY_E = true
- #define KEY_D KEY_E = false
- #define LIGHT_ON digitalWrite(RLY_LIGHT, LOW)
- #define LIGHT_OFF digitalWrite(RLY_LIGHT, HIGH)
- #define CFAN_ON digitalWrite(RLY_CFAN, LOW)
- #define CFAN_OFF digitalWrite(RLY_CFAN, HIGH)
- #define VFAN_ON digitalWrite(RLY_VFAN, LOW)
- #define VFAN_OFF digitalWrite(RLY_VFAN, HIGH)
- #define AC_ON digitalWrite(RLY_AC, LOW)
- #define AC_OFF digitalWrite(RLY_AC, HIGH)
- //****************************************************************************************
- //Define Signals, Messages and Functions (End)
- //****************************************************************************************
- //****************************************************************************************
- //Include (Begin)
- //Place all include files (preprocessor directives, datatypes, etc) here.
- //****************************************************************************************
- #include <Wire.h>
- //#include <Grove_LED_Bar.h>
- //#include <Ultrasonic.h>
- #include <math.h>
- #include <Digital_Light_TSL2561.h>
- #include <Keypad.h>
- #include <EleF_LiquidCrystal.h>
- #include <stdio.h>
- //****************************************************************************************
- //Include (End)
- //****************************************************************************************
- //****************************************************************************************
- //External Peripherals Resources Initialization (Begin)
- //Place all declarations and codes related to external peripherals initializations here.
- //External Perihperals: Keypad, LCD Module, etc.
- //****************************************************************************************
- //Grove_LED_Bar bar(LED_CLK_PIN, LED_DATA_PIN, 0); // Grove LED Bar Module Clock pin,
- // Data pin and Orientation setup.
- //Ultrasonic ultrasonic(ULTRASONIC_PIN); // Grove Untrasonic Module setup for
- // for Assigned Pin.
- unsigned int KCOUNT = 0; // Record number of times a keypad is pressed
- char KUSER_PASSWORD[PASSWORD_LEN+1]; // Record user password input
- const byte KROWS = 4; // Four rows
- const byte KCOLS = 4; // Four columns
- char keys[KROWS][KCOLS] = { // Define the Keymap
- {'1','2','3', 'A'},
- {'4','5','6', 'B'},
- {'7','8','9', 'C'},
- {'*','0','#', 'D'}
- };
- byte KrowPins[KROWS] = { 39, 41, 43, 45 }; // Connect keypad ROW1, ROW2, ROW3 and ROW4 to these Arduino pins.
- byte KcolPins[KCOLS] = { 47, 49, 51, 53 }; // Connect keypad COL1, COL2, COL3 and COL4 to these Arduino pins.
- Keypad keypad = Keypad( makeKeymap(keys), KrowPins, KcolPins, KROWS, KCOLS ); // Create the Keypad
- //LCD Module Peripheral
- EleF_LiquidCrystal lcd(0);
- //****************************************************************************************
- //External Peripherals Resources Initialization (End)
- //****************************************************************************************
- //****************************************************************************************
- //Global Variables (Begin)
- //Declare global variables for use by application here.
- //****************************************************************************************
- unsigned int MACHINE_STATE; // Variable to hold current machine state
- // (Do NOT Remove).
- bool BUZZER; // Variable to control buzer ON / OFF.
- // long DISTANCE; // Variable to hold Measured Distance in CM.
- // int B_CTR; // Variable to determine buzzer Beep Length.
- unsigned long L; // Ambient light intensity reading
- float T; // Ambient temperature reading
- bool MOTION; // Motion sensing result
- const int B = 4275; // B Value of the NTC Thermistor.
- const int R0 = 100000; // R0 = 100k
- int TIMER = 0;
- unsigned int KEY; // Variable to store number entered by user
- unsigned int R_CT; // User Password Retry Counter
- unsigned int P_STAT; // Variable to store Password Status
- unsigned int D_STAT; // Variable to store Door Position Sensor Status
- unsigned int K_STAT; // Variable to store Key Status
- boolean KEY_E; // Flag Variable to store Keypad Enable / Disable Signal (true, false)
- //****************************************************************************************
- //Global Variables - End
- //****************************************************************************************
- //****************************************************************************************
- // setup() function that run once during startup (Begin)
- //****************************************************************************************
- void setup()
- {
- Wire.begin(); // Initialize I2C
- Serial.begin(9600); // Initialize serial port for debug.
- analogReadResolution(10); // Set ADC resolution to 10-bit.
- pinMode(M_SENSOR1_PIN, INPUT); // Set GPIO pin (input) to read Motion Sensor 1
- pinMode(M_SENSOR2_PIN, INPUT); // Set GPIO pin (input) to read Motion Sensor 2
- pinMode(RLY_LIGHT, OUTPUT); // Set GPIO pin to control Light Relay
- pinMode(RLY_CFAN, OUTPUT); // Set GPIO pin to control Ceiling Fan Relay
- pinMode(RLY_VFAN, OUTPUT); // Set GPIO pin to control Exhaust Fan Relay
- pinMode(RLY_AC, OUTPUT); // Set GPIO pin to control Exhaust Fan Relay
- pinMode(BUZZER_PIN, OUTPUT); // Set GPIO pin controlling BUZZER to OUTPUT.
- //TSL2561.init(); // Initialize Grove Digital Light Sensor Module
- //bar.begin(); // Initialize Grove LED Bar.
- pinMode(BUZZER, OUTPUT); // Initialize GPIO2 pin as an output
- pinMode(RLY_LOCK, OUTPUT); // Initialize GPIO7 pin as an output
- pinMode(DOOR_SENSOR, INPUT); // Initialize GPIO4 pin as an input
- lcd.begin(16, 2); // Initialize LCD of 2 Rows and 16 Columns
- lcd.clear(); // Clear LCD
- LCDPrintMessageStart("Enter Option", 1, true); // Display "Enter Option" on first row, first column of LCD
- LCDPrintMessageStart(NULL, 2, false); // Position cursor at second row, first column of LCD
- // Initial State Initialization - Begin
- // Set Initial State Transition and Action.
- R_CT = 1; // Set Password Retry Counter to 1 (1st attempt)
- KEY_E; // Enable Keypad for User to Enter Password
- BUZZER_OFF; // Turn Off Buzzer (Alarm Off)
- DOOR_L; // Lock the door.
- D_STAT = DOOR_C;
- MACHINE_STATE = mSTATE_1;
- TSL2561.init();
- //Initial State Initialization - End
- }
- //****************************************************************************************
- // setup() function (End)
- //****************************************************************************************
- //****************************************************************************************
- // loop() function that runs repeatedly (Begin)
- //****************************************************************************************
- void loop() // put your main code here, to run repeatedly.
- {
- // Sample Input - Begin
- // Read (or process) and update information
- // from input device at a periodic interval.
- T = MeasureTemperature(); // Measure Temperature and update TEMPERATURE
- // with latest temperature.
- L = MeasureLightIntensity(); // Measure Light Intensity and update
- // INTENSITY with latest light intensity.
- M_SENSOR1 = digitalRead(M_SENSOR1_PIN);// Check PIR Motion Sensor status.
- M_SENSOR2 = digitalRead(M_SENSOR2_PIN);// Check PIR Motion Sensor status.
- // DISTANCE = ultrasonic.MeasureInCentimeters(); // Measure Distance and update DISTANCE
- // with latest value.
- digitalWrite(BUZZER_PIN , BUZZER); // Update Buzzer Pin Status (Buzzer On / Off)
- P_STAT = GetUserPassword (PASSWORD, &KEYPAD_ENABLE);
- D_STAT = digitalRead(DOOR_SENSOR);
- // Sample Input - End
- // Finite State Machine - Begin
- switch(MACHINE_STATE) //
- {
- case mSTATE_1: // State 1
- Serial.println("STATE_1:LOCK CHOICE"); // Debug message.
- if(K_STAT == W_KEY && KEY == '#') // Transition: (Condition)
- {
- lcd.clear();
- LCDPrintMessageStart("Enter Option", 1, true); // Display "Enter Option" on first row, first column of LCD
- LCDPrintMessageStart(NULL, 2, false); // Position cursor at second row, first column of LCD
- MACHINE_STATE = mSTATE_1;
- }
- if(K_STAT == W_KEY && (KEY != '#' || KEY != 1 || KEY != 2 || KEY != 3)) // Transition: (Condition)
- {
- lcd.clear();
- LCDPrintMessageStart("Wrong Option", 1, true); // Display "Wrong Option" on first row, first column of LCD
- LCDPrintMessageStart(NULL, 2, false); // Position cursor at second row, first column of LCD
- MACHINE_STATE = mSTATE_1;
- }
- else if(KEY == 1) // Transition: (Condition)
- {
- MACHINE_STATE = mSTATE_2; // Transition: Jump to G_PASS (State 2)
- }
- else if(KEY == 2) // Transition: Check Condition
- {
- MACHINE_STATE = mSTATE_3; // Transition: Jump to R_PASS (State 3)
- }
- else if(KEY == 3) // Transition: (Condition)
- {
- MACHINE_STATE = mSTATE_4; // Transition: Jump to M_PASS (State 4)
- }
- else
- {}
- break;
- case mSTATE_2: // G_PASS State
- Serial.println("STATE_2:G_PASS"); // Debug message.
- if(P_STAT == W_PASS && R_CT < 3) // Transition: (Condition)
- {
- lcd.clear();
- LCDPrintMessageStart("Guest Password?", 1, true);
- LCDPrintMessageStart(NULL, 2, false); // Position cursor at second row, first column of LCD
- MACHINE_STATE = mSTATE_1;
- }
- else if(KEY == "#" && R_CT < 3) // Transition: (Condition)
- {
- lcd.clear();
- LCDPrintMessageStart("Guest Password?", 1, true);
- LCDPrintMessageStart(NULL, 2, false); // Position cursor at second row, first column of LCD
- MACHINE_STATE = mSTATE_1;
- }
- else if(R_CT < 3 && P_STAT != G_PASS) // Transition: (Condition)
- {
- lcd.clear();
- LCDPrintMessageStart("Try Again :(", 1, true);
- LCDPrintMessageStart(NULL, 2, false); // Position cursor at second row, first column of LCD
- MACHINE_STATE = mSTATE_1;
- R_CT++;
- }
- else if(P_STAT == G_PASS) // Transition: Check Condition
- {
- lcd.clear();
- LCDPrintMessageStart("Correct! :D", 1, true);
- MACHINE_STATE = mSTATE_5; // Transition: Jump to D_UNLOCK (State 5)
- KEY_D;
- DOOR_U;
- R_CT = 0;
- }
- else if(R_CT == 3) // Transition: (Condition)
- {
- lcd.clear();
- LCDPrintMessageStart("Wrong!", 1, true);
- MACHINE_STATE = mSTATE_6; // Transition: Jump to ALARM RING (State 6)
- BUZZER_ON;
- // *add sms here*
- }
- else
- {}
- case mSTATE_3: // G_PASS State
- Serial.println("STATE_3:R_PASS"); // Debug message.
- if(P_STAT == W_PASS && R_CT < 3) // Transition: (Condition)
- {
- lcd.clear();
- LCDPrintMessageStart("Rental Password?", 1, true);
- LCDPrintMessageStart(NULL, 2, false); // Position cursor at second row, first column of LCD
- MACHINE_STATE = mSTATE_1;
- }
- else if(R_CT < 3 && P_STAT != R_PASS) // Transition: (Condition)
- {
- lcd.clear();
- LCDPrintMessageStart("Try Again :(", 1, true);
- LCDPrintMessageStart(NULL, 2, false); // Position cursor at second row, first column of LCD
- MACHINE_STATE = mSTATE_1;
- R_CT++;
- }
- else if(P_STAT == R_PASS) // Transition: Check Condition
- {
- lcd.clear();
- LCDPrintMessageStart("Correct! :D", 1, true);
- MACHINE_STATE = mSTATE_5; // Transition: Jump to D_UNLOCK (State 5)
- KEY_D;
- DOOR_U;
- R_CT = 0;
- }
- else if(R_CT == 3) // Transition: (Condition)
- {
- lcd.clear();
- LCDPrintMessageStart("Wrong!", 1, true);
- MACHINE_STATE = mSTATE_6; // Transition: Jump to ALARM RING (State 6)
- BUZZER_ON;
- // *add sms here*
- }
- else
- {}
- break;
- case mSTATE_4: // G_PASS State
- Serial.println("STATE_4:M_PASS"); // Debug message.
- if(P_STAT == W_PASS && R_CT < 3) // Transition: (Condition)
- {
- lcd.clear();
- LCDPrintMessageStart("Master password?", 1, true);
- LCDPrintMessageStart(NULL, 2, false); // Position cursor at second row, first column of LCD
- MACHINE_STATE = mSTATE_1;
- }
- else if(R_CT < 3 && P_STAT != M_PASS) // Transition: (Condition)
- {
- lcd.clear();
- LCDPrintMessageStart("Try Again :(", 1, true);
- LCDPrintMessageStart(NULL, 2, false); // Position cursor at second row, first column of LCD
- MACHINE_STATE = mSTATE_1;
- R_CT++;
- }
- else if(P_STAT == R_PASS) // Transition: Check Condition
- {
- lcd.clear();
- LCDPrintMessageStart("Correct! :D", 1, true);
- MACHINE_STATE = mSTATE_5; // Transition: Jump to D_UNLOCK (State 5)
- KEY_D;
- DOOR_U;
- R_CT = 0;
- }
- else if(R_CT == 3) // Transition: (Condition)
- {
- lcd.clear();
- LCDPrintMessageStart("Wrong!", 1, true);
- MACHINE_STATE = mSTATE_6; // Transition: Jump to ALARM RING (State 6)
- BUZZER_ON;
- // *add sms here*
- }
- else if(KEY=='#' && TIME == 30) // Transition: Check Condition
- {
- MACHINE_STATE = mSTATE_7; // Transition: Jump to M_CHECK (State 7)
- }
- else
- {}
- break;
- case mSTATE_5: // State 3
- Serial.println("STATE_3: D_UNLOCK"); // Debug message.
- if(D_STAT == DOOR_C) // Transition: (Condition)
- {
- lcd.clear();
- LCDPrintMessageStart("Door Unlocked", 1, true);
- MACHINE_STATE = mSTATE_5; // Transition: Jump to DOOR_O (State 16)
- KEY_D;
- DOOR_U;
- R_CT = 0;
- }
- else if(D_STAT == DOOR_O) // Transition: (Condition)
- {
- lcd.clear();
- LCDPrintMessageStart("Door Opened", 1, true);
- V_FAN = ON;
- MACHINE_STATE = mSTATE_16; // Transition: Jump to DOOR_O (State 16)
- }
- else
- {}
- break;
- case mSTATE_6: // State 4
- Serial.println("STATE_4:Alarm"); // Debug message.
- if((P_STAT == W_PASS || P_STAT == X_PASS) && TIME < 30) // Transition: (Condition)
- {
- lcd.clear();
- LCDPrintMessageStart("Enter Password", 1, true);
- LCDPrintMessageStart(NULL, 2, false);
- TIME++;
- MACHINE_STATE = mSTATE_6; // Transition: Jump to Next State.
- //check with teacher about the countdown timmer
- }
- else if(P_STAT == G_PASS || P_STAT == R_PASS || P_STAT == M_PASS) // Transition: (Condition)
- {
- lcd.clear();
- LCDPrintMessageStart("Correct! :D", 1, true);
- MACHINE_STATE = mSTATE_5; // Transition: Jump to Next State.
- BUZZER_OFF;
- KEY_D;
- R_CT = 0;
- DOOR_U;
- TIME = 0;
- }
- else if(TIME == 30) // Transition: (Condition)
- {
- MACHINE_STATE = mSTATE_1; // Transition: Jump to Next State.
- BUZZER_OFF;
- R_CT = 0;
- TIME = 0;
- }
- else
- {}
- break;
- case mSTATE_7: // State 4
- Serial.println("STATE_7: M_CHECK"); // Debug message.
- if(P_STAT == W_PASS || P_STAT != M_PASS || KEY == '#') // Transition: (Condition)
- {
- lcd.clear();
- LCDPrintMessageStart("Enter Password", 1, true);
- LCDPrintMessageStart(NULL, 2, false);
- TIME = 0;
- MACHINE_STATE = mSTATE_7; // Transition: Jump to Next State.
- //check with teacher about the countdown timmer
- }
- else if(P_STAT == F_FLOCK || P_STAT == M_PASS) // Transition: (Condition)
- {
- MACHINE_STATE = mSTATE_8; // Transition: Jump to Next State.
- }
- else
- {}
- break;
- case mSTATE_8: // State 4
- Serial.println("STATE_8: O_PASS"); // Debug message.
- if(K_STAT == W_KEY) // Transition: (Condition)
- {
- lcd.clear();
- LCDPrintMessageStart("Enter Option", 1, true);
- LCDPrintMessageStart(NULL, 2, false);
- MACHINE_STATE = mSTATE_8; // Transition: Jump to Next State.
- }
- else if(KEY != 1 || KEY != 2 || KEY != 3 || KEY != 4) // Transition: (Condition)
- {
- lcd.clear();
- LCDPrintMessageStart("Enter Right Option", 1, true);
- LCDPrintMessageStart(NULL, 2, false);
- MACHINE_STATE = mSTATE_8; // Transition: Jump to Next State.
- }
- else if(K_STAT == 1) // Transition: (Condition)
- {
- O_PASS = 1;
- MACHINE_STATE = mSTATE_9; // Transition: Jump to Next State.
- }
- else if(K_STAT == 2) // Transition: (Condition)
- {
- O_PASS = 2;
- MACHINE_STATE = mSTATE_9; // Transition: Jump to Next State.
- }
- else if(K_STAT == 3) // Transition: (Condition)
- {
- O_PASS = 3;
- MACHINE_STATE = mSTATE_9; // Transition: Jump to Next State.
- }
- else if(K_STAT == 4) // Transition: (Condition)
- {
- MACHINE_STATE = mSTATE_12; // Transition: Jump to Next State.
- }
- else
- {}
- break;
- case mSTATE_9: // State 4
- Serial.println("STATE_9: N_PASS"); // Debug message.
- if(K_STAT == W_KEY || KEY == '#') // Transition: (Condition)
- {
- lcd.clear();
- LCDPrintMessageStart("Enter New Password", 1, true);
- LCDPrintMessageStart(NULL, 2, false);
- MACHINE_STATE = mSTATE_9; // Transition: Jump to Next State.
- }
- else if(KEY == '#' && TIME == 30) // Transition: (Condition)
- {
- lcd.clear();
- LCDPrintMessageStart("Enter Password", 1, true);
- LCDPrintMessageStart(NULL, 2, false);
- MACHINE_STATE = mSTATE_9; // Transition: Jump to Next State.
- //ASK CHER HOW TO RECORD NEW PASS
- }
- else if(KEY == '*') // Transition: (Condition)
- {
- //ASK CHER HOW RECORD THE NEW PASSWORD
- MACHINE_STATE = mSTATE_10; // Transition: Jump to Next State.
- }
- else
- {}
- break;
- case mSTATE_10: // State 4
- Serial.println("STATE_10: RN_PASS"); // Debug message.
- if(P_STAT == W_PASS || KEY == '#') // Transition: (Condition)
- {
- lcd.clear();
- LCDPrintMessageStart("Re-Enter Password", 1, true);
- LCDPrintMessageStart(NULL, 2, false);
- MACHINE_STATE = mSTATE_10; // Transition: Jump to Next State.
- }
- else if(KEY == '*' && KEY == R_KEY && O_PASS == 1) // Transition: (Condition)
- {
- //ASK CHER HOW RECORD THE NEW PASSWORD
- G_PASS == R_KEY;
- MACHINE_STATE = mSTATE_11; // Transition: Jump to Next State.
- }
- else if(KEY == '*' && KEY == R_KEY && O_PASS == 2) // Transition: (Condition)
- {
- //ASK CHER HOW RECORD THE NEW PASSWORD
- R_PASS == R_KEY;
- MACHINE_STATE = mSTATE_11; // Transition: Jump to Next State.
- }
- else if(KEY == '*' && KEY == R_KEY && O_PASS == 3) // Transition: (Condition)
- {
- //ASK CHER HOW RECORD THE NEW PASSWORD
- M_PASS == R_KEY;
- MACHINE_STATE = mSTATE_11; // Transition: Jump to Next State.
- }
- else
- {}
- break;
- case mSTATE_11: // State 4
- Serial.println("STATE_11: S_PASS"); // Debug message.
- if(K_STAT == W_KEY) // Transition: (Condition)
- {
- lcd.clear();
- LCDPrintMessageStart("Change Successful!", 1, true);
- MACHINE_STATE = mSTATE_10; // Transition: Jump to Next State.
- }
- else if(KEY == '*') // Transition: (Condition)
- {
- //ASK CHER HOW RECORD THE NEW PASSWORD
- KEY_D;
- DOOR_U;
- R_CT = 0;
- MACHINE_STATE = mSTATE_5; // Transition: Jump to Next State.
- }
- else if(KEY == '#') // Transition: (Condition)
- {
- //ASK CHER HOW RECORD THE NEW PASSWORD
- R_CT = 0;
- TIME = 0;
- MACHINE_STATE = mSTATE_1; // Transition: Jump to Next State.
- }
- else
- {}
- break;
- case mSTATE_12: // State 4
- Serial.println("STATE_12: F_PRINT"); // Debug message.
- if(K_STAT == W_PRINT) // Transition: (Condition)
- {
- lcd.clear();
- LCDPrintMessageStart("Place Your Thump", 1, true);
- MACHINE_STATE = mSTATE_12; // Transition: Jump to Next State.
- }
- else if(R_CT < 20 && F_STAT != F_PRINT) // Transition: (Condition)
- {
- lcd.clear();
- LCDPrintMessageStart("Wrong", 1, true);
- R_CT++;
- MACHINE_STATE = mSTATE_12; // Transition: Jump to Next State.
- }
- else if(F_STAT == F_PRINT && R_CT < 20) // Transition: (Condition)
- {
- lcd.clear();
- LCDPrintMessageStart("Finger Reconised!", 1, true);
- R_CT = 0;
- KEY_D;
- DOOR_U;
- MACHINE_STATE = mSTATE_5; // Transition: Jump to Next State.
- }
- else if(F_STAT == F_PRINT && O_PASS == 4) // Transition: (Condition)
- {
- lcd.clear();
- LCDPrintMessageStart("Finger Reconised!", 1, true);
- R_CT = 0;
- MACHINE_STATE = mSTATE_13; // Transition: Jump to Next State.
- }
- else if(F_STAT != F_PRINT && R_CT == 20) // Transition: (Condition)
- {
- lcd.clear();
- LCDPrintMessageStart("ERROR!", 1, true);
- R_CT = 0;
- BUZZER_ON;
- // SMS
- MACHINE_STATE = mSTATE_6; // Transition: Jump to Next State.
- }
- else
- {}
- break;
- case mSTATE_13: // State 4
- Serial.println("STATE_13: F_CHANGE"); // Debug message.
- if(F_STAT == W_PRINT) // Transition: (Condition)
- {
- lcd.clear();
- LCDPrintMessageStart("Place thump", 1, true);
- MACHINE_STATE = mSTATE_13; // Transition: Jump to Next State.
- }
- else if(F_STAT == NF_PRINT) // Transition: (Condition)
- {
- //ASK CHER
- F_PRINT = NF_PRINT;
- MACHINE_STATE = mSTATE_14; // Transition: Jump to Next State.
- }
- else if(KEY == '#') // Transition: (Condition)
- {
- MACHINE_STATE = mSTATE_12; // Transition: Jump to Next State.
- }
- else
- {}
- break;
- case mSTATE_14: // State 4
- Serial.println("STATE_14: F_CONFIRM"); // Debug message.
- if(F_STAT == W_PRINT) // Transition: (Condition)
- {
- lcd.clear();
- LCDPrintMessageStart("Place again", 1, true);
- MACHINE_STATE = mSTATE_14; // Transition: Jump to Next State.
- }
- else if(F_STAT == NF_PRINT) // Transition: (Condition)
- {
- //ASK CHER
- F_PRINT = NF_PRINT;
- MACHINE_STATE = mSTATE_15; // Transition: Jump to Next State.
- }
- else if(KEY == '#') // Transition: (Condition)
- {
- MACHINE_STATE = mSTATE_12; // Transition: Jump to Next State.
- }
- else
- {}
- break;
- case mSTATE_15: // State 4
- Serial.println("STATE_15: UNLOCK?"); // Debug message.
- if(K_STAT == W_KEY) // Transition: (Condition)
- {
- lcd.clear();
- LCDPrintMessageStart("UNLOCK?", 1, true);
- MACHINE_STATE = mSTATE_15; // Transition: Jump to Next State.
- }
- else if(KEY == '*') // Transition: (Condition)
- {
- MACHINE_STATE = mSTATE_5; // Transition: Jump to Next State.
- }
- else if(KEY == '#') // Transition: (Condition)
- {
- MACHINE_STATE = mSTATE_1; // Transition: Jump to Next State.
- }
- else
- {}
- break;
- case mSTATE_16: // State 4
- Serial.println("STATE_16: DOOR_C"); // Debug message.
- if(D_STAT == DOOR_C && M_SENSOR2 == NONE) // Transition: (Condition)
- {
- lcd.clear();
- LCDPrintMessageStart("Door Closed", 1, true);
- DOOR_L;
- R_CT = 0;
- KEY_E;
- MACHINE_STATE = mSTATE_1; // Transition: Jump to Next State.
- }
- else if (D_STAT == DOOR_C && M_SENSOR2 == DETECTED)
- {
- DOOR_L;
- R_CT = 0;
- KEY_E;
- MACHINE_STATE = mSTATE_17; // Transition: Jump to Next State.
- }
- else
- {}
- break;
- case mSTATE_17: // State 4
- Serial.println("STATE_16: TEMP AND LIGHT"); // Debug message.
- if(L < LT) // Transition: (Condition)
- {
- LIGHT_ON;
- MACHINE_STATE = mSTATE_17; // Transition: Jump to Next State.
- }
- else if (L >= LT)
- {
- LIGHT_OFF;
- MACHINE_STATE = mSTATE_17; // Transition: Jump to Next State.
- }
- else if (T >= TT1 && T<=TT2)
- {
- CFAN_ON;
- AC_OFF;
- MACHINE_STATE = mSTATE_17; // Transition: Jump to Next State.
- }
- else if (T > TT2)
- {
- CFAN_ON;
- AC_ON;
- MACHINE_STATE = mSTATE_17; // Transition: Jump to Next State.
- }
- else if (T < TT1)
- {
- CFAN_OFF;
- AC_OFF;
- MACHINE_STATE = mSTATE_17; // Transition: Jump to Next State.
- }
- else if(M_SENSOR2 == NONE) // Transition: (Condition)
- {
- MACHINE_STATE = mSTATE_16; // Transition: Jump to Next State.
- }
- else
- {}
- break;
- // Finite State Machine - End
- delay(100); // Set Loop Refresh
- // (Refresh rate of the Finite State Machine).
- default: //
- MACHINE_STATE = mSTATE_1; // Set the Initial State.
- } //
- }
- //****************************************************************************************
- // loop() function (End)
- //****************************************************************************************
- //****************************************************************************************
- // Extra Section (Custom User Defined Functions) (Begin)
- // This last section is user defined for the purpose of adding custom supporting
- // functions.
- //****************************************************************************************
- //Functions to display message to LCD
- void LCDPrintMessageStart(const char* Message, unsigned int row, boolean clear)
- {
- if(clear)
- {
- lcd.clear(); // Clear LCD
- }
- switch(row)
- {
- case 1:
- lcd.setCursor(0, 0); // Set LCD cursor position to first row, first column
- break;
- case 2:
- lcd.setCursor(0, 1); // Set LCD cursor position to second row, first column
- break;
- default:
- lcd.setCursor(0, 0); // Set LCD cursor position to first row, first column
- }
- if(Message != NULL)
- {
- lcd.print(Message); // Print MESSAGE
- }
- lcd.cursor(); // Turns on cursor
- lcd.blink(); // Makes cursor blink
- }
- void LCDPrintMessageFromLastPosition(const char* Message)
- {
- lcd.print(Message); // Print MESSAGE to current LCD position
- }
- //Function to get user password.
- unsigned int GetUserPassword (const char* Password, bool *KEYPAD_ENABLE)
- {
- static boolean KEYPAD_DISABLED;
- char key = keypad.getKey();
- if(KEYPAD_DISABLED) && (*KEYPAD_ENABLE))
- {
- KEYPAD_DISABLED = false;
- LCDPrintMessageStart("Password?", 1, true);
- LCDPrintMessageStart(NULL, 2, false);
- }
- if(key) && (*KEYPAD_ENABLE))
- {
- KUSER_PASSWORD[KCOUNT] = key;
- KCOUNT= KCOUNT + 1;
- if(KCOUNT == PASSWORD_LEN)
- {
- KUSER_PASSWORD[KCOUNT] = '\0';
- KCOUNT = 0;
- if(strcmp(Password, KUSER_PASSWORD) == 0)
- {
- LCDPrintMessageFromLastPosition("*");
- LCDPrintMessageStart("Password OK!", 1, true);
- return CORRECT_PWD;
- }
- else
- {
- LCDPrintMessageFromLastPosition("*");
- LCDPrintMessageStart("Password ERROR!", 1, true);
- delay (500);
- LCDPrintMessageStart("Password?", 1, true);
- LCDPrintMessageStart(NULL, 2, false);
- return X_PASS;
- }
- }
- else
- {
- LCDPrintMessageFromLastPosition("*");
- return W_PASS;
- }
- }
- else
- {
- if(*KEYPAD_ENABLE == false)
- {
- KEYPAD_DISABLED = true;
- }
- return W_PASS;
- }
- }
- unsigned long MeasureLightIntensity()
- {
- return(TSL2561.readVisibleLux());
- }
- float MeasureTemperature()
- {
- int a = analogRead(TMP_SENSOR_PIN);
- float R = 1023.0/((float)a)-1.0;
- R = 100000.0*R;
- float temperature=1.0/(log(R/100000.0)/B+1/298.15)-273.15; // Convert to temperature
- // via datasheet info.
- return temperature; //ask cher about the variable name
- }
- //void DisplayTemperature(float temperature)
- //{
- // unsigned int value = (unsigned int) temperature / 5;
- // unsigned int x;
- //
- // for(x = 1; x <= 10; x++)
- // {
- // bar.setLed(x, 0);
- // }
- //
- // for(x = 1; x <= value; x++)
- // {
- // bar.setLed(x, 1);
- // }
- //}
- //void DisplayDistance(int distance)
- //{
- // unsigned int value = distance / 20;
- // unsigned int x;
- //
- // if((value <= 10) && (value > 0))
- // {
- // for(x = 1; x <= 10; x++)
- // {
- // bar.setLed(x, 0);
- // }
- //
- // for(x = 1; x <= value; x++)
- // {
- // bar.setLed(x, 1);
- // }
- // }
- // else if(value == 0)
- // {
- // for(x = 1; x <= 10; x++)
- // {
- // bar.setLed(x, 0);
- // }
- //
- // bar.setLed(1, 1);
- // }
- // else
- // {
- // for(x = 1; x <= 10; x++)
- // {
- // bar.setLed(x, 1);
- // }
- // }
- //}
- //****************************************************************************************
- // Extra Section (Custom User Defined Functions) (End)
- //****************************************************************************************
Advertisement
Add Comment
Please, Sign In to add comment