Untitled

 avatar
unknown
plain_text
5 months ago
1.4 kB
5
Indexable
import RPi.GPIO as GPIO
import time

# Pin configurations
LED_PIN = 7
SERVO_PIN = 8

# Setup GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(LED_PIN, GPIO.OUT)

# Initialize PWM for Servo
GPIO.setup(SERVO_PIN, GPIO.OUT)
servo = GPIO.PWM(SERVO_PIN, 50)  # Set frequency to 50 Hz
servo.start(0)  # Initialize servo position

try:
    while True:
        command = input("Enter command (lightOn, lightOff, servoOpen, servoClose): ")

        if command == "lightOn":
            GPIO.output(LED_PIN, GPIO.HIGH)  # Turn LED on
            print("LED is ON")
        elif command == "lightOff":
            GPIO.output(LED_PIN, GPIO.LOW)  # Turn LED off
            print("LED is OFF")
        elif command == "servoOpen":
            servo.ChangeDutyCycle(2)  # Adjust for your servo
            time.sleep(1)  # Allow time for movement
            servo.ChangeDutyCycle(0)  # Stop sending signal
            print("Servo is OPEN")
        elif command == "servoClose":
            servo.ChangeDutyCycle(12)  # Adjust for your servo
            time.sleep(1)  # Allow time for movement
            servo.ChangeDutyCycle(0)  # Stop sending signal
            print("Servo is CLOSED")
        else:
            print("Invalid command")

except KeyboardInterrupt:
    pass
finally:
    GPIO.cleanup()  # Clean up GPIO on exit
    servo.stop()  # Stop the servo PWM
Editor is loading...
Leave a Comment