-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjoystick.py
67 lines (43 loc) · 1.09 KB
/
joystick.py
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
58
59
60
61
62
63
64
65
66
67
import time
from machine import ADC, Pin
HIGH = 35000
LOW = 30000
def is_still(val):
return val > LOW and val < HIGH
def increase(val):
return val > HIGH
def decrease(val):
return val < LOW
joystick_y = ADC(Pin(26))
joystick_x = ADC(Pin(27))
STOP = 1
STRAIGHT_FORWARD = 2
RIGHT_FORWARD = 3
LEFT_FORWARD = 4
RIGHT_BACKWARD = 5
LEFT_BACKWARD = 6
STRAIGHT_BACKWARD = 7
while True:
x_val = joystick_x.read_u16()
y_val = joystick_y.read_u16()
x_still = is_still(x_val)
y_still = is_still(y_val)
x_increase = increase(x_val)
y_increase = increase(y_val)
x_decrease = decrease(x_val)
y_decrease = decrease(y_val)
if x_still and y_still:
print(STOP)
if y_increase and x_still:
print(STRAIGHT_FORWARD)
if y_decrease and x_still:
print(STRAIGHT_BACKWARD)
if y_increase and x_increase:
print(LEFT_FORWARD)
if y_increase and x_decrease:
print(RIGHT_FORWARD)
if y_decrease and x_increase:
print(LEFT_BACKWARD)
if y_decrease and x_decrease:
print(RIGHT_BACKWARD)
time.sleep(0.1)