Guest User

Untitled

a guest
Jan 7th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 40.05 KB | None | 0 0
  1. //****************************************************************************************
  2. //Define Machine States (Begin)
  3. //Define and label the number of states in the FSM
  4. //****************************************************************************************
  5. #define mSTATE_1    1     //S1 - L_CHOICE (LOCK CHOICE)
  6. #define mSTATE_2    2     //S2 - G_PASS (GUEST PASSWORD)
  7. #define mSTATE_3    3     //S3 - R_PASS (RENTAL PASSWORD)
  8. #define mSTATE_4    4     //S4 - M_PASS (MASTER PASSWORD)
  9. #define mSTATE_5    5     //s5 - D_UNLOCK (DOOR UNLOCK)
  10. #define mSTATE_6    6     //s6 - ALARM (BUZZER RING)
  11. #define mSTATE_7    7     //s7 - M_CHECK (PROMPT USER TO ENTER MASTER PASSWORD)
  12. #define mSTATE_8    8     //s8 - O_PASS (PASSWORD OPTION)
  13. #define mSTATE_9    9     //s9 - N_PASS (ENTER NEW PASSWORD SCREEN)
  14. #define mSTATE_10   10    //s10 - RN_PASS (RE_ENTER NEW PASSWORD SCREEN)
  15. #define mSTATE_11   11    //s11 - S_PASS (PASSWORD CHANGE SUCCESS)
  16. #define mSTATE_12   12    //s12 - F_PRINT (FINGERPRINT LOCK)
  17. #define mSTATE_13   13    //s13 - F_CHANGE (FINGERPRINT CHANGE)
  18. #define mSTATE_14   14    //s14 - F_CONFIRM (FINGERPRINT CONFIRMATION)
  19. #define mSTATE_15   15    //s15 - UNLOCK? (UNLOCK THE DOOR OT NOT)
  20. #define mSTATE_16   16    //s16 - DOOR_O (DOOR OPEN)
  21. #define mSTATE_18   17    //s17 - L_ON (LIGHTS ON)
  22. #define mSTATE_19   18    //s18 - F_ON (CELING FAN ON)
  23. #define mSTATE_20   19    //s19 - AC_ON (AIR-CONDITIONAL ON)
  24. //****************************************************************************************
  25. //Define Machine States (End)
  26. //****************************************************************************************
  27.  
  28. //****************************************************************************************
  29. //Define GPIO Labels (Begin)
  30. //Define and label all GPIOs with meaningful names.
  31. //****************************************************************************************
  32. #define BUZZER             A2                    // GPIO2 (Output) for Grove Buzzer Control
  33. #define RLY_LOCK           6                    // GPIO6 (Output) for Relay Control (Door Lock)
  34. #define DOOR_SENSOR        12                    // GPIO4 (Input) for Door Position Sensing
  35.  
  36. #define M_SENSOR1_PIN      8                  // GPIO2 (Input) for Parallex PIR Motion
  37. #define M_SENSOR2_PIN      10                  // GPIO2 (Input) for Parallex PIR Motion
  38.                                              
  39. #define TMP_SENSOR_PIN     A0                 // A0 (Input) for Grove Temperature Sensor Module.
  40.  
  41. #define RLY_LIGHT          2                  // GPIO2 for Relay Control (LIGHTS)  
  42. #define RLY_CFAN          3                  // GPIO3 for Relay Control (CELING FAN)
  43. #define RLY_VFAN          4                  // GPIO4 for Relay Control (VANDILATION FAN)
  44. #define RLY_AC             5                 // GPIO5 for Relay Control (AIR-CONDITION)                                          
  45. //****************************************************************************************
  46. //Define GPIO Labels  (End)
  47. //****************************************************************************************
  48.  
  49. //****************************************************************************************
  50. //Define Signals, Messages and Functions (Begin)
  51. //Define and label control signals and messages.
  52. //****************************************************************************************
  53. #define TT1   25                // Temperature Threshold
  54. #define TT2   32                // Temperature Threshold
  55. #define LT    300               // Light Intensity Threshold
  56.  
  57. #define ON  1                                     // Turn on buzzer (Buzzer On)
  58. #define OFF 0                                     // Turn of buzzer (Buzzer Off)
  59. #define DETECTED 1
  60. #define NONE 0
  61.  
  62. //#define DISPLAY_DT    DisplayDistance(DISTANCE) // Function to Display Measured Distance
  63.  
  64. #define DOOR_L            1                       // Door Lock (Lock Position)
  65. #define DOOR_U            0                       // Door Magnetic Lock (Unlock Position)
  66.  
  67. #define DOOR_O            0                       // Door Sensor is in Open Position
  68. #define DOOR_C            1                       // Door Sensor is in Close Posotion
  69.  
  70. #define W_PASS            0                       // Wait for User to Input Password
  71. #define M_PASS            1                       // Master Password
  72. #define R_PASS            2                       // Rental Password
  73. #define G_PASS            3                       // Guest Password
  74. #define O_PASS            4                       // Password Option
  75.  
  76. #define W_KEY             0                       // Wait for User to Input Key
  77.  
  78. #define PASS_L6           6                       // Password Length to 6
  79. #define PASS_L16          16                      // Password Length to 6
  80. #define R_L               3                       // Password Retry Limit to 3
  81. #define PASS_RG           "123456"                // Default Password
  82. #define PASS_M            "1234567890ABCD11"      // Default Password
  83. #define R_CT              0                       // Password Retry Counter to 0
  84.  
  85. #define DOOR_L             digitalWrite(RLY_LOCK, HIGH)
  86. #define DOOR_U             digitalWrite(RLY_LOCK, LOW)
  87. #define BUZZER_ON          digitalWrite(BUZZER,   HIGH)
  88. #define BUZZER_OFF         digitalWrite(BUZZER,   LOW)
  89.  
  90. #define KEY_E      KEY_E = true
  91. #define KEY_D      KEY_E = false
  92.  
  93. #define LIGHT_ON           digitalWrite(RLY_LIGHT, LOW)
  94. #define LIGHT_OFF          digitalWrite(RLY_LIGHT, HIGH)
  95. #define CFAN_ON            digitalWrite(RLY_CFAN,  LOW)
  96. #define CFAN_OFF           digitalWrite(RLY_CFAN,  HIGH)
  97. #define VFAN_ON            digitalWrite(RLY_VFAN,  LOW)
  98. #define VFAN_OFF           digitalWrite(RLY_VFAN,  HIGH)
  99. #define AC_ON              digitalWrite(RLY_AC,    LOW)
  100. #define AC_OFF             digitalWrite(RLY_AC,    HIGH)
  101. //****************************************************************************************
  102. //Define Signals, Messages and Functions (End)
  103. //****************************************************************************************
  104.  
  105. //****************************************************************************************
  106. //Include (Begin)
  107. //Place all include files (preprocessor directives, datatypes, etc) here.
  108. //****************************************************************************************
  109. #include <Wire.h>
  110. //#include <Grove_LED_Bar.h>
  111. //#include <Ultrasonic.h>
  112. #include <math.h>
  113. #include <Digital_Light_TSL2561.h>
  114.  
  115. #include <Keypad.h>
  116. #include <EleF_LiquidCrystal.h>
  117. #include <stdio.h>
  118.  
  119. //****************************************************************************************
  120. //Include (End)
  121. //****************************************************************************************
  122.  
  123. //****************************************************************************************
  124. //External Peripherals Resources Initialization (Begin)
  125. //Place all declarations and codes related to external peripherals initializations here.
  126. //External Perihperals: Keypad, LCD Module, etc.
  127. //****************************************************************************************
  128. //Grove_LED_Bar bar(LED_CLK_PIN, LED_DATA_PIN, 0);    // Grove LED Bar Module Clock pin,
  129.                                                       // Data pin and Orientation setup.
  130.  
  131. //Ultrasonic ultrasonic(ULTRASONIC_PIN);              // Grove Untrasonic Module setup for
  132.                                                       // for Assigned Pin.    
  133.  
  134. unsigned int  KCOUNT = 0;                             // Record number of times a keypad is pressed
  135. char KUSER_PASSWORD[PASSWORD_LEN+1];                  // Record user password input
  136.  
  137. const byte KROWS = 4;                                 // Four rows
  138. const byte KCOLS = 4;                                 // Four columns
  139.  
  140. char keys[KROWS][KCOLS] = {                          // Define the Keymap
  141.   {'1','2','3', 'A'},
  142.   {'4','5','6', 'B'},
  143.   {'7','8','9', 'C'},
  144.   {'*','0','#', 'D'}
  145. };
  146.  
  147. byte KrowPins[KROWS] = { 39, 41, 43, 45 };            // Connect keypad ROW1, ROW2, ROW3 and ROW4 to these Arduino pins.
  148. byte KcolPins[KCOLS] = { 47, 49, 51, 53 };            // Connect keypad COL1, COL2, COL3 and COL4 to these Arduino pins.
  149. Keypad keypad = Keypad( makeKeymap(keys), KrowPins, KcolPins, KROWS, KCOLS ); // Create the Keypad
  150.  
  151. //LCD Module Peripheral
  152. EleF_LiquidCrystal lcd(0);                                                      
  153. //****************************************************************************************
  154. //External Peripherals Resources Initialization (End)
  155. //****************************************************************************************  
  156.  
  157. //****************************************************************************************
  158. //Global Variables (Begin)
  159. //Declare global variables for use by application here.
  160. //****************************************************************************************
  161. unsigned int MACHINE_STATE;               // Variable to hold current machine state
  162.                                                               // (Do NOT Remove).
  163.      bool  BUZZER;                      // Variable to control buzer ON / OFF.  
  164. //     long  DISTANCE;                    // Variable to hold Measured Distance in CM.
  165. //     int   B_CTR;                       // Variable to determine buzzer Beep Length.  
  166.  
  167.      unsigned long L;           // Ambient light intensity reading
  168.     float T;                  // Ambient temperature reading
  169.     bool  MOTION;                       // Motion sensing result
  170.  
  171.    const int    B  = 4275;                // B Value of the NTC Thermistor.
  172.    const int    R0 = 100000;              // R0 = 100k
  173.  
  174. int TIMER = 0;
  175. unsigned int KEY;                       // Variable to store number entered by user
  176. unsigned int R_CT;                      // User Password Retry Counter
  177. unsigned int P_STAT;                 // Variable to store Password Status
  178. unsigned int D_STAT;                   // Variable to store Door Position Sensor Status
  179. unsigned int K_STAT;                  // Variable to store Key Status
  180. boolean      KEY_E;                   // Flag Variable to store Keypad Enable / Disable Signal (true, false)
  181. //****************************************************************************************
  182. //Global Variables - End
  183. //****************************************************************************************
  184.  
  185. //****************************************************************************************
  186. // setup() function that run once during startup (Begin)
  187. //****************************************************************************************
  188. void setup()
  189. {
  190.   Wire.begin();                               // Initialize I2C
  191.  
  192.   Serial.begin(9600);                     // Initialize serial port for debug.
  193.   analogReadResolution(10);               // Set ADC resolution to 10-bit.
  194.   pinMode(M_SENSOR1_PIN, INPUT);      // Set GPIO pin (input) to read Motion Sensor 1
  195.   pinMode(M_SENSOR2_PIN, INPUT);      // Set GPIO pin (input) to read Motion Sensor 2
  196.   pinMode(RLY_LIGHT, OUTPUT);             // Set GPIO pin to control Light Relay
  197.   pinMode(RLY_CFAN, OUTPUT);              // Set GPIO pin to control Ceiling Fan Relay
  198.   pinMode(RLY_VFAN, OUTPUT);              // Set GPIO pin to control Exhaust Fan Relay
  199.   pinMode(RLY_AC, OUTPUT);              // Set GPIO pin to control Exhaust Fan Relay
  200.  
  201.   pinMode(BUZZER_PIN, OUTPUT);            // Set GPIO pin controlling BUZZER to OUTPUT.
  202. //TSL2561.init();                         // Initialize Grove Digital Light Sensor Module
  203. //bar.begin();                            // Initialize Grove LED Bar.
  204.  
  205.   pinMode(BUZZER, OUTPUT);                // Initialize GPIO2 pin as an output
  206.   pinMode(RLY_LOCK,   OUTPUT);            // Initialize GPIO7 pin as an output
  207.   pinMode(DOOR_SENSOR, INPUT);            // Initialize GPIO4 pin as an input
  208.  
  209.   lcd.begin(16, 2);                           // Initialize LCD of 2 Rows and 16 Columns
  210.   lcd.clear();                                // Clear LCD
  211.   LCDPrintMessageStart("Enter Option", 1, true); // Display "Enter Option" on first row, first column of LCD
  212.   LCDPrintMessageStart(NULL, 2, false);       // Position cursor at second row, first column of LCD  
  213.  
  214.  
  215.  
  216.                                           // Initial State Initialization - Begin
  217.                                           // Set Initial State Transition and Action.
  218.   R_CT = 1;                            // Set Password Retry Counter to 1 (1st attempt)
  219.   KEY_E;                          // Enable Keypad for User to Enter Password
  220.   BUZZER_OFF;                             // Turn Off Buzzer (Alarm Off)
  221.   DOOR_L;                              // Lock the door.  
  222.   D_STAT = DOOR_C;                                      
  223.   MACHINE_STATE = mSTATE_1;
  224.   TSL2561.init();
  225.                                           //Initial State Initialization - End
  226. }
  227. //****************************************************************************************
  228. // setup() function (End)
  229. //****************************************************************************************
  230.  
  231. //****************************************************************************************
  232. // loop() function that runs repeatedly (Begin)
  233. //****************************************************************************************
  234. void loop()  // put your main code here, to run repeatedly.
  235. {
  236.                                             // Sample Input - Begin
  237.                                             // Read (or process) and update information  
  238.                                             // from input device at a periodic interval.
  239.     T = MeasureTemperature();     // Measure Temperature and update TEMPERATURE
  240.                                                                 // with latest temperature.
  241.                                            
  242.     L = MeasureLightIntensity();    // Measure Light Intensity and update
  243.                                             // INTENSITY with latest light intensity.  
  244.     M_SENSOR1 = digitalRead(M_SENSOR1_PIN);// Check PIR Motion Sensor status.                                                                                
  245.     M_SENSOR2 = digitalRead(M_SENSOR2_PIN);// Check PIR Motion Sensor status.
  246.    
  247. //  DISTANCE = ultrasonic.MeasureInCentimeters(); // Measure Distance and update DISTANCE
  248.                                                   // with latest value.
  249.  
  250.     digitalWrite(BUZZER_PIN , BUZZER);      // Update Buzzer Pin Status (Buzzer On / Off)
  251.  
  252.     P_STAT = GetUserPassword (PASSWORD, &KEYPAD_ENABLE);
  253.     D_STAT   = digitalRead(DOOR_SENSOR);
  254.                                            
  255.                                             // Sample Input - End
  256.  
  257.                                             // Finite State Machine - Begin  
  258.  
  259.  switch(MACHINE_STATE)                     //
  260.   {
  261.     case mSTATE_1:                          // State 1
  262.      Serial.println("STATE_1:LOCK CHOICE");        // Debug message.
  263.     if(K_STAT == W_KEY && KEY == '#') // Transition: (Condition)
  264.      {
  265.         lcd.clear();
  266.         LCDPrintMessageStart("Enter Option", 1, true); // Display "Enter Option" on first row, first column of LCD
  267.         LCDPrintMessageStart(NULL, 2, false);          // Position cursor at second row, first column of LCD                        
  268.         MACHINE_STATE = mSTATE_1;                  
  269.      }
  270.     if(K_STAT == W_KEY && (KEY != '#' || KEY != 1 || KEY != 2 || KEY != 3)) // Transition: (Condition)
  271.      {
  272.         lcd.clear();
  273.         LCDPrintMessageStart("Wrong Option", 1, true); // Display "Wrong Option" on first row, first column of LCD
  274.         LCDPrintMessageStart(NULL, 2, false);          // Position cursor at second row, first column of LCD                        
  275.         MACHINE_STATE = mSTATE_1;                  
  276.      }
  277.  
  278.      
  279.      else if(KEY == 1) // Transition: (Condition)
  280.      {              
  281.         MACHINE_STATE = mSTATE_2;                   // Transition: Jump to G_PASS (State 2)    
  282.      }                      
  283.      else if(KEY == 2) // Transition: Check Condition
  284.      {
  285.  
  286.        MACHINE_STATE = mSTATE_3;                    // Transition: Jump to R_PASS (State 3)                        
  287.      }
  288.      else if(KEY == 3)  // Transition: (Condition)
  289.      {
  290.        
  291.         MACHINE_STATE = mSTATE_4;                   // Transition: Jump to M_PASS (State 4)
  292.      }
  293.      
  294.      else
  295.      {}
  296.      break;
  297.  
  298.     case mSTATE_2:                          // G_PASS State
  299.      Serial.println("STATE_2:G_PASS");        // Debug message.
  300.     if(P_STAT == W_PASS && R_CT < 3) // Transition: (Condition)
  301.      {
  302.         lcd.clear();
  303.         LCDPrintMessageStart("Guest Password?", 1, true);
  304.         LCDPrintMessageStart(NULL, 2, false);          // Position cursor at second row, first column of LCD                                                  
  305.         MACHINE_STATE = mSTATE_1;                  
  306.      }
  307.      else if(KEY == "#" && R_CT < 3) // Transition: (Condition)
  308.      {
  309.         lcd.clear();
  310.         LCDPrintMessageStart("Guest Password?", 1, true);
  311.         LCDPrintMessageStart(NULL, 2, false);          // Position cursor at second row, first column of LCD                                                                                                                        
  312.         MACHINE_STATE = mSTATE_1;                  
  313.      }
  314.      else if(R_CT < 3 && P_STAT != G_PASS) // Transition: (Condition)
  315.      {
  316.         lcd.clear();
  317.         LCDPrintMessageStart("Try Again :(", 1, true);
  318.         LCDPrintMessageStart(NULL, 2, false);          // Position cursor at second row, first column of LCD                                                                                  
  319.         MACHINE_STATE = mSTATE_1;                  
  320.         R_CT++;                                        
  321.      }                      
  322.      else if(P_STAT == G_PASS) // Transition: Check Condition
  323.      {
  324.        lcd.clear();
  325.        LCDPrintMessageStart("Correct! :D", 1, true);                                                                              
  326.        MACHINE_STATE = mSTATE_5;                    // Transition: Jump to D_UNLOCK (State 5)
  327.        KEY_D;
  328.        DOOR_U;
  329.        R_CT = 0;                          
  330.      }
  331.      else if(R_CT == 3)  // Transition: (Condition)
  332.      {
  333.         lcd.clear();
  334.         LCDPrintMessageStart("Wrong!", 1, true);                                                                                  
  335.         MACHINE_STATE = mSTATE_6;                   // Transition: Jump to ALARM RING (State 6)
  336.         BUZZER_ON;
  337.         // *add sms here*
  338.      }
  339.      
  340.      else
  341.      {}
  342.  
  343.     case mSTATE_3:                          // G_PASS State
  344.      Serial.println("STATE_3:R_PASS");        // Debug message.
  345.     if(P_STAT == W_PASS && R_CT < 3) // Transition: (Condition)
  346.      {
  347.         lcd.clear();
  348.         LCDPrintMessageStart("Rental Password?", 1, true);
  349.         LCDPrintMessageStart(NULL, 2, false);          // Position cursor at second row, first column of LCD                                                      
  350.         MACHINE_STATE = mSTATE_1;                  
  351.      }
  352.      else if(R_CT < 3 && P_STAT != R_PASS) // Transition: (Condition)
  353.      {
  354.         lcd.clear();
  355.         LCDPrintMessageStart("Try Again :(", 1, true);
  356.         LCDPrintMessageStart(NULL, 2, false);          // Position cursor at second row, first column of LCD                                
  357.         MACHINE_STATE = mSTATE_1;                  
  358.         R_CT++;                                        
  359.      }                      
  360.      else if(P_STAT == R_PASS) // Transition: Check Condition
  361.      {
  362.        lcd.clear();
  363.        LCDPrintMessageStart("Correct! :D", 1, true);                                                                              
  364.        MACHINE_STATE = mSTATE_5;                    // Transition: Jump to D_UNLOCK (State 5)
  365.        KEY_D;
  366.        DOOR_U;
  367.        R_CT = 0;                          
  368.      }
  369.      else if(R_CT == 3)  // Transition: (Condition)
  370.      {
  371.         lcd.clear();
  372.         LCDPrintMessageStart("Wrong!", 1, true);                                                                                  
  373.         MACHINE_STATE = mSTATE_6;                   // Transition: Jump to ALARM RING (State 6)
  374.         BUZZER_ON;
  375.         // *add sms here*
  376.      }
  377.      
  378.      else
  379.      {}
  380.      break;
  381.  
  382.  
  383.     case mSTATE_4:                          // G_PASS State
  384.      Serial.println("STATE_4:M_PASS");        // Debug message.
  385.     if(P_STAT == W_PASS && R_CT < 3) // Transition: (Condition)
  386.      {
  387.         lcd.clear();
  388.         LCDPrintMessageStart("Master password?", 1, true);
  389.         LCDPrintMessageStart(NULL, 2, false);          // Position cursor at second row, first column of LCD                                                      
  390.         MACHINE_STATE = mSTATE_1;                  
  391.      }
  392.      else if(R_CT < 3 && P_STAT != M_PASS) // Transition: (Condition)
  393.      {
  394.         lcd.clear();
  395.         LCDPrintMessageStart("Try Again :(", 1, true);
  396.         LCDPrintMessageStart(NULL, 2, false);          // Position cursor at second row, first column of LCD                                
  397.         MACHINE_STATE = mSTATE_1;                  
  398.         R_CT++;                                        
  399.      }                      
  400.      else if(P_STAT == R_PASS) // Transition: Check Condition
  401.      {
  402.        lcd.clear();
  403.        LCDPrintMessageStart("Correct! :D", 1, true);                                                                              
  404.        MACHINE_STATE = mSTATE_5;                    // Transition: Jump to D_UNLOCK (State 5)
  405.        KEY_D;
  406.        DOOR_U;
  407.        R_CT = 0;                          
  408.      }
  409.      else if(R_CT == 3)  // Transition: (Condition)
  410.      {
  411.         lcd.clear();
  412.         LCDPrintMessageStart("Wrong!", 1, true);                                                                                  
  413.         MACHINE_STATE = mSTATE_6;                   // Transition: Jump to ALARM RING (State 6)
  414.         BUZZER_ON;
  415.         // *add sms here*
  416.      }
  417.      else if(KEY=='#' && TIME == 30) // Transition: Check Condition
  418.      {                                                                              
  419.        MACHINE_STATE = mSTATE_7;                    // Transition: Jump to M_CHECK (State 7)                      
  420.      }
  421.      else
  422.      {}
  423.      break;
  424.     case mSTATE_5:                              // State 3
  425.      Serial.println("STATE_3: D_UNLOCK");  // Debug message.
  426.      if(D_STAT == DOOR_C)  // Transition: (Condition)
  427.      {
  428.         lcd.clear();
  429.         LCDPrintMessageStart("Door Unlocked", 1, true);
  430.         MACHINE_STATE = mSTATE_5;                       // Transition: Jump to DOOR_O (State 16)
  431.         KEY_D;
  432.         DOOR_U;
  433.         R_CT = 0;
  434.      }
  435.      else if(D_STAT == DOOR_O)  // Transition: (Condition)
  436.      {
  437.         lcd.clear();
  438.         LCDPrintMessageStart("Door Opened", 1, true);  
  439.         V_FAN = ON;
  440.         MACHINE_STATE = mSTATE_16;                       // Transition: Jump to DOOR_O (State 16)
  441.      }
  442.      else
  443.      {}
  444.      break;
  445.    case mSTATE_6:                          // State 4
  446.      Serial.println("STATE_4:Alarm");      // Debug message.                          
  447.      if((P_STAT == W_PASS || P_STAT == X_PASS) && TIME < 30)  // Transition: (Condition)
  448.      {
  449.         lcd.clear();
  450.         LCDPrintMessageStart("Enter Password", 1, true);
  451.         LCDPrintMessageStart(NULL, 2, false);  
  452.         TIME++;
  453.         MACHINE_STATE = mSTATE_6;                    // Transition: Jump to Next State.
  454.         //check with teacher about the countdown timmer
  455.      }
  456.      else if(P_STAT == G_PASS || P_STAT == R_PASS || P_STAT == M_PASS)  // Transition: (Condition)
  457.      {
  458.         lcd.clear();
  459.         LCDPrintMessageStart("Correct! :D", 1, true);
  460.         MACHINE_STATE = mSTATE_5;                  // Transition: Jump to Next State.
  461.         BUZZER_OFF;
  462.         KEY_D;
  463.         R_CT = 0;
  464.         DOOR_U;
  465.         TIME = 0;
  466.      }
  467.      else if(TIME == 30)  // Transition: (Condition)
  468.      {
  469.         MACHINE_STATE = mSTATE_1;                  // Transition: Jump to Next State.
  470.         BUZZER_OFF;
  471.         R_CT = 0;
  472.         TIME = 0;
  473.      }    
  474.      else
  475.      {}
  476.      break;
  477.  
  478.  case mSTATE_7:                          // State 4
  479.      Serial.println("STATE_7: M_CHECK");      // Debug message.                          
  480.      if(P_STAT == W_PASS || P_STAT != M_PASS || KEY == '#')  // Transition: (Condition)
  481.      {
  482.         lcd.clear();
  483.         LCDPrintMessageStart("Enter Password", 1, true);
  484.         LCDPrintMessageStart(NULL, 2, false);  
  485.         TIME = 0;
  486.         MACHINE_STATE = mSTATE_7;                    // Transition: Jump to Next State.
  487.         //check with teacher about the countdown timmer
  488.      }
  489.      else if(P_STAT == F_FLOCK || P_STAT == M_PASS)  // Transition: (Condition)
  490.      {
  491.         MACHINE_STATE = mSTATE_8;                  // Transition: Jump to Next State.
  492.      }    
  493.      else
  494.      {}
  495.      break;
  496.      
  497.  case mSTATE_8:                          // State 4
  498.      Serial.println("STATE_8: O_PASS");      // Debug message.                          
  499.      if(K_STAT == W_KEY)  // Transition: (Condition)
  500.      {
  501.         lcd.clear();
  502.         LCDPrintMessageStart("Enter Option", 1, true);
  503.         LCDPrintMessageStart(NULL, 2, false);  
  504.         MACHINE_STATE = mSTATE_8;                    // Transition: Jump to Next State.
  505.      }
  506.      else if(KEY != 1 || KEY != 2 || KEY != 3 || KEY != 4)  // Transition: (Condition)
  507.      {
  508.        lcd.clear();
  509.        LCDPrintMessageStart("Enter Right Option", 1, true);
  510.        LCDPrintMessageStart(NULL, 2, false);  
  511.        MACHINE_STATE = mSTATE_8;                    // Transition: Jump to Next State.
  512.      }    
  513.      else if(K_STAT == 1)  // Transition: (Condition)
  514.      {
  515.         O_PASS = 1;
  516.         MACHINE_STATE = mSTATE_9;                  // Transition: Jump to Next State.
  517.      }
  518.      else if(K_STAT == 2)  // Transition: (Condition)
  519.      {
  520.         O_PASS = 2;
  521.         MACHINE_STATE = mSTATE_9;                  // Transition: Jump to Next State.
  522.      }  
  523.      else if(K_STAT == 3)  // Transition: (Condition)
  524.      {
  525.         O_PASS = 3;
  526.         MACHINE_STATE = mSTATE_9;                  // Transition: Jump to Next State.
  527.      }  
  528.      else if(K_STAT == 4)  // Transition: (Condition)
  529.      {
  530.         MACHINE_STATE = mSTATE_12;                  // Transition: Jump to Next State.
  531.      }    
  532.      else
  533.      {}
  534.      break;
  535.  
  536.  case mSTATE_9:                          // State 4
  537.      Serial.println("STATE_9: N_PASS");      // Debug message.                          
  538.      if(K_STAT == W_KEY || KEY == '#')  // Transition: (Condition)
  539.      {
  540.         lcd.clear();
  541.         LCDPrintMessageStart("Enter New Password", 1, true);
  542.         LCDPrintMessageStart(NULL, 2, false);  
  543.         MACHINE_STATE = mSTATE_9;                    // Transition: Jump to Next State.
  544.      }
  545.      else if(KEY == '#' && TIME == 30)  // Transition: (Condition)
  546.      {
  547.         lcd.clear();
  548.         LCDPrintMessageStart("Enter Password", 1, true);
  549.         LCDPrintMessageStart(NULL, 2, false);  
  550.         MACHINE_STATE = mSTATE_9;                    // Transition: Jump to Next State.
  551.         //ASK CHER HOW TO RECORD NEW PASS
  552.      }
  553.      else if(KEY == '*')  // Transition: (Condition)
  554.      {
  555.       //ASK CHER HOW RECORD THE NEW PASSWORD
  556.         MACHINE_STATE = mSTATE_10;                  // Transition: Jump to Next State.
  557.      }    
  558.      else
  559.      {}
  560.      break;
  561.  
  562.       case mSTATE_10:                          // State 4
  563.      Serial.println("STATE_10: RN_PASS");      // Debug message.                          
  564.      if(P_STAT == W_PASS || KEY == '#')  // Transition: (Condition)
  565.      {
  566.         lcd.clear();
  567.         LCDPrintMessageStart("Re-Enter Password", 1, true);
  568.         LCDPrintMessageStart(NULL, 2, false);  
  569.         MACHINE_STATE = mSTATE_10;                    // Transition: Jump to Next State.
  570.      }
  571.      else if(KEY == '*' && KEY == R_KEY && O_PASS == 1)  // Transition: (Condition)
  572.      {
  573.       //ASK CHER HOW RECORD THE NEW PASSWORD
  574.         G_PASS == R_KEY;
  575.         MACHINE_STATE = mSTATE_11;                  // Transition: Jump to Next State.
  576.      }    
  577.      else if(KEY == '*' && KEY == R_KEY && O_PASS == 2)  // Transition: (Condition)
  578.      {
  579.       //ASK CHER HOW RECORD THE NEW PASSWORD
  580.         R_PASS == R_KEY;
  581.         MACHINE_STATE = mSTATE_11;                  // Transition: Jump to Next State.
  582.      }  
  583.      else if(KEY == '*' && KEY == R_KEY && O_PASS == 3)  // Transition: (Condition)
  584.      {
  585.       //ASK CHER HOW RECORD THE NEW PASSWORD
  586.         M_PASS == R_KEY;
  587.         MACHINE_STATE = mSTATE_11;                  // Transition: Jump to Next State.
  588.      }  
  589.      else
  590.      {}
  591.      break;
  592.      
  593.      case mSTATE_11:                          // State 4
  594.      Serial.println("STATE_11: S_PASS");      // Debug message.                          
  595.      if(K_STAT == W_KEY)  // Transition: (Condition)
  596.      {
  597.         lcd.clear();
  598.         LCDPrintMessageStart("Change Successful!", 1, true);
  599.         MACHINE_STATE = mSTATE_10;                    // Transition: Jump to Next State.
  600.      }
  601.      else if(KEY == '*')  // Transition: (Condition)
  602.      {
  603.       //ASK CHER HOW RECORD THE NEW PASSWORD
  604.        KEY_D;
  605.        DOOR_U;
  606.        R_CT = 0;  
  607.        MACHINE_STATE = mSTATE_5;                  // Transition: Jump to Next State.
  608.      }    
  609.      else if(KEY == '#')  // Transition: (Condition)
  610.      {
  611.       //ASK CHER HOW RECORD THE NEW PASSWORD
  612.       R_CT = 0;
  613.       TIME = 0;
  614.       MACHINE_STATE = mSTATE_1;                  // Transition: Jump to Next State.
  615.      }
  616.      else
  617.      {}
  618.      break;
  619.  
  620.   case mSTATE_12:                          // State 4
  621.      Serial.println("STATE_12: F_PRINT");      // Debug message.                          
  622.      if(K_STAT == W_PRINT)  // Transition: (Condition)
  623.      {
  624.         lcd.clear();
  625.         LCDPrintMessageStart("Place Your Thump", 1, true);
  626.         MACHINE_STATE = mSTATE_12;                    // Transition: Jump to Next State.
  627.      }
  628.      
  629.      else if(R_CT < 20 && F_STAT != F_PRINT)  // Transition: (Condition)
  630.      {
  631.       lcd.clear();
  632.       LCDPrintMessageStart("Wrong", 1, true);
  633.        R_CT++;  
  634.        MACHINE_STATE = mSTATE_12;                  // Transition: Jump to Next State.
  635.      }    
  636.      else if(F_STAT == F_PRINT && R_CT < 20)  // Transition: (Condition)
  637.      {
  638.       lcd.clear();
  639.       LCDPrintMessageStart("Finger Reconised!", 1, true);
  640.       R_CT = 0;
  641.       KEY_D;
  642.       DOOR_U;
  643.       MACHINE_STATE = mSTATE_5;                  // Transition: Jump to Next State.
  644.      }
  645.      else if(F_STAT == F_PRINT && O_PASS == 4)  // Transition: (Condition)
  646.      {
  647.       lcd.clear();
  648.       LCDPrintMessageStart("Finger Reconised!", 1, true);
  649.       R_CT = 0;
  650.       MACHINE_STATE = mSTATE_13;                  // Transition: Jump to Next State.
  651.      }
  652.      else if(F_STAT != F_PRINT && R_CT == 20)  // Transition: (Condition)
  653.      {
  654.       lcd.clear();
  655.       LCDPrintMessageStart("ERROR!", 1, true);
  656.       R_CT = 0;
  657.       BUZZER_ON;
  658.       // SMS
  659.       MACHINE_STATE = mSTATE_6;                  // Transition: Jump to Next State.
  660.      }
  661.      else
  662.      {}
  663.      break;
  664.  
  665.      case mSTATE_13:                          // State 4
  666.      Serial.println("STATE_13: F_CHANGE");      // Debug message.                          
  667.      if(F_STAT == W_PRINT)  // Transition: (Condition)
  668.      {
  669.         lcd.clear();
  670.         LCDPrintMessageStart("Place thump", 1, true);
  671.         MACHINE_STATE = mSTATE_13;                    // Transition: Jump to Next State.
  672.      }
  673.      else if(F_STAT == NF_PRINT)  // Transition: (Condition)
  674.      {
  675.       //ASK CHER
  676.        F_PRINT = NF_PRINT;  
  677.        MACHINE_STATE = mSTATE_14;                  // Transition: Jump to Next State.
  678.      }
  679.      else if(KEY == '#')  // Transition: (Condition)
  680.      {
  681.        MACHINE_STATE = mSTATE_12;                  // Transition: Jump to Next State.
  682.      }
  683.      else
  684.      {}
  685.      break;
  686.  
  687.   case mSTATE_14:                          // State 4
  688.      Serial.println("STATE_14: F_CONFIRM");      // Debug message.                          
  689.      if(F_STAT == W_PRINT)  // Transition: (Condition)
  690.      {
  691.         lcd.clear();
  692.         LCDPrintMessageStart("Place again", 1, true);
  693.         MACHINE_STATE = mSTATE_14;                    // Transition: Jump to Next State.
  694.      }
  695.      else if(F_STAT == NF_PRINT)  // Transition: (Condition)
  696.      {
  697.       //ASK CHER
  698.        F_PRINT = NF_PRINT;  
  699.        MACHINE_STATE = mSTATE_15;                  // Transition: Jump to Next State.
  700.      }
  701.      else if(KEY == '#')  // Transition: (Condition)
  702.      {
  703.        MACHINE_STATE = mSTATE_12;                  // Transition: Jump to Next State.
  704.      }
  705.      else
  706.      {}
  707.      break;
  708.  
  709. case mSTATE_15:                          // State 4
  710.      Serial.println("STATE_15: UNLOCK?");      // Debug message.                          
  711.      if(K_STAT == W_KEY)  // Transition: (Condition)
  712.      {
  713.         lcd.clear();
  714.         LCDPrintMessageStart("UNLOCK?", 1, true);
  715.         MACHINE_STATE = mSTATE_15;                    // Transition: Jump to Next State.
  716.      }
  717.      else if(KEY == '*')  // Transition: (Condition)
  718.      {
  719.       MACHINE_STATE = mSTATE_5;                  // Transition: Jump to Next State.
  720.      }
  721.      else if(KEY == '#')  // Transition: (Condition)
  722.      {
  723.        MACHINE_STATE = mSTATE_1;                  // Transition: Jump to Next State.
  724.      }
  725.      else
  726.      {}
  727.      break;
  728.      
  729. case mSTATE_16:                          // State 4
  730.      Serial.println("STATE_16: DOOR_C");      // Debug message.                          
  731.         if(D_STAT == DOOR_C && M_SENSOR2 == NONE)  // Transition: (Condition)
  732.      {
  733.         lcd.clear();
  734.         LCDPrintMessageStart("Door Closed", 1, true);
  735.         DOOR_L;
  736.         R_CT = 0;
  737.         KEY_E;
  738.         MACHINE_STATE = mSTATE_1;                    // Transition: Jump to Next State.
  739.      }
  740.         else if (D_STAT == DOOR_C && M_SENSOR2 == DETECTED)
  741.      {
  742.           DOOR_L;
  743.           R_CT = 0;
  744.           KEY_E;
  745.           MACHINE_STATE = mSTATE_17;                    // Transition: Jump to Next State.
  746.      }
  747.  
  748.      else
  749.      {}
  750.      break;
  751.  
  752.   case mSTATE_17:                          // State 4
  753.          Serial.println("STATE_16: TEMP AND LIGHT");      // Debug message.                          
  754.           if(L < LT)  // Transition: (Condition)
  755.         {
  756.           LIGHT_ON;
  757.           MACHINE_STATE = mSTATE_17;                    // Transition: Jump to Next State.
  758.         }
  759.          else if (L >= LT)
  760.         {
  761.          LIGHT_OFF;
  762.          MACHINE_STATE = mSTATE_17;                    // Transition: Jump to Next State.
  763.         }
  764.         else if (T >= TT1 && T<=TT2)
  765.         {
  766.          CFAN_ON;
  767.          AC_OFF;
  768.          MACHINE_STATE = mSTATE_17;                    // Transition: Jump to Next State.
  769.         }
  770.         else if (T > TT2)
  771.         {
  772.          CFAN_ON;
  773.          AC_ON;
  774.          MACHINE_STATE = mSTATE_17;                    // Transition: Jump to Next State.
  775.         }
  776.  
  777.         else if (T < TT1)
  778.         {
  779.          CFAN_OFF;
  780.          AC_OFF;
  781.          MACHINE_STATE = mSTATE_17;                    // Transition: Jump to Next State.
  782.         }
  783.        
  784.       else  if(M_SENSOR2 == NONE)  // Transition: (Condition)
  785.      {
  786.         MACHINE_STATE = mSTATE_16;                    // Transition: Jump to Next State.
  787.      }
  788.  
  789.  
  790.      else
  791.      {}
  792.      break;
  793.  
  794.  
  795.  
  796.  
  797.  
  798.  
  799.  
  800.  
  801.  
  802.                                             // Finite State Machine - End
  803.   delay(100);                               // Set Loop Refresh
  804.                                                                 // (Refresh rate of the Finite State Machine).
  805.  
  806.  
  807.  
  808.  
  809.  
  810.  
  811.  
  812.  
  813.  
  814.  
  815.  
  816.      
  817.     default:                                //
  818.       MACHINE_STATE = mSTATE_1;             // Set the Initial State.
  819.   }                                         //
  820. }
  821.  
  822. //****************************************************************************************
  823. // loop() function (End)
  824. //****************************************************************************************
  825.  
  826. //****************************************************************************************
  827. // Extra Section (Custom User Defined Functions) (Begin)
  828. // This last section is user defined for the purpose of adding custom supporting
  829. // functions.
  830. //****************************************************************************************
  831. //Functions to display message to LCD
  832. void LCDPrintMessageStart(const char* Message, unsigned int row, boolean clear)
  833. {
  834.   if(clear)
  835.   {
  836.     lcd.clear();                                // Clear LCD
  837.   }
  838.  
  839.   switch(row)
  840.   {
  841.     case 1:
  842.       lcd.setCursor(0, 0);                      // Set LCD cursor position to first row, first column
  843.       break;
  844.     case 2:
  845.       lcd.setCursor(0, 1);                      // Set LCD cursor position to second row, first column
  846.       break;
  847.     default:
  848.       lcd.setCursor(0, 0);                      // Set LCD cursor position to first row, first column
  849.  
  850.   }
  851.  
  852.   if(Message != NULL)
  853.   {
  854.     lcd.print(Message);                         // Print MESSAGE
  855.   }
  856.  
  857.   lcd.cursor();                                 // Turns on cursor
  858.   lcd.blink();                                  // Makes cursor blink
  859. }
  860.  
  861. void LCDPrintMessageFromLastPosition(const char* Message)
  862. {
  863.   lcd.print(Message);                           // Print MESSAGE to current LCD position
  864. }
  865.  
  866. //Function to get user password.
  867. unsigned int GetUserPassword (const char* Password, bool *KEYPAD_ENABLE)
  868. {
  869.     static boolean KEYPAD_DISABLED;
  870.     char key = keypad.getKey();
  871.    
  872.     if(KEYPAD_DISABLED) && (*KEYPAD_ENABLE))
  873.     {
  874.       KEYPAD_DISABLED = false;
  875.       LCDPrintMessageStart("Password?", 1, true);  
  876.       LCDPrintMessageStart(NULL, 2, false);
  877.     }
  878.  
  879.     if(key) && (*KEYPAD_ENABLE))
  880.     {      
  881.       KUSER_PASSWORD[KCOUNT] = key;
  882.       KCOUNT= KCOUNT + 1;
  883.       if(KCOUNT == PASSWORD_LEN)
  884.       {
  885.         KUSER_PASSWORD[KCOUNT] = '\0';
  886.         KCOUNT = 0;
  887.  
  888.         if(strcmp(Password, KUSER_PASSWORD) == 0)
  889.         {
  890.           LCDPrintMessageFromLastPosition("*");
  891.           LCDPrintMessageStart("Password OK!", 1, true);
  892.           return CORRECT_PWD;
  893.         }
  894.         else
  895.         {
  896.           LCDPrintMessageFromLastPosition("*");
  897.           LCDPrintMessageStart("Password ERROR!", 1, true);
  898.           delay (500);
  899.           LCDPrintMessageStart("Password?", 1, true);
  900.           LCDPrintMessageStart(NULL, 2, false);            
  901.           return X_PASS;    
  902.         }
  903.       }
  904.       else
  905.       {
  906.         LCDPrintMessageFromLastPosition("*");
  907.         return W_PASS;
  908.       }
  909.     }
  910.     else
  911.     {
  912.       if(*KEYPAD_ENABLE == false)
  913.       {
  914.           KEYPAD_DISABLED = true;  
  915.       }
  916.       return W_PASS;
  917.     }
  918. }
  919. unsigned long MeasureLightIntensity()
  920. {
  921.   return(TSL2561.readVisibleLux());
  922. }
  923.  
  924. float MeasureTemperature()
  925. {
  926.     int a = analogRead(TMP_SENSOR_PIN);
  927.  
  928.     float R = 1023.0/((float)a)-1.0;
  929.     R = 100000.0*R;
  930.  
  931.     float temperature=1.0/(log(R/100000.0)/B+1/298.15)-273.15;  // Convert to temperature
  932.                                   // via datasheet info.
  933.     return temperature; //ask cher about the variable name
  934. }
  935.  
  936. //void DisplayTemperature(float temperature)
  937. //{
  938. //   unsigned int value = (unsigned int) temperature / 5;
  939. //   unsigned int x;
  940. //
  941. //   for(x = 1; x <= 10; x++)
  942. //   {
  943. //      bar.setLed(x, 0);
  944. //   }
  945. //
  946. //   for(x = 1; x <= value; x++)
  947. //   {
  948. //      bar.setLed(x, 1);
  949. //   }
  950. //}
  951.  
  952. //void DisplayDistance(int distance)
  953. //{
  954. //   unsigned int value = distance / 20;
  955. //   unsigned int x;
  956. //
  957. //   if((value <= 10) && (value > 0))
  958. //   {
  959. //      for(x = 1; x <= 10; x++)
  960. //      {
  961. //        bar.setLed(x, 0);
  962. //      }
  963. //      
  964. //      for(x = 1; x <= value; x++)
  965. //      {
  966. //        bar.setLed(x, 1);
  967. //      }
  968. //   }
  969. //   else if(value == 0)
  970. //   {
  971. //      for(x = 1; x <= 10; x++)
  972. //      {
  973. //        bar.setLed(x, 0);
  974. //      }  
  975. //        
  976. //      bar.setLed(1, 1);
  977. //   }
  978. //   else
  979. //   {
  980. //      for(x = 1; x <= 10; x++)
  981. //      {
  982. //        bar.setLed(x, 1);
  983. //      }    
  984. //   }
  985. //}
  986. //****************************************************************************************
  987. // Extra Section (Custom User Defined Functions) (End)
  988. //****************************************************************************************
Advertisement
Add Comment
Please, Sign In to add comment