EloiStree / HelloEloiTeachingVideo

Page pour les vidéos sur Eloi Teaching.
0 stars 0 forks source link

Pico W à Warcraft, 06: Relay pour des moteurs et des manettes. #113

Open EloiStree opened 3 days ago

EloiStree commented 3 days ago

https://youtu.be/6aik49xUCgI

import time
import digitalio
import board

# Set up pins for directional relays (left, right, up, down)
DIR_LEFT_PIN = board.GP10
DIR_RIGHT_PIN = board.GP11
DIR_UP_PIN = board.GP12
DIR_DOWN_PIN = board.GP13
SHURIKEN_PIN = board.GP7
SWORD_PIN = board.GP8
RESTART_PIN = board.GP9

# Initialize relay pins
left_relay = digitalio.DigitalInOut(DIR_LEFT_PIN)
left_relay.direction = digitalio.Direction.OUTPUT

right_relay = digitalio.DigitalInOut(DIR_RIGHT_PIN)
right_relay.direction = digitalio.Direction.OUTPUT

up_relay = digitalio.DigitalInOut(DIR_UP_PIN)
up_relay.direction = digitalio.Direction.OUTPUT

down_relay = digitalio.DigitalInOut(DIR_DOWN_PIN)
down_relay.direction = digitalio.Direction.OUTPUT

shuriken_relay = digitalio.DigitalInOut(SHURIKEN_PIN)
shuriken_relay.direction = digitalio.Direction.OUTPUT

sword_relay = digitalio.DigitalInOut(SWORD_PIN)
sword_relay.direction = digitalio.Direction.OUTPUT

restart_relay = digitalio.DigitalInOut(RESTART_PIN)
restart_relay.direction = digitalio.Direction.OUTPUT

dico_pin_inverse = {
    DIR_LEFT_PIN: True,
    DIR_RIGHT_PIN: True,
    DIR_UP_PIN: True,
    DIR_DOWN_PIN: True,
    SHURIKEN_PIN: False,
    SWORD_PIN: True,
    RESTART_PIN: False
}

dico_pin = {
    DIR_LEFT_PIN: left_relay,
    DIR_RIGHT_PIN: right_relay,
    DIR_UP_PIN: up_relay,
    DIR_DOWN_PIN: down_relay,
    SHURIKEN_PIN: shuriken_relay,
    SWORD_PIN: sword_relay,
    RESTART_PIN: restart_relay
}

def set_pin_state(relay_pin, state):
    """Set relay pin state considering inverse logic."""
    if dico_pin_inverse[relay_pin]:
        dico_pin[relay_pin].value = not state  # Invert the state
    else:
        dico_pin[relay_pin].value = state

def set_active_pin_for_delay(relay_pin, duration=0.5, description=""):
    """Activate pin for a delay, then deactivate."""
    print(description)
    set_pin_state(relay_pin, True)  # Activate relay (set high)
    time.sleep(duration)            # Wait for the specified duration
    set_pin_state(relay_pin, False) # Deactivate relay (set low)
    time.sleep(duration)            # Wait for the specified duration

# Main loop to trigger the relays
while True:
    delay = 0.3
    set_active_pin_for_delay(DIR_LEFT_PIN, delay, description="Left")
    set_active_pin_for_delay(DIR_RIGHT_PIN, delay, description="Right")
    set_active_pin_for_delay(DIR_UP_PIN, delay, description="Up")
    set_active_pin_for_delay(DIR_DOWN_PIN, delay, description="Down")
    set_active_pin_for_delay(SHURIKEN_PIN, delay, description="Shuriken")
    set_active_pin_for_delay(SWORD_PIN, delay, description="Sword")
    set_active_pin_for_delay(RESTART_PIN, delay, description="Restart")
    time.sleep(0.1)  # Optional, adjust this as needed