Skip to content
Snippets Groups Projects
Commit 36b9d143 authored by Evan Jones's avatar Evan Jones
Browse files

Replace main.py

parent 79da3a4c
No related branches found
No related tags found
No related merge requests found
......@@ -72,12 +72,13 @@ def notify_audio(duration):
# === Sensing Functions ===
def detect_button_press():
global last_button_state
button = digitalRead(button_port)
#makes sure button only goes once if held down
if button == 0 and last_button_state == 1:
log_event("EVENT", "Button Pressed")
set_display("Ringing", "")
set_display("Ringing")
notify(preferences)
time.sleep(2)
set_display("Smart Doorbell", "Ready")
......@@ -86,30 +87,21 @@ def detect_button_press():
return
def detect_motion():
motion = digitalRead(pir_port)
if motion == 1 and not motion_detected:
print("Motion detected!")
motion_detected = True
no_motion_counter = 0
elif motion == 0:
no_motion_counter += 1
if motion_detected and no_motion_counter > 5:
print("motion stopped")
motion_detected = False
return
def detect_light_level():
global light_dark_counter, light_trigger_delay, night_mode
light = analogRead(light_port)
if light < light_dark_threshold:
light_dark_counter += 1
if light_dark_counter >= light_trigger_delay and not night_mode:
log_event("EVENT", "SIGNIFICANT DARKNESS DETECTED")
#log_event("EVENT", "SIGNIFICANT DARKNESS DETECTED")
adjust_lights()
else:
if night_mode:
log_event("EVENT", "SIGNIFICANT LIGHT DETECTED")
#log_event("EVENT", "SIGNIFICANT LIGHT DETECTED")
adjust_lights()
else:
return
......@@ -121,11 +113,11 @@ def set_display(message):
if len(message) > 32:
scroll_text(message)
return
# Split and display in 2 lines if it fits
line1 = message[:16]
line2 = message[16:32] if len(message) > 16 else ""
setText(f"{line1}\n{line2}")
else:
# Split and display in 2 lines if it fits
line1 = message[:16]
line2 = message[16:32] if len(message) > 16 else ""
setText(f"{line1}\n{line2}")
except Exception as e:
print(f"[LCD Error] Could not update display: {e}")
......@@ -147,12 +139,12 @@ def scroll_text(message, delay=0.3, loop_count=2, idle_message="Smart Doorbell R
print(f"[LCD Scroll Error] {e}")
def adjust_lights():
night_mode = not night_mode
#night_mode = not night_mode
return
# === System Functions ===
def log_event(type, message):
print(f"[{type}] ({now}): {message}")
def log_event(mtype, message):
print(f"[{mtype}] ({now}): {message}")
return
def self_check():
......@@ -212,21 +204,22 @@ def self_check():
# === Main Reaction Loop ===
def main_loop():
print("System Starting")
set_display("Smart Doorbell", "Ready")
set_display("Smart Doorbell Ready")
try:
while True:
detect_button_press()
detect_motion()
detect_light_level()
print("[DEBUG]: LOOP")
time.sleep(0.2)
except KeyboardInterrupt:
print("Smart doorbell shutdown.")
set_display("SYSTEM HALTED", "KEYBRD INTERRUPT")
set_display("SYSTEM HALTED KEYBOARD INTERRUPT")
# Run
if __name__ == "__main__":
initialise_sensors()
self_check()
#self_check()
main_loop()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment