print("Program: Emergency Alert")
print("Date: 24/10/2024")
print("Created by: Wak")
# Import Libraries
from machine import Pin, SoftI2C
from utime import sleep
import oled_lib # Ensure oled_lib is available
# Pin Declarations
red_led_pin = Pin(12, Pin.OUT) # Red LED for emergency system
buzzer_pin = Pin(13, Pin.OUT) # Buzzer for emergency system
button_pin = Pin(15, Pin.IN, Pin.PULL_UP) # Button with internal pull-up for toggle
oled_i2c = SoftI2C(scl=Pin(22), sda=Pin(21)) # OLED I2C for display
slide_pin = Pin(2, Pin.IN, Pin.PULL_UP) # Slide switch for reset
# OLED Object Declaration
wak = oled_lib.SSD1306_I2C(width=128, height=64, i2c=oled_i2c)
# Variables
emergency_state = False # Emergency state flag
system_active = False # Control system on/off state
previous_button_state = 1 # Initial state of button
# Function to make buzzer beep
def buzzer_beep(duration=0.2):
buzzer_pin.on()
sleep(duration)
buzzer_pin.off()
# Main Program
print("System Initialized.")
while True:
# Reset system if slide switch is toggled
if slide_pin.value() == 0:
emergency_state = False
system_active = False
print("System Reset")
wak.fill(0)
wak.text("System Reset", 10, 20, 1)
wak.show()
sleep(0.5) # Debounce delay for slide switch
continue
# Read button state
current_button_state = button_pin.value()
# Toggle system on/off when button is pressed
if current_button_state == 0 and previous_button_state == 1:
system_active = not system_active # Toggle system state
print("System Activated" if system_active else "System Deactivated")
buzzer_beep(0.3) # Beep for system state change
# If system is active, handle emergency logic
if system_active:
if emergency_state:
# Emergency active: LED and SOS message
red_led_pin.on()
wak.fill(0)
wak.text("Emergency SOS!", 10, 20, 1)
wak.show()
else:
# Normal system state
red_led_pin.off()
wak.fill(0)
wak.text("System Normal", 10, 20, 1)
wak.show()
# Emergency state toggled by button press
if current_button_state == 0 and previous_button_state == 1:
emergency_state = not emergency_state
print("Emergency Mode Activated" if emergency_state else "Emergency Mode Deactivated")
buzzer_beep(0.2) # Beep for emergency state toggle
else:
# System is off
red_led_pin.off()
buzzer_pin.off()
wak.fill(0)
wak.text("System Off", 10, 20, 1)
wak.show()
# Update button state for debouncing
previous_button_state = current_button_state
# Debounce delay
sleep(0.1)