print("This program will test Adjustable Fan Speed")
print("Create by : Muhammad Muqri Bin Ahmad Zamri")
print("Date : 29/4/2024")

# Import libraries
from machine import Pin, PWM, SoftI2C
import oled_lib
import servo_lib
import dht
from utime import sleep

# Pin declaration
oled_pin = SoftI2C(scl=Pin(22), sda=Pin(21))
servo_pin = Pin(2)  
led1_pin = 4
led2_pin = 5
led3_pin = 18
sensor = dht.DHT22(Pin(13))
servo = servo_lib.Servo(pin=servo_pin)  # Initialize the servo
button_0 = Pin(26, Pin.IN, Pin.PULL_UP)  # Initialize push button on pin 12 (Stop Button)
button_1 = Pin(27, Pin.IN, Pin.PULL_UP)  # Initialize push button on pin 14 (Rotate Slow)
button_2 = Pin(14, Pin.IN, Pin.PULL_UP)  # Initialize push button on pin 27 (Rotate Medium)
button_3 = Pin(12, Pin.IN, Pin.PULL_UP)  # Initialize push button on pin 26 (Rotate Fast)

# Create Object Instances
sensor = dht.DHT22(Pin(13))
servo = servo_lib.Servo(pin=servo_pin)
led1 = Pin(led1_pin, Pin.OUT)
led2 = Pin(led2_pin, Pin.OUT)
led3 = Pin(led3_pin, Pin.OUT)
oled = oled_lib.SSD1306_I2C(width=128, height=64, i2c=oled_pin, external_vcc=False)

# Variable to track servo state and speed
servo_running = False
speed_factor = 1  # Default speed factor

# Function to control servo motor
def Servo(angle, speed_factor):
    # Calculate duty cycle corresponding to the given angle and speed factor
    duty_cycle = int(((angle) / 180 * 2 + 0.5) / 20 * 1023 * speed_factor)
    # Set duty cycle
    servo.duty(duty_cycle)

# Interrupt callback function for button press
def button_callback(pin):
    global servo_running, speed_factor
    if pin == button_0:  # Stop Button
        servo_running = False
        servo.duty = 0  # Stop the servo motor
        speed_factor = 1  # Reset speed factor
    elif pin == button_1:  # Rotate Slow
        speed_factor = 0.5
        servo_running = True
    elif pin == button_2:  # Rotate Medium
        speed_factor = 1
        servo_running = True
    elif pin == button_3:  # Rotate Fast
        speed_factor = 2
        servo_running = True

# Register button interrupts
button_0.irq(trigger=Pin.IRQ_FALLING, handler=button_callback)
button_1.irq(trigger=Pin.IRQ_FALLING, handler=button_callback)
button_2.irq(trigger=Pin.IRQ_FALLING, handler=button_callback)
button_3.irq(trigger=Pin.IRQ_FALLING, handler=button_callback)

# Main Program
while True:
    # Measure temperature and humidity
    sensor.measure()
    t = sensor.temperature()
    h = sensor.humidity()

    # Control LEDs and servo based on conditions
    if h < 50 and t < 25:  # Conditions to turn off the servo
        led1.on()  # Turn on LED1
        led2.off()  # Turn off LED2
        servo.duty = 0  # Stop the servo motor
        servo_running = False  # Stop the servo operation
    elif h > 50 and t > 25:  # Conditions to turn on the servo
        led1.off()  # Turn off LED1
        led2.on()  # Turn on LED2
        if not servo_running:  # Start the servo motor if it's not running
            servo_running = True
            speed_factor = 1  # Default speed factor
            servo.duty = 0  # Reset servo duty
    else:
        led1.off()  # Turn off LED1
        led2.off()  # Turn off LED2

    # Check if the servo should run
    if servo_running and not led1.value():
        # Rotate servo motor based on speed factor
        for angle in range(0, 181):  # Rotate from 0 to 180
            servo.move(angle, speed_factor)
            sleep(0.01)
        for angle in range(180, -1, -1):  # Rotate from 180 to 0
            servo.move(angle, speed_factor)
            sleep(0.01)

        # Display speed status on OLED when fan is running
        oled.fill(0)  # Clear display (0 is usually black)
        oled.text("Fan is running", 13, 20, 1)  # Default text
        oled.text("Speed: ", 13, 40, 1)  # Show speed text
        if speed_factor == 0.5:
            oled.text("Slow", 70, 30, 1)
        elif speed_factor == 1:
            oled.text("Moderate", 70, 30, 1)
        elif speed_factor == 2:
            oled.text("High", 70, 30, 1)
        oled.show()

    # Measure temperature and humidity
    sensor.measure()
    t = sensor.temperature()
    h = sensor.humidity()
    print("Temperature:", t)
    print("Humidity:", h)

    # Clear OLED display
    oled.fill(0)

    # Check the state of LEDs and display corresponding messages on OLED
    if led1.value() == 1:
        oled.text("OFF", 40, 30)
    elif led2.value() == 1:
        oled.text("ON", 30, 30)

    # Display temperature and humidity on OLED
    oled.text("Temp: {:.2f} C".format(t),5, 20, 1)
    oled.text("Humidity: {:.2f}%".format(h),5, 40, 1)
    oled.show()

    # Control LEDs and servo based on conditions
    if h < 50 and t < 40:
        led1.on()
    else:
        led1.off()

    if h > 50 and t > 40:
        led2.on()
    else:
        led2.off()














Loading
esp32-devkit-v1