-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonCode.py
More file actions
57 lines (43 loc) · 1.07 KB
/
PythonCode.py
File metadata and controls
57 lines (43 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import RPi.GPIO as GPIO
import time
# 핀 설정
in1 = 17
in2 = 27
ena = 22
# GPIO 핀 모드 설정
GPIO.setmode(GPIO.BCM)
GPIO.setup(in1, GPIO.OUT)
GPIO.setup(in2, GPIO.OUT)
GPIO.setup(ena, GPIO.OUT)
# PWM 설정
pwm = GPIO.PWM(ena, 1000) # PWM 주파수 설정
pwm.start(25) # 초기 듀티 사이클 설정 (25%)
def motor_forward():
GPIO.output(in1, GPIO.HIGH)
GPIO.output(in2, GPIO.LOW)
def motor_backward():
GPIO.output(in1, GPIO.LOW)
GPIO.output(in2, GPIO.HIGH)
def motor_stop():
GPIO.output(in1, GPIO.LOW)
GPIO.output(in2, GPIO.LOW)
try:
while True:
print("Motor Forward")
motor_forward()
time.sleep(5)
print("Motor Stop")
motor_stop()
time.sleep(2)
print("Motor Backward")
motor_backward()
time.sleep(5)
print("Motor Stop")
motor_stop()
time.sleep(2)
except KeyboardInterrupt:
pass
pwm.stop()
GPIO.cleanup()
servo.stop() # 서보 모터 정지
GPIO.cleanup() # GPIO 초기화하여 다음 모터 작동을 준비함