Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* USER CODE BEGIN 4 */
- void State_Machine(TextLCDType lcd)
- {
- int counter = 0, timer = 1;
- static int triggered_cnt = 0;
- switch(state)
- {
- case 0: //DEACTIVATED STATE
- HAL_GPIO_WritePin(GPIOA, LED_GREEN_Pin, GPIO_PIN_SET);
- TextLCD_Printf(&lcd, "DISARMED");
- TextLCD_Cmd(&lcd, 0xc0);
- TextLCD_Printf(&lcd, "# to activate");
- TextLCD_Home(&lcd);
- if(Keypad_ScanKeyChar() == '#') Lock_state(lcd);
- break;
- case 1: // ARMING STATE
- while(counter <=1)
- {
- HAL_GPIO_WritePin(GPIOA, LED_YELLOW_Pin, GPIO_PIN_RESET);
- TextLCD_Printf(&lcd, "ARMING");
- TextLCD_Cmd(&lcd, 0xc0);
- Active_Buzzer_on();
- TextLCD_Printf(&lcd, "%d, # deactivate", timer--);
- HAL_Delay(1000);
- if(Keypad_ScanKeyChar() == '#') Lock_state(lcd);
- if(state == 0)
- break;
- HAL_GPIO_WritePin(GPIOA, LED_YELLOW_Pin, GPIO_PIN_SET);
- TextLCD_Clear(&lcd);
- TextLCD_Home(&lcd);
- counter++;
- }
- Active_Buzzer_off();
- counter =0;
- if(state == 0)
- {
- HAL_GPIO_WritePin(GPIOA, LED_YELLOW_Pin, GPIO_PIN_RESET);
- state =0;
- }
- else
- {
- HAL_GPIO_WritePin(GPIOA, LED_YELLOW_Pin, GPIO_PIN_RESET);
- state = 2;
- }
- break;
- case 2: //ARMED STATE
- HAL_GPIO_WritePin(GPIOA, LED_RED_Pin, GPIO_PIN_SET);
- TextLCD_Printf(&lcd, "ARMED");
- TextLCD_Cmd(&lcd, 0xc0);
- TextLCD_Printf(&lcd, "# to deactivate");
- TextLCD_Home(&lcd);
- if(Keypad_ScanKeyChar() == '#') Lock_state(lcd);
- Read_PIR();
- break;
- case 3: //ALARM TRIGGERED
- TextLCD_Printf(&lcd, "ALARM TRIGGERED!");
- TextLCD_Cmd(&lcd, 0xc0);
- TextLCD_Printf(&lcd, "# to deactivate");
- TextLCD_Home(&lcd);
- while(triggered_cnt <=2 )
- {
- if(Keypad_ScanKeyChar() == '#') Lock_state_master(lcd);
- HAL_Delay(1000);
- triggered_cnt++;//only works first time around... fix this mby function
- }
- Passive_Buzzer_on();
- if(Keypad_ScanKeyChar() == '#') Lock_state_master(lcd);
- Passive_Buzzer_off();
- break;
- }
- }
- //Buzzer on
- void Active_Buzzer_on()
- {
- HAL_GPIO_WritePin(GPIOA, ACTIVE_BUZZER_Pin, GPIO_PIN_SET);
- }
- //Buzzer off
- void Active_Buzzer_off()
- {
- HAL_GPIO_WritePin(GPIOA, ACTIVE_BUZZER_Pin, GPIO_PIN_RESET);
- }
- //Buzzer on
- void Passive_Buzzer_on()
- {
- __HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, 5000);
- HAL_Delay(100);
- __HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, 100000);
- }
- //Buzzer off
- void Passive_Buzzer_off()
- {
- __HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, 0);
- HAL_TIM_Base_Stop(&htim10);
- }
- //Read PIR value
- void Read_PIR()
- {
- int PIR_Value;
- PIR_Value = HAL_GPIO_ReadPin(GPIOA, PIR_INPUT_Pin);
- if(PIR_Value == 1)
- state = 3;
- }
- //This is when the alarm has been triggered. only master code will disarm this.
- void Lock_state_master(TextLCDType lcd)
- {
- int lock_state=0, complete=0, PinCode;
- TextLCD_Clear(&lcd);
- TextLCD_Printf(&lcd, "PIN:");
- while(complete==0)
- {
- Passive_Buzzer_on();
- PinCode = Keypad_ScanKeyNum();
- if(PinCode < 10)
- {
- switch(lock_state)
- {
- case 0:
- Input_combination[0] = PinCode;
- TextLCD_Printf(&lcd, "%d", Input_combination[0]);
- lock_state = lock_state + 1;
- HAL_Delay(500);
- break;
- case 1:
- Input_combination[1] = PinCode;
- TextLCD_Printf(&lcd, "%d", Input_combination[1]);
- lock_state = lock_state + 1;
- HAL_Delay(500);
- break;
- case 2:
- Input_combination[2] = PinCode;
- TextLCD_Printf(&lcd, "%d", Input_combination[2]);
- lock_state = lock_state + 1;
- HAL_Delay(500);
- break;
- case 3:
- Input_combination[3] = PinCode;
- TextLCD_Printf(&lcd, "%d", Input_combination[3]);
- complete = 1;
- HAL_Delay(500);
- break;
- }
- }
- }
- if(Input_combination[0] == Master_Code[0] && Input_combination[1] == Master_Code[1] && Input_combination[2] == Master_Code[2] && Input_combination[3] == Master_Code[3])
- {
- TextLCD_Printf(&lcd,"Master code!");
- HAL_GPIO_WritePin(GPIOA, LED_RED_Pin, GPIO_PIN_RESET);
- state = 0;
- }
- else
- TextLCD_Printf(&lcd, "Wrong Pin.");
- TextLCD_Clear(&lcd);
- TextLCD_Home(&lcd);
- HAL_Delay(1000);
- }
- //This function is for the regular unlock code and mater code.
- void Lock_state(TextLCDType lcd)
- {
- int lock_state=0, complete=0, PinCode;
- TextLCD_Clear(&lcd);
- TextLCD_Printf(&lcd, "PIN:");
- while(complete==0)
- {
- PinCode = Keypad_ScanKeyNum();
- if(PinCode < 10)
- {
- switch(lock_state)
- {
- case 0:
- Input_combination[0] = PinCode;
- TextLCD_Printf(&lcd, "%d", Input_combination[0]);
- lock_state = lock_state + 1;
- HAL_Delay(500);
- break;
- case 1:
- Input_combination[1] = PinCode;
- TextLCD_Printf(&lcd, "%d", Input_combination[1]);
- lock_state = lock_state + 1;
- HAL_Delay(500);
- break;
- case 2:
- Input_combination[2] = PinCode;
- TextLCD_Printf(&lcd, "%d", Input_combination[2]);
- lock_state = lock_state + 1;
- HAL_Delay(500);
- break;
- case 3:
- Input_combination[3] = PinCode;
- TextLCD_Printf(&lcd, "%d", Input_combination[3]);
- complete = 1;
- HAL_Delay(500);
- break;
- }
- }
- }
- TextLCD_Clear(&lcd);
- if(Input_combination[0] == Lock_combination[0] && Input_combination[1] == Lock_combination[1] && Input_combination[2] == Lock_combination[2] && Input_combination[3] == Lock_combination[3])
- {
- TextLCD_Printf(&lcd,"Correct Pin!");
- if(state == 0)
- {
- state = 1;
- HAL_GPIO_WritePin(GPIOA, LED_GREEN_Pin, GPIO_PIN_RESET);
- }
- else
- {
- state = 0;
- HAL_GPIO_WritePin(GPIOA, LED_YELLOW_Pin, GPIO_PIN_RESET);
- }
- }
- else if(Input_combination[0] == Master_Code[0] && Input_combination[1] == Master_Code[1] && Input_combination[2] == Master_Code[2] && Input_combination[3] == Master_Code[3])
- {
- TextLCD_Printf(&lcd,"Master code!");
- if(state == 0)
- {
- state = 1;
- HAL_GPIO_WritePin(GPIOA, LED_GREEN_Pin, GPIO_PIN_RESET);
- }
- else
- {
- state = 0;
- HAL_GPIO_WritePin(GPIOA, LED_YELLOW_Pin, GPIO_PIN_RESET);
- }
- }
- else
- TextLCD_Printf(&lcd, "Wrong Pin.");
- HAL_Delay(1000);
- TextLCD_Clear(&lcd);
- }
- //Function to check which of the keys have been pressed. (CHARACTER ONLY)
- int Keypad_ScanKeyChar()
- {
- unsigned char Keypad_character = 'K';
- Keypad_SetColumn1();
- HAL_Delay(5);
- if(Read_Keypad_Column1 && Read_Keypad_Row4)
- Keypad_character = 'A';
- else if(Read_Keypad_Column1 && Read_Keypad_Row3)
- Keypad_character = 'B';
- else if(Read_Keypad_Column1 && Read_Keypad_Row2)
- Keypad_character = 'C';
- else if(Read_Keypad_Column1 && Read_Keypad_Row1)
- Keypad_character = 'D';
- HAL_Delay(5);
- Keypad_SetColumn2();
- HAL_Delay(5);
- if(Read_Keypad_Column2 && Read_Keypad_Row1)
- Keypad_character = '#';
- HAL_Delay(5);
- Keypad_SetColumn4();
- HAL_Delay(5);
- if(Read_Keypad_Column3 && Read_Keypad_Row1)
- Keypad_character = '*';
- return Keypad_character;
- }
- //Function to check which of the keys have been pressed. (NUMBERS ONLY)
- int Keypad_ScanKeyNum()
- {
- uint16_t Keypad_number = 33;
- HAL_Delay(5);
- Keypad_SetColumn2();
- HAL_Delay(5);
- if(Read_Keypad_Column2 && Read_Keypad_Row4)
- Keypad_number = 3;
- else if(Read_Keypad_Column2 && Read_Keypad_Row3)
- Keypad_number = 6;
- else if(Read_Keypad_Column2 && Read_Keypad_Row2)
- Keypad_number = 9;
- HAL_Delay(5);
- Keypad_SetColumn3();
- HAL_Delay(5);
- if(Read_Keypad_Column3 && Read_Keypad_Row4)
- Keypad_number = 2;
- else if(Read_Keypad_Column3 && Read_Keypad_Row3)
- Keypad_number = 5;
- else if(Read_Keypad_Column3 && Read_Keypad_Row2)
- Keypad_number = 8;
- else if(Read_Keypad_Column3 && Read_Keypad_Row1)
- Keypad_number = 0;
- HAL_Delay(5);
- Keypad_SetColumn4();
- HAL_Delay(5);
- if(Read_Keypad_Column4 && Read_Keypad_Row4)
- Keypad_number = 1;
- else if(Read_Keypad_Column4 && Read_Keypad_Row3)
- Keypad_number = 4;
- else if(Read_Keypad_Column4 && Read_Keypad_Row2)
- Keypad_number = 7;
- HAL_Delay(5);
- return Keypad_number;
- }
- //Set column 1 low and the other high to see if a button on column 1 has been pressed or not.
- void Keypad_SetColumn1()
- {
- HAL_GPIO_WritePin(GPIOB, KEYPAD_1_Pin, GPIO_PIN_SET);
- HAL_GPIO_WritePin(GPIOB, KEYPAD_2_Pin | KEYPAD_3_Pin | KEYPAD_4_Pin, GPIO_PIN_RESET);
- }
- //See comment above, this is column 2
- void Keypad_SetColumn2()
- {
- HAL_GPIO_WritePin(GPIOB, KEYPAD_2_Pin, GPIO_PIN_SET);
- HAL_GPIO_WritePin(GPIOB, KEYPAD_1_Pin | KEYPAD_3_Pin | KEYPAD_4_Pin, GPIO_PIN_RESET);
- }
- //See comment above, this is column 3
- void Keypad_SetColumn3()
- {
- HAL_GPIO_WritePin(GPIOB, KEYPAD_3_Pin, GPIO_PIN_SET);
- HAL_GPIO_WritePin(GPIOB, KEYPAD_1_Pin | KEYPAD_2_Pin | KEYPAD_4_Pin, GPIO_PIN_RESET);
- }
- //See comment above, this is column 4
- void Keypad_SetColumn4()
- {
- HAL_GPIO_WritePin(GPIOB, KEYPAD_4_Pin, GPIO_PIN_SET);
- HAL_GPIO_WritePin(GPIOB, KEYPAD_1_Pin | KEYPAD_2_Pin | KEYPAD_3_Pin, GPIO_PIN_RESET);
- }
- //Serial UART numbers
- void UART_Transmit_num(int16_t num_value)
- {
- unsigned char transmit_num[sizeof(num_value)];
- sprintf(transmit_num, "%d", num_value);
- HAL_UART_Transmit(&huart2, transmit_num, strlen(transmit_num), 100);
- }
- //Serial UART character
- void UART_Transmit_string(char * message)
- {
- HAL_UART_Transmit(&huart2, message, strlen(message), 100);
- }
- /* USER CODE END 4 */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement