from machine import Pin, ADC, PWM
from time import sleep, ticks_ms, ticks_diff
from servo import Servo
from joystick import Joystick
last_time_pressed = 0
button_action = False
button_pressed = False
joystick = Joystick(14, 12, 'interrupt') # Setting the joystick pins and mode
joystick_button = Pin(27, Pin.IN, Pin.PULL_UP) # The joystick button
slide = Pin(18, Pin.IN) # Slide switch pin
tilt1 = Servo(0)
pan1 = Servo(4)
tilt2 = Servo(15)
pan2 = Servo(2)
servos = [tilt1, pan1, tilt2, pan2] # Servos pins
for i in servos: # Start all servos with 90 degree
i.goto(90)
led_pins = [5, 17] # Leds pins
leds = [Pin(i, Pin.OUT) for i in led_pins]
def button_handler(pin):
global button_action
last_time_pressed = ticks_ms()
button_action = True
def speed_level(): # A function to set the gain and check the button debounce
global last_time_pressed, button_action
current_time = ticks_ms()
if ticks_diff(current_time, last_time_pressed) > 50:
last_time_pressed = current_time
if joystick_button.value() == 0:
button_pressed = True
else:
button_pressed = False
button_action = False
if button_pressed:
level = input('Enter the desired level --> range{1 - 10}')
try:
level = int(level)
except:
print('Invaild input, try again')
else:
if 1 <= level <= 10:
Servo.speed_level = level
else:
print('Invaild input, try again')
button_pressed = False
def which_pair(): # Checking which camera is beaing used
led_select = bool(slide.value())
if led_select == True:
leds[0].value(1)
leds[1].value(0)
return servos[0], servos[1]
else:
leds[0].value(0)
leds[1].value(1)
return servos[2], servos[3]
# Setting the joystick button to interrupt
joystick_button.irq(trigger=Pin.IRQ_FALLING, handler=button_handler)
while True: # Main loop
if button_action: # For button debounce
speed_level()
if joystick.mode == 'polling': # To read the joystick position in polling mode
joystick.read(None)
tilt, pan = which_pair()
state = joystick.state
if state == 'M':
print('Current joystick state is --> ',state)
elif state == 'R':
pan.goto(180)
print('Current joystick state is --> ',state)
elif state == 'L':
pan.goto(0)
print('Current joystick state is --> ',state)
elif state == 'U':
tilt.goto(0)
print('Current joystick state is --> ',state)
elif state == 'D':
tilt.goto(180)
print('Current joystick state is --> ',state)
sleep(.1)
passLoading
esp32-devkit-c-v4
esp32-devkit-c-v4
CAMERA 1
CAMERA 2