Making a turning target (or similar) on a budget (<£20-ish)
Jul 21, 2022 17:57:53 GMT
Doyley83 and ifield like this
Post by twodoctors on Jul 21, 2022 17:57:53 GMT
I'm just going to copy and paste here for now. Musket69 Doyley83
Note that the LCD settings is for my 4 line, 20 characters LCD. Don't bother with the fancy 1.3" IPS screens with buttons. They are way too small for any practical use.
Main menu = main.py
Single target, random away time, 6 exposure - random6.py
Note that the LCD settings is for my 4 line, 20 characters LCD. Don't bother with the fancy 1.3" IPS screens with buttons. They are way too small for any practical use.
Main menu = main.py
# Adrian Kwa
# Kwa Targets System
# (C) 2022
from machine import Pin, I2C, PWM
from machine_i2c_lcd import I2cLcd
import utime
# LCD 2004 settings
i2c = I2C(0, scl=Pin(17), sda=Pin(16), freq=400000)
addr = i2c.scan()[0]
lcd = I2cLcd(i2c, addr, 4, 20)
# Button / Ground / Step / Direction
button_pin = Pin(18, Pin.IN, Pin.PULL_UP)
direction_pin = Pin(19, Pin.IN, Pin.PULL_UP)
step_pin = Pin(20, Pin.IN, Pin.PULL_UP)
previous_value = True
button_down = False
# Name the servo
servo_pin_ids = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# List of PWM instances
servos = []
# Looping - assigning a value to pwm using list servo_pin_ids
for p in servo_pin_ids:
pwm = PWM(Pin(p))
pwm.freq(50)
# Adding to servos list
servos.append(pwm)
# Start/Reset Button
#target_reset_button = Pin(14, Pin.IN, Pin.PULL_DOWN)
# Preset Programmes
program_lists = ("10 Targets - Single", "10 Targets - Single", "10 Targets - Single", "10 Targets - Pairs", "Single Target", "Single Target", "TP1 Match 1", "TP1 Match 2", "TP1 Match 3")
detail_lists = ("Random Exposures", "Random Exposures","Whack-a-Mole", "Two At A Time!", "6 Exposures", "10 Exposures", "12 Shots in 120 Secs", "1 Shot in 2 Secs", "2 Shots in 3 Secs")
detail1_lists = ("Random Timings", "Advanced Level", "Random Timings", "Random Timings", "Random Timings", "Random Timings", "1x Reload", "2 Strings of 6 Shots", "6 Shots in Total")
file_lists = ("RandomMulti.py", "RandomMulti-Hard.py", "RandomMulti-WAM.py", "RandomMulti-Pair.py", "Random6.py", "Random10.py", "TP1_1.py", "TP1_2.py", "TP1_3.py")
# 1st Programme Selected for Menu
selected = 0
# Menu Screen
def Menu():
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr(program_lists[selected])
lcd.move_to(0, 1)
lcd.putstr(detail_lists[selected])
lcd.move_to(0, 2)
lcd.putstr(detail1_lists[selected])
lcd.move_to(3, 3)
lcd.putstr("Press to Start...")
# Loading Screen
lcd.move_to(1, 1)
lcd.putstr("Kwa Targets System")
lcd.move_to(5, 2)
lcd.putstr("(c) 2022")
utime.sleep(2)
for p in servo_pin_ids:
servos[p].duty_u16(1300)
Menu()
# Rotary Encoder to select Programme
while True:
# Left Turn
if previous_value !=step_pin.value():
if step_pin.value() == False:
if direction_pin.value() == False:
saved = program_lists[selected]
selected = selected + 1
if selected > len(program_lists) - 1:
selected = 0
Menu()
else:
# Right Turn
saved = program_lists[selected]
selected = selected - 1
if selected == -1:
selected = len(program_lists) - 1
Menu()
previous_value = step_pin.value()
# Button Press
if button_pin.value() == False and not button_down:
filename = file_lists[selected]
print(filename)
exec(open(filename).read())
Menu()
button_down = True
if button_pin.value() == True and button_down:
button_down = False
Single target, random away time, 6 exposure - random6.py
import utime
import urandom
from machine import Pin, PWM, I2C, ADC, soft_reset
from time import sleep, sleep_ms
from machine_i2c_lcd import I2cLcd
# LCD 2004 settings
i2c = I2C(0, scl=Pin(17), sda=Pin(16), freq=400000)
addr = i2c.scan()[0]
lcd = I2cLcd(i2c, addr, 4, 20)
# Base settings for servo
servoA = PWM(Pin(10))
servoA.freq(50)
# Servo at Edge = 4500
# Servo at Face = 1200
# command = servoName.duty_u16(face or edge)
###Bank Servos###
# Name the servo
servo_pin_ids = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# List of PWM instances
servos = []
# Looping - assigning a value to pwm using list servo_pin_ids
for p in servo_pin_ids:
pwm = PWM(Pin(p))
pwm.freq(50)
# Adding to servos list
servos.append(pwm)
# Hide bank
for p in servo_pin_ids:
servos[p].duty_u16(4700)
# Start Button
start = Pin(14, Pin.IN, Pin.PULL_DOWN)
# Stop Button
stop = Pin(15, Pin.IN, Pin.PULL_DOWN)
# Buzzer
buzz = Pin(21, Pin.OUT)
# Introduction
def Intro():
lcd.clear()
servoA.duty_u16(1200)
# 1st Line
lcd.move_to(0, 0)
lcd.putstr("6 Exposures")
# 2nd Line
lcd.move_to(1, 1)
lcd.putstr("Random Delay")
# 3rd Line
lcd.move_to(2, 2)
lcd.putstr("Between 5-15 Sec")
# "Press start"
lcd.move_to(3, 3)
lcd.putstr("Press Start...")
# Short Buzz
def Buzz():
buzz.on()
sleep(0.2)
buzz.off()
# Normal Cycle
def Run():
#Cycle (0 to ntimes)
for x in range (0,6):
# 5 to 15 seconds Edge Delay
utime.sleep(urandom.uniform(5, 15))
# Face
servoA.duty_u16(1200)
lcd.clear()
lcd.move_to(8,1)
lcd.putstr("Face")
# Face time (seconds)
utime.sleep(3)
# Edge
servoA.duty_u16(4500)
lcd.clear()
lcd.move_to(8,1)
lcd.putstr("Edge")
# End
utime.sleep(1)
lcd.clear()
lcd.move_to(6,1)
lcd.putstr("Finished")
Buzz()
# End in Face after (delay)
utime.sleep(3)
servoA.duty_u16(1200)
# Interrupt End
def Interrupt(Pin):
lcd.clear()
lcd.move_to(3,1)
lcd.putstr("Stopped by User")
# End in Face after (delay)
utime.sleep(2)
servoA.duty_u16(1200)
machine.reset()
# Interrupt Function
stop.irq(trigger=Pin.IRQ_RISING, handler=Interrupt)
Intro()
while True:
if start.value() == 1:
lcd.clear()
lcd.move_to(8, 1)
lcd.putstr("Go!")
Buzz()
sleep(1)
servoA.duty_u16(4500)
Run()
break
elif stop.value() == 1:
break #breaks back into the main.py