from machine import Pin, ADC, SoftI2C, Timer
from ssd1306 import SSD1306_I2C
import time
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
pot_adc = ADC(Pin(4))
pir_digital = Pin(33, Pin.IN)
ss_digital = Pin(17, Pin.IN)
pb_digital = Pin(25, Pin.IN, Pin.PULL_UP)
#pb_digital = Pin(25, Pin.IN)
ds_digital = Pin(32,Pin.IN)
rgb_red = Pin(27, Pin.OUT)
rgb_green = Pin(14, Pin.OUT)
rgb_blue = Pin(12, Pin.OUT)
led = Pin(26, Pin.OUT)
# Initialize I2C for OLED
#i2c = I2C(0, scl=Pin(22), sda=Pin(21))
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=100000)
oled = SSD1306_I2C(128, 64, i2c)
I2C_ADDR2 = 0x27
totalRows = 4
totalColumns = 16
lcd = I2cLcd(i2c, I2C_ADDR2, totalRows, totalColumns)
# Display initialization message
oled.fill(0)
oled.text("Initializing...", 0, 0)
oled.show()
time.sleep(2)
#####################################
###### SUBROUTINE FOR INTERRUPT
# create a function to be called when the trigger occurs
# this function just toggles the onboard LED
def interrupt_ds(pin):
print('Interrupt by:', pin)
rgb_red.off()
rgb_green.on()
rgb_blue.off()
time.sleep(1)
rgb_red.off()
rgb_green.off()
rgb_blue.off()
# Interrupt trigger
ds_digital.irq(trigger=Pin.IRQ_RISING, handler=interrupt_ds)
#####################################
###### SUBROUTINE FOR TIMER
# create an uninitialized timer object
myTimer = Timer(1)
# create a function to be called when the timer goes off
# this function just toggles the onboard LED
def toggle_led(myTimer):
rgb_red.off()
rgb_green.off()
rgb_blue.on()
#time.sleep(0.5)
#rgb_red.off()
#rgb_green.off()
#rgb_blue.off()
#time.sleep(0.5)
# initialize the timer object to tick every second (1,000 milliseconds)
myTimer.init(period=1000, mode=Timer.PERIODIC, callback=toggle_led)
#####################################
###### Main loop
while True:
pot_value = pot_adc.read()
# Clear the display
oled.fill(0)
# Display the heartbeat value
oled.text("Heartbeat:", 0, 0)
oled.text(str(pot_value), 0, 10)
# Update the display
oled.show()
# Read the state of the slide switch
ss_state = ss_digital.value()
# Check the state of the slide switch
if ss_state == 1:
# Slide switch is ON, turn on the LED
lcd.move_to(0,0)
lcd.putstr('Hi')
else:
# Slide switch is OFF, turn off the LED
lcd.move_to(0,0)
lcd.putstr(' ')
#get the input response
pb_state = pb_digital.value()
#if button1 is pushed (1 means pushed)
if pb_state == 0:
#turn ON led_red1
led.on()
time.sleep(1)
else:
led.off()
#get the input response
pir_state = pir_digital.value()
#if button1 is pushed (1 means pushed)
rgb_red.off()
rgb_green.off()
rgb_blue.off()
if pir_state == 1:
#turn ON led_red1
rgb_red.on()
rgb_green.off()
rgb_blue.off()
time.sleep(1)
#else:
# rgb_red.off()
# rgb_green.off()
# rgb_blue.off()
# Wait before taking another reading
time.sleep(0.5)