This code is written for a medical emergency alert system using a GSM module. It utilizes a button to trigger an emergency alert, which sends an SMS and makes a call to a predefined number.
The code includes the SoftwareSerial
library, which allows serial communication on pins other than the default Serial
communication pins (0 and 1).
BUTTON_PIN
(4): The pin connected to the button that triggers the emergency alert.LED_PIN
(2): The pin connected to an LED indicator.BUZZER_PIN
(3): The pin connected to a buzzer.LED2_PIN
(5): The pin connected to a second LED indicator.BUZZER2_PIN
(6): The pin connected to a second buzzer.
The setup()
function is called once when the microcontroller starts. It initializes the GSM module and sets the pins as inputs or outputs. The setup tasks include:
- Initializing the
SIM900A
SoftwareSerial object with the RX (receive) and TX (transmit) pins connected to the GSM module (9 and 10, respectively). - Setting the baud rate of the GSM module to 9600 using
SIM900A.begin(9600)
. - Starting the serial communication with the Serial Monitor in the Arduino IDE at a baud rate of 9600 using
Serial.begin(9600)
. - Configuring the button pin, LED pins, and buzzer pins as inputs or outputs.
- Printing a message to the Serial Monitor to indicate that the text message module is ready.
The SendMessage()
function is responsible for sending an SMS. It performs the following steps:
- Sends the command
AT+CMGF=1
to initialize the GSM module in text mode. - Waits for 1 second using
delay(1000)
. - Sends the command
AT+CMGS=\"+919618531821\"\r
, where+919618531821
is the receiver's mobile number. - Waits for 1 second.
- Sends the message content
"PATIENT MEDICAL EMERGENCY ALERT SYSTEM"
. - Waits for 100 milliseconds.
- Sends the end-of-message character
(char)26
to indicate the end of the message content. - Waits for 1 second.
The SendCall()
function is responsible for making a call. It performs the following steps:
- Prints a message to the Serial Monitor to indicate that the call is being made.
- Initializes the
SIM900A
SoftwareSerial object with a baud rate of 9600. - Waits for 2 seconds using
delay(2000)
. - Sends the command
ATD8978722723;
to make a call to the number8978722723
. Note that you need to replace this number with the desired phone number. - Prints a message to the Serial Monitor to indicate the number being called.
- If there is data available to read from the GSM module, it reads and writes it to the Serial Monitor.
The loop()
function is the main part of the code that runs repeatedly. It performs the following steps:
- Reads the state of the button connected to
BUTTON_PIN
. - If the button is pressed (
buttonState == LOW
), the code executes the following steps:- Prints a message to the Serial Monitor to indicate that the button is pressed.
- Enters a while loop that runs as long as the button is held down.
- Turns on the LEDs (
LED_PIN
andLED2_PIN
) and activates the buzzers (BUZZER_PIN
and `
- Turns on the LEDs (
BUZZER2_PIN`). - Waits for 500 milliseconds. - Turns off the LEDs and deactivates the buzzers. - Waits for another 500 milliseconds.
- If the button is held for more than 10 seconds (10,000 milliseconds), it calls the
SendCall()
function to make a call and theSendMessage()
function to send an SMS. - Waits for 100 milliseconds.
- If there is data available to read from the
SIM900A
SoftwareSerial object, it reads and writes it to the Serial Monitor.
That's the explanation of the provided code. It sets up the GSM module, detects button presses, and triggers an emergency alert by making a call and sending an SMS.