from machine import Pin, Timer
import utime
button_lft = Pin(27, Pin.IN, Pin.PULL_DOWN)
button_rgt = Pin(28, Pin.IN, Pin.PULL_DOWN)
#led definition
led_onboard = Pin(25, Pin.OUT)
led_1 = Pin(16, Pin.OUT)
led_2 = Pin(17, Pin.OUT)
led_3 = Pin(18, Pin.OUT)
led_4 = Pin(19, Pin.OUT)
led_5 = Pin(20, Pin.OUT)
led_6 = Pin(21, Pin.OUT)
led_7 = Pin(22, Pin.OUT)
led_8 = Pin(26, Pin.OUT)
leds=[led_1,led_2,led_3,led_4,led_5,led_6,led_7,led_8]
state = 0
button_lft_flag = False
button_rgt_flag = False
last_button_state_lft = 0
last_button_state_rgt = 0
last_debounce_lft = 0
last_debounce_rgt = 0
bin_lft = 0b00000000
bin_rgt = 0b00000000
score_rgt = 0
score_lft = 0
#timer
timer_leds = Timer()
def reset_game():
global state, button_lft_flag, button_rgt_flag, bin_lft, bin_rgt
state = 0
button_lft_flag = False
button_rgt_flag = False
bin_lft = 0
bin_rgt = 0
print(state)
def debounce_filter_rising(pin):
global last_button_state_rgt, last_debounce_rgt, last_button_state_lft, last_debounce_lft, button_rgt_flag, button_lft_flag
print(pin)
debounce_time = 37 #ms
if pin == button_rgt:
time = utime.ticks_ms()
if utime.ticks_diff(time, last_debounce_rgt) > debounce_time:
last_debounce_rgt = time
button_rgt_flag = not button_rgt_flag
print(button_rgt_flag)
if pin == button_lft:
time = utime.ticks_ms()
if utime.ticks_diff(time, last_debounce_lft) > debounce_time:
last_debounce_lft = time
button_lft_flag = not button_lft_flag
"""def debounce_filter_falling(pin):
global last_button_state_rgt, last_debounce_rgt, last_button_state_lft, last_debounce_lft, button_rgt_flag, button_lft_flag
#print(pin)
debounce_time = 37 #ms
if pin == button_rgt:
time = utime.ticks_ms()
if utime.ticks_diff(time, last_debounce_rgt) > debounce_time:
last_debounce_rgt = time
if pin == button_lft:
time = utime.ticks_ms()
if utime.ticks_diff(time, last_debounce_lft) > debounce_time:
last_debounce_lft = time"""
# This function basically transforms any integer which is between 0-255 to binary type.
# later turns this binary to a list that shows bit by bit in a list to us.
def lister(binary):
lst = []
for j in range (len(bin(binary)), 10):
lst.append(0)
for i in bin(binary)[2:]:
lst.append(int(i))
return(lst)
#This function helps us to adjust the values of the leds one by one according to the binary list that we previously got in the lister function.
#Most importantly it makes our job less complicated because we do not have to use this particular function writing over and over again in other functions.
def bin_to_led(binary):
lst_bin = lister(binary)
for i in range(0,8):
leds[i].value(lst_bin[i])
def shooting(t):
global bin_rgt, bin_lft, state
bin_combined = bin_rgt | bin_lft
bin_to_led(bin_combined)
if state == 1:
bin_rgt <<= 1
elif state == 3:
bin_lft >>= 1
elif state == 2:
bin_rgt <<= 1
bin_lft >>= 1
elif state == -1:
led_onboard.value(0)
timer_leds.deinit()
def state_0():
global button_lft_flag, button_rgt_flag, score_lft, score_rgt, state
if button_lft_flag and not button_rgt_flag:
bin_lft = 0b10000000
state_3()
elif not button_lft_flag and button_rgt_flag:
bin_rgt = 0b00000001
state_1()
else:
state_0()
def state_1():
global button_rgt_flag, button_lft_flag, bin_rgt, bin_lft, score_rgt, score_lft, state
state = 1
print(state)
while True:
if bin_rgt == 0b100000000:
score_rgt += 1
print("Left player is shot! Right player is now at", score_rgt, "points.")
reset_game()
break
elif not button_rgt_flag and not button_lft_flag:
score_lft += 2
print("Right player has double fired! Left player is now at", score_lft, "points.")
reset_game()
break
elif button_rgt_flag and button_lft_flag:
bin_lft = 0b10000000
state_2()
break
def state_3():
global button_rgt_flag, button_lft_flag, bin_lft, bin_rgt, score_rgt, score_lft, state
state = 3
print(state)
while True:
if bin_lft == 0b00000000:
score_lft += 1
print("Right player is shot! Left player is now at", score_lft, "points.")
reset_game()
break
elif not button_lft_flag and not button_rgt_flag:
score_rgt += 2
print("Left player has double fired! Right player is now at", score_rgt, "points.")
reset_game()
break
elif button_lft_flag and button_rgt_flag:
bin_rgt = 1
print("bin_rgt:", bin_rgt)
state_2()
break
def state_2():
global bin_lft, bin_rgt, button_lft_flag, button_rgt_flag, score_rgt, score_lft, state
state = 2
print(state)
print(bin(bin_lft), bin(bin_rgt))
while True:
if 1/2 <= bin_rgt/bin_lft <= 2:
print("Shots have clashed! No points for nobody.")
reset_game()
break
if not button_lft_flag:
score_rgt += 2
print("Left player has double fired! Right player is now at", score_rgt, "points.")
reset_game()
break
if not button_rgt_flag:
score_lft += 2
print("Right player has double fired! Left player is now at", score_lft, "points.")
reset_game()
break
print(button_lft_flag, button_rgt_flag)
#initilization
led_onboard.value(1)
timer_leds.init(period = 500, mode = Timer.PERIODIC, callback = shooting)
button_rgt.irq(trigger = Pin.IRQ_RISING, handler = debounce_filter_rising)
button_lft.irq(trigger = Pin.IRQ_RISING, handler = debounce_filter_rising)
"""button_rgt.irq(trigger = Pin.IRQ_FALLING, handler = debounce_filter_falling)
button_lft.irq(trigger = Pin.IRQ_FALLING, handler = debounce_filter_falling)"""
while True:
if score_lft >= 5:
print("Left player wins!!!")
break
elif score_rgt >= 5:
print("Right player wins!!!")
break
elif button_lft_flag and not button_rgt_flag:
bin_lft = 0b10000000
state_3()
elif not button_lft_flag and button_rgt_flag:
bin_rgt = 0b00000001
state_1()
reset_game()
state = -1