from machine import Pin, PWM, I2C
import time
import ssd1306
s0_pin = Pin(15, Pin.OUT)
s1_pin = Pin(4, Pin.OUT)
s2_pin = Pin(5, Pin.OUT)
s3_pin = Pin(18, Pin.OUT)
out_pin = Pin(19, Pin.IN)
i2c = I2C(-1, scl=Pin(22), sda=Pin(21))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
red_color, green_color, blue_color = 0, 0, 0
def setup():
s0_pin.value(1)
s1_pin.value(0)
print("Welcome to MVC-DBT")
time.sleep(4)
def pulse_in(pin, state, timeout=1000000):
start_time = time.ticks_us()
while pin.value() != state:
if time.ticks_diff(time.ticks_us(), start_time) > timeout:
return -1
pulse_start_time = time.ticks_us()
while pin.value() == state:
pass
pulse_duration = time.ticks_diff(time.ticks_us(), pulse_start_time)
return pulse_duration
def map_value(value, in_min, in_max, out_min, out_max):
return int((value - in_min) * (out_max - out_min) / (in_max - in_min) + out_min)
if __name__ == "__main__":
setup()
while True:
s2_pin.value(0)
s3_pin.value(0)
red = pulse_in(out_pin, 0)
red = map_value(red, 25, 72, 255, 0)
red = min(255, max(0, red))
s2_pin.value(1)
s3_pin.value(1)
green = pulse_in(out_pin, 0)
green = map_value(green, 30, 90, 255, 0)
green = min(255, max(0, green))
s2_pin.value(0)
s3_pin.value(1)
blue = pulse_in(out_pin, 0)
blue = map_value(blue, 25, 70, 255, 0)
blue = min(255, max(0, blue))
print("RED:", red, "GREEN:", green, "BLUE:", blue)
oled.fill(0)
oled.text('Red : '+str(red), 0, 0)
oled.text('Green : '+str(green), 0, 10)
oled.text('Blue : '+str(blue), 0, 20)
oled.show()
time.sleep(5)