from machine import Pin, PWM, I2C, ADC
import ssd1306
import time
#configuration du I2C
i2c = I2C(0, sda=Pin(8), scl=Pin(9))
display = ssd1306.SSD1306_I2C(128, 64, i2c) # create SSD1306 object
#configuration moteur DC
motor=0
EN= PWM(Pin(16))
EN.freq(15000)
in1= Pin(14, Pin.OUT)
in2= Pin(15, Pin.OUT)
#configuration du Servo
servo=PWM(Pin(17))
servo.freq(50)
#configuration du potentiometre
pot = ADC(Pin(26))
#configuration joystick
stick = ADC(Pin(27))
button= Pin(10, Pin.IN, Pin.PULL_UP)
button_memory = 0
#configurration des leds (j'ai perdu mon rgb désolé)
rouge=PWM(Pin(12))
rouge.freq(50)
vert=PWM(Pin(11))
vert.freq(50)
bleu=PWM(Pin(13))
bleu.freq(50)
while 1:
#Code bascuelemnt entre l'état d'arrêt et l'état de marche du moteur
if button_memory != button.value() and not button.value():
if motor == 0:
motor = 1
elif motor == 1:
motor = 0
button_memory = button.value()
if motor: #Si la variable moteur est égale à un, le moteur tourne
in1.high()
in2.low()
else:
in1.low()
in2.low()
stick_value=stick.read_u16()
if stick_value > 60000:
EN.duty_u16(65535)
else:
EN.duty_u16(0)
angle=180*pot.read_u16()/65535
duty = int((34*angle)+1000)
servo.duty_u16(duty)
print("Angle Potentio & Servo :",int(angle),"degres")
print("Joystick :", stick_value)
print("Button :",button.value())
print("Moteur :",motor)
print()
#Configuration affichage de l'OLED
display.fill(0)
display.text("Servo angle:"+str(int(angle)), 0, 0)
if motor:
display.text("Motor ON", 0, 10)
if stick_value > 60000:
display.text("Forward", 0, 20)
elif stick_value < 10000:
display.text("Backward", 0, 20)
else:
display.text("Stop", 0, 20)
else:
display.text("Motor OFF",0,10)
display.show()
#Activation des LED avec le potentiomètre
if angle < 60:
rouge.duty_u16(65535)
vert.duty_u16(0)
bleu.duty_u16(0)
elif 60 < angle < 120:
rouge.duty_u16(65535)
bleu.duty_u16(0)
vert.duty_u16(65535)
else:
rouge.duty_u16(0)
bleu.duty_u16(0)
vert.duty_u16(65535)
time.sleep(0.02)