from machine import Pin, I2C, ADC, PWM
import ssd1306
import time
# INITIALISER LES PINS
# Led RGB
red = Pin(7, Pin.OUT)
green = Pin(8, Pin.OUT)
blue = Pin(9, Pin.OUT)
# OLED
i2c = I2C(0, sda=Pin(20), scl=Pin(21))
display = ssd1306.SSD1306_I2C(128, 64, i2c)
# Bouton
bouton_manu = Pin(2, Pin.IN, Pin.PULL_UP)
bouton_auto = Pin(3, Pin.IN, Pin.PULL_UP)
# Potentiomètre
adc_acceleration = ADC(26)
adc_direction = ADC(27)
# Initialisation des variables de message
msg_manu = ""
msg_auto = ""
# Fonction pour gérer le mode manuel
def Mode_manu():
global msg_manu, msg_auto
red.on()
green.off() # Éteindre la LED verte
msg_manu = "manuel"
msg_auto = "" # Réinitialiser le message du mode automatique
# Fonction pour gérer le mode automatique
def Mode_auto():
global msg_auto, msg_manu
green.on()
red.off() # Éteindre la LED rouge
msg_auto = "auto"
msg_manu = "" # Réinitialiser le message du mode manuel
# BOUCLE
while True:
display.fill(0) # Effacer l'écran
adcValue1 = adc_acceleration.read_u16() # Lecture de la valeur de l'accélération du pot
adcValue2 = adc_direction.read_u16() # Lecture de la valeur du degré de la direction gauche/droite
angle_direction = round(adcValue2 / 65535 * 180) # Change la lecture en degré
if not bouton_manu.value():
Mode_manu()
if not bouton_auto.value():
Mode_auto()
if angle_direction <= 90:
display.text("tourne: gauche", 0, 30)
else:
display.text("tourne: droite", 0, 30)
display.text("acceleration:{}".format(adcValue1), 0, 10)
display.text("direction:{}%".format(angle_direction), 0, 20)
display.text("mode: {}".format(msg_manu), 0, 40) # Afficher le mode manuel
display.text("mode: {}".format(msg_auto), 0, 40) # Afficher le mode automatique
display.show()
time.sleep(0.1)