print("MINI PROJECT")
print('By: ALIF FITRI')
print('Date: 20/6/2024')
# Import libraries
import library_oled
from machine import Pin, SoftI2C, PWM, ADC
from utime import sleep
# Pin declaration
Red_led = Pin(26, Pin.OUT)
Green_led = Pin(27, Pin.OUT)
buzzer = PWM(Pin(5, Pin.OUT))
oled_pin = SoftI2C(scl=Pin(23), sda=Pin(22))
ldr_pin = ADC(Pin(14)) # Assuming LDR is connected to pin 32 (ADC1 channel)
# Create an object
display = library_oled.SSD1306_I2C(width=128, height=64, i2c=oled_pin, external_vcc=False)
# Display OLED initial message
display.fill(1)
display.text("SYSTEM CONDITION", x=0, y=10, col=0)
display.text("**STANDBY**", x=20, y=30, col=0)
display.text("0 SMOKE DETECTED", x=0, y=50, col=0)
display.show()
# Main program
while True:
ldr_value = ldr_pin.read()
print("LDR Value: {ldr_value}")
if ldr_value < 500:
# High light intensity detected
display.fill(1)
display.text("SYSTEM CONDITION", x=0, y=10, col=0)
display.text("**ALERT**", x=20, y=30, col=0)
display.text("SMOKE DETECTED", x=0, y=50, col=0)
display.show()
Green_led.off()
buzzer.duty(512) # Half duty cycle to make the buzzer sound
# Blink red LED fast
for _ in range(10): # Blink 10 times
Red_led.on()
sleep(0.1)
Red_led.off()
sleep(0.1)
else:
# Normal condition
Green_led.on()
buzzer.duty(0) # Turn off the buzzer
display.fill(1)
display.text("SYSTEM CONDITION", x=0, y=10, col=0)
display.text("**STANDBY**", x=20, y=30, col=0)
display.text("0 SMOKE DETECTED", x=0, y=50, col=0)
display.show()
sleep(1) # Delay for 1 second before reading the sensor again