Untitled

mail@pastecode.io avatar
unknown
plain_text
13 days ago
3.0 kB
4
Indexable
Never
import VisionFive.gpio as gpio
import time

# Define your pin numbers (adjust as per your actual pin numbers)
IR_sensor = 28        # Example pin number for IR sensor
led = 37              # Example pin number for main LED
led_pin_wrong = 15    # Pin for incorrect password LED indicator
led_pin_correct = 18  # Pin for correct password LED indicator
buzzer_pin = 7        # GPIO pin for the buzzer
signal_pin_wrong = 22 # GPIO pin for wrong password signal input
signal_pin_correct = 26 # GPIO pin for correct password signal input
SERVO_PIN = 27        # GPIO pin for servo motor control

# Set up the GPIO using 'BOARD' mode
gpio.setmode(gpio.BOARD)
gpio.setup(IR_sensor, gpio.IN)
gpio.setup(led, gpio.OUT)
gpio.setup(led_pin_wrong, gpio.OUT)
gpio.setup(led_pin_correct, gpio.OUT)
gpio.setup(buzzer_pin, gpio.OUT)
gpio.setup(signal_pin_wrong, gpio.IN)
gpio.setup(signal_pin_correct, gpio.IN)
gpio.setup(SERVO_PIN, gpio.OUT)

# Set up the servo motor
servo = gpio.PWM(SERVO_PIN, 50)  # Set frequency to 50Hz
servo.start(2.5)  # Start PWM with 0% duty cycle (servo off)

def set_servo_angle(angle):
    try:
        duty = angle / 18 + 2
        gpio.output(SERVO_PIN, True)
        servo.ChangeDutyCycle(duty)
 time.sleep(0.5)
        gpio.output(SERVO_PIN, False)
        servo.ChangeDutyCycle(0)
    except Exception as e:
        print(f"Error setting servo angle: {e}")

current_led = None

def main():
    global current_led

    try:
        while True:
            # Check IR sensor state
            sensor_state = gpio.input(IR_sensor)

            if sensor_state == 0:
                if current_led != gpio.HIGH:
 gpio.output(led, gpio.HIGH)
                    current_led = gpio.HIGH
            else:
                if current_led != gpio.LOW:
                    gpio.output(led, gpio.LOW)
                    current_led = gpio.LOW

            # Check signals from 8051 for password verification
            if gpio.input(signal_pin_wrong) == gpio.HIGH:
                # Wrong password signal from 8051 detected
                gpio.output(led_pin_wrong, gpio.HIGH)
                gpio.output(led_pin_correct, gpio.LOW)
                gpio.output(buzzer_pin, gpio.HIGH)

            elif gpio.input(signal_pin_correct) == gpio.HIGH:
 # Correct password signal from 8051 detected
                gpio.output(led_pin_correct, gpio.HIGH)
                gpio.output(led_pin_wrong, gpio.LOW)
                gpio.output(buzzer_pin, gpio.LOW)

                # Operate the motor
                set_servo_angle(92.5)
                time.sleep(3)
                set_servo_angle(2.5)

            # If no signals, turn off LEDs and buzzer
            else:
                gpio.output(led_pin_wrong, gpio.LOW)
                gpio.output(led_pin_correct, gpio.LOW)
                gpio.output(buzzer_pin, gpio.LOW)

            time.sleep(0.1)  # Add a small delay to avoid excessive looping
 except KeyboardInterrupt:
        pass
    finally:
        servo.stop()
        gpio.cleanup()

if __name__ == '__main__':
    main()
Leave a Comment