print("+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=")
print("\n\tMotion Sensor + LED + OLED Display")
print("\t\t(Motion Detector)")
print("\n\t\t\tBy:\n")
print("\t[Muhammad Irfan Bin Mohd Fardaus]")
print("\t\t (4/11/2024)\n")
print("+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=")
#import library
from machine import Pin, SoftI2C
from utime import sleep
import ssd1306
#Pin Declare
led_r = Pin(26, Pin.OUT)
led_g = Pin(32, Pin.OUT)
sensor = Pin(13, Pin.IN)
oled_pin = SoftI2C(scl=Pin(22), sda=Pin(21))
#Para Declare
led_r_blink = 5
led_r_delay = 0.5
led_g_delay = 0.5
sensor_delay = 0.02
#object declare
oled = ssd1306.SSD1306_I2C(width = 128, height = 64, i2c = oled_pin)
#Main Program
oled.fill(1)
oled.show()
oled.fill(0)
oled.show()
oled.text("Hello", 20, 25)
oled.show()
sleep(1)
for i in range(8):
oled.scroll(20, 0)
oled.show()
while True:
sensor_state = sensor.value()
sensor_state = bool(sensor_state)
print("Detection: "+str(sensor_state))
if sensor_state == True:
led_r.off()
oled.fill(0)
oled.text("Motion", 40, 25)
oled.text("Detected!", 30, 35)
oled.invert(1)
oled.show()
for i in range (led_r_blink):
led_g.on()
sleep(led_r_delay)
print("Detection: "+str(sensor_state))
led_g.off()
sleep(led_r_delay)
else:
led_g.off()
led_r.on()
oled.fill(0)
oled.text("No Motion", 28, 25)
oled.text("Detected!", 30, 35)
oled.invert(0)
oled.show()
sleep(led_g_delay)
sleep(sensor_delay)