from machine import Pin
from machine import Pin, I2C
import machine
import ssd1306
import time
scl_pin = 26
sda_pin = 25
i2c = machine.I2C(0, scl=machine.Pin(scl_pin), sda=machine.Pin(sda_pin))
switch1 = Pin(14, Pin.IN, Pin.PULL_UP)
switch2 = Pin(12, Pin.IN, Pin.PULL_UP)
switch3 = Pin(13, Pin.IN, Pin.PULL_UP)
switch4 = Pin(16, Pin.IN, Pin.PULL_UP)
switch5 = Pin(17, Pin.IN, Pin.PULL_UP)
switch6 = Pin(18, Pin.IN, Pin.PULL_UP)
switch7 = Pin(19, Pin.IN, Pin.PULL_UP)
switch8 = Pin(21, Pin.IN, Pin.PULL_UP)
switch9 = Pin(22, Pin.IN, Pin.PULL_UP)
led_verte = Pin(23, Pin.OUT)
led_jaune = Pin(5, Pin.OUT)
led_rouge = Pin(2, Pin.OUT)
relay = Pin(0, Pin.OUT)
while True:
s1 = not switch1.value()
s2 = not switch2.value()
s3 = not switch3.value()
s4 = not switch4.value()
s5 = not switch5.value()
s6 = not switch6.value()
s7 = not switch7.value()
s8 = not switch8.value()
s9 = not switch9.value()
premiere = s1 and (not s2) and s3
deuxieme = s4 and s5 and (not s6)
troisieme = (not s7) and s8 and s9
if premiere and deuxieme and troisieme:
led_verte.value(1)
led_jaune.value(0)
led_rouge.value(0)
relay.value(0)
elif premiere or deuxieme or troisieme:
led_verte.value(0)
led_jaune.value(1)
led_rouge.value(0)
relay.value(1)
else:
led_verte.value(0)
led_jaune.value(0)
led_rouge.value(1)
relay.value(1)
time.sleep(0.05)
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
WIDTH = 128
HEIGHT = 64
oled = ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c)
dstart_time = None
running = False
def format_time(elapsed_ms):
total_seconds = elapsed_ms // 1000
minutes = total_seconds // 60
seconds = total_seconds % 60
milliseconds = (elapsed_ms % 1000) // 10 # deux decimals
return "{:02d}:{:02d}.{:02d}".format(minutes, seconds, milliseconds)
def draw_text_centered(text, y):
# effacer lecran et afficher le texte centré sur la ligne horyzontal a la ligne y
oled.fill(0)
width = len(text) * 8
x = (WIDTH - width) // 2
oled.text(text, x, y)
oled.show()
def start_stopwatch():
global start_time, running
start_time = time.ticks_ms()
running = True
def get_elapsed():
if not running or start_time is None:
return 0
return time.ticks_diff(time.ticks_ms(), start_time)
def main():
global running
start_stopwatch()
while True:
elapsed = get_elapsed()
time_str = format_time(elapsed)
draw_text_centered(time_str, HEIGHT//2 - 8)
time.sleep_ms(100) # actualisé toutes les 100ms
if __name__ == "__main__":
main()
oled.show()