from machine import Pin,ADC,PWM
from time import sleep
buzzer=PWM(Pin(14)) #Configure a pin to output a PWM signal
frequency=500
#This example uses an LDR in a sensor circuit board
#which doesn't need a separate voltage divider
#or the pin configuration on the following line
Pin(20,Pin.IN,Pin.PULL_UP) #Configure an internal 50kohm resistor to 3.3V
LDR=ADC(Pin(28))
#Values to control pulseWidth range
rangeofpulsewidths = 6200
onedegree = rangeofpulsewidths / 180
twodegree = rangeofpulsewidths / 180
minpulsewidth = 1800
#Attach each sensor to an analog to digital converter input pin
potentiometer1=ADC(Pin(26))
potentiometer2=ADC(Pin(27))
#attach each servo to a PWM-capable pin
servo1=PWM(Pin(0))
servo2=PWM(Pin(1))
#set the pulse frequency for each servo
servo1.freq(50)
servo2.freq(50)
score = 0
i = 1
#test reading the pins with debug output
def reset():
'''Reset score variable and provide user feedback before a new game'''
global score
score=0
beep(2)
print('___________________________________________________________')
print('')
print("Preparing new game...")
print('')
print("Adjust the potentiometer to your desire level (1, 2, 3)")
print('___________________________________________________________')
print('')
for s in range(0,6):
print(f'Lock in level difficulty in {s}/5s')
print('')
sleep(1)
print('Game has started!!!')
print('')
print('Shiner Score')
print('______________________')
print('')
potentiometer1Value=potentiometer1.read_u16()
potentiometer2Value=potentiometer2.read_u16()
degrees1 = potentiometer1Value / 65536 * 180
degrees2 = potentiometer2Value / 65536 * 180
# Read Level Number
if degrees1 >= 120:
i = 3
elif degrees1 >= 60:
i = 2
else:
i = 1
print(f'Level: {i}')
def beep(numBeeps):
'''Turn the buzzer on and off a given number of times'''
print('')
print("Beep")
for i in range(numBeeps):
buzzer.duty_u16(1000)
buzzer.freq(1000)
sleep(0.2)
buzzer.duty_u16(0)
sleep(0.1)
def winningSound():
'''Play a nice winning sound'''
print("Winner!")
beep(3)
buzzer.duty_u16(5000)
#add a better winning sound here - eg loop through a range of frequencies in quick succession
reset()
while True:
# print(f'LDR: {LDR.read_u16()}')
# sleep(0.1)
potentiometer1Value=potentiometer1.read_u16()
potentiometer2Value=potentiometer2.read_u16()
degrees1 = potentiometer1Value / 65536 * 180
degrees2 = potentiometer2Value / 65536 * 180
# Confirm Level Difficulty and modify Score
if degrees1 >= 120:
i = 3
elif degrees1 >= 60:
i = 2
else:
i = 1
servo1Value=int(score/1010 * (8500+4000/65536))
servo2Value=int(score/1010 * (8500+4000/65536))
servo1.duty_u16(servo1Value)
servo2.duty_u16(servo2Value)
#print(f"Servo 1 pulse: {potentiometer1Value}, Degrees: {degrees1}, Servo 2 pulse: {potentiometer2Value}, Degrees: {degrees2}")
#PulseWidth# = (potentiometer#Value / 65536 * 180) * (6200 / 180) + 1800)
# = potentiometer#Value / 11796480 * 34.444 + 1800
# pulseWidth1 = score/1010 * (degrees1 * onedegree + minpulsewidth)
#pulseWidth2 = score/1010 * (degrees2 * twodegree + minpulsewidth)
# sleep(0.1)
#test each servo by looping through a range of pulse widths
# 3000/65536 corresponds roughly to a 1ms pulse
# 7000/65536 corresponds roughly to a 2ms pulse
#for pulseWidth in range(3000,7000):
# servo1.duty_u16(int(pulseWidth1))
# sleep(0.001)
#for pulseWidth in range(3000,7000):
# servo2.duty_u16(int(pulseWidth2))
# sleep(0.001)
#if frequency < 5000: #Loop through a few octaves
#
# buzzer.freq(frequency) #Set the frequency
#
# buzzer.duty_u16(2000) #Turn the buzzer on
# sleep(0.25)
#
# buzzer.duty_u16(0) #Turn the buzzer off
# sleep(0.5)
#
# frequency=frequency*2
#if frequency > 5000:
# frequency = 500
if LDR.read_u16()<5000: #Strong light detected - change this value to suit your room and LDR
score=score+(10*i)
buzzer.freq(score+100)
buzzer.duty_u16(score+1000)
print('')
print(f'Score: {score}')
if score>1000: #If the game has been won, play a sound and get ready for the next round
print('')
winningSound()
reset()
else: #Strong light not detected .. do nothing (turn the buzzer off)
buzzer.duty_u16(0)
sleep(0.1)
L1
L2
L3
Not in use (Maybe I could use this for start or stop the game)
0% ------->-------100%
Light exposure
Use for pre-game