from machine import Pin
from utime import sleep
button_1 = Pin('GP14', Pin.IN, Pin.PULL_DOWN)
button_2 = Pin('GP15', Pin.IN, Pin.PULL_DOWN)
button_3 = Pin('GP13', Pin.IN, Pin.PULL_DOWN)
led_1 = Pin('GP17', Pin.OUT)
led_2 = Pin('GP16', Pin.OUT)
led_3 = Pin('GP18', Pin.OUT)
def handler_1(x):
led_1.high()
return
def handler_2(x):
led_2.high()
return
def handler_3(x):
led_3.high()
return
button_1.irq(handler_1, Pin.IRQ_RISING)
button_2.irq(handler_2,Pin.IRQ_RISING)
button_3.irq(handler_3,Pin.IRQ_RISING)
led_1.low()
led_2.low()
led_3.low()
try:
play_again = True
while play_again==True:
sleep(1)
button_1_state = button_1.value()
button_2_state = button_2.value()
button_3_state = button_3.value()
d = 4*button_3_state + 2*button_1_state + 1*button_2_state
print('The state of button 1 is: ', button_1_state)
print('The state of button 2 is: ', button_2_state)
print('The state of button 3 is: ', button_3_state)
print('3-bit binary number: ')
print('button 1: ', bin(button_1_state))
print('button 2: ', bin(button_2_state))
print('button 3: ', bin(button_3_state))
print('Integer Equivalent: ', d)
print('\n')
play_again_str = input('Play again? Y or N: ')
if play_again_str == 'N'or play_again_str == 'n':
play_again = False
print('Thanks for playing! Goodbye!')
led_1.low()
led_2.low()
led_3.low()
except KeyboardInterrupt:
print('\n')
print('You sunk my battle ship!')
finally:
led_1.low()
led_2.low()
led_3.low()