Coding
Note: We will be going through the steps in completing this program as all programming techniques and functions required for this program have already been covered in previous lesson through the course. The final source code will be included at the end. Remember on the MFS the digital commands are reversed, HIGH = OFF and LOW = ON. Refer to the source code for guidance.
Global Scope - To start of our programming sketch, we will begin by declaring all required information in the global scope. We will first include any libraries we can use within the program. For this system we will be using the 4x7 segment display and therefore we will need to use the MFS library and the timer one library. We must also declare any global variables which can be used in any function throughout the program. We will need to declare two LEDs, two push buttons and the buzzer. The buzzer will be used to give an audible indication for the alarm and it is connected to digital pin 3 on the MFS. After the digital pins have been declared, we also need to declare digital variables for the two separate buttons to determine their digital status. (Refer to Lesson 5) Remember to group the variable names to avoid syntax errors and confusions.
Setup - Moving on to our setup, we need to configure the pin mode for all digital components which will either be used as inputs or outputs. Then we will initialise the timer library, followed by the MFS library which is initialised in conjunction with the timer.
Loop - In the main loop section of our program, we need to write instruction sets to detect whether the alarm is armed or disarmed. Taking code efficiency in to consideration, we will use two individual subroutine functions (Arm and Disarm). Before we can move to the functions, in the loop we will program button 1 to be used as a toggle switch. When the button is on, the arm subroutine will be called for and alternatively, when the button is off, the disarmed subroutine will be called for. (Remember ON = LOW and OFF = HIGH)
Function 1 (Arm) - Whilst button 1 is toggled as low (on), this will call for the Arm subroutine. The program specification states that when the alarm is armed, the MFS must read as 'on' and there must be an LED indication. During this state, if button 3 is toggled to be on, the alarm must trigger and a different LED must show this, as well as a buzzer alert. The way we will code this is by again writing the instruction set for button 3 and when the button state is low (on) the alarm will activate and if button 3 is high (off) the trigger state will exit and reset the alarm.
Function 2 (Disarm) - Whilst button 1 is toggled as high (off), this will call for the Disarm subroutine. When the alarm is disarmed, the MFS display must read as 'off' and there should be no operation happening until button 1 is toggled again to exit this subroutine. To avoid bugs and errors, we will add code to turn off all LEDs and the buzzer in this subroutine.
This program is now complete and the source code is available below to refer to. When using the MFS it is important to remember the digital states are reversed due to the electronic configuration internally.