# here are the imports that are needed for the program
import time
import machine
from machine import Pin,PWM
from pico_i2c_lcd import I2cLcd
import random
import sys
# initialise LEDs
l_1 = Pin(0,Pin.OUT)
l_2 = Pin(1,Pin.OUT)
l_3 = Pin(2,Pin.OUT)
l_4 = Pin(3,Pin.OUT)
l_5 = Pin(15,Pin.OUT)
# validation LED for positive feedback
# initialise Button
b_1 = Pin(4,Pin.IN,Pin.PULL_UP)
b_2 = Pin(5,Pin.IN,Pin.PULL_UP)
b_3 = Pin(6,Pin.IN,Pin.PULL_UP)
b_4 = Pin(7,Pin.IN,Pin.PULL_UP)
# Define I2C pins (adjust the pins if needed)
sda_pin = machine.Pin(10)
scl_pin = machine.Pin(11)
# Initialize I2C
i2c = machine.I2C(1, sda=sda_pin, scl=scl_pin, freq=400000)
# I2C LCD module address (adjust if needed)
lcd_address = 0x27
# Initialize LCD
lcd = I2cLcd(i2c, lcd_address, 2, 16)
#initilise the Buzzer
buzzer = PWM(Pin(16,Pin.OUT))
# Length of game and current level
length = 50
level = 1
# Boolean to check trigger end of the game and if input recieved was correct
Passed = True
got = False
# List comprehension generates list of random numbers
# and a variable to store the users attempt
sequence = [random.randint(1,4) for i in range(length)]
print(sequence)
attempt = 0
user_input = []
lcd.putstr('press any button to play')
tones = {
"B0": 31,
"C1": 33,
"CS1": 35,
"D1": 37,
"DS1": 39,
"E1": 41,
"F1": 44,
"FS1": 46,
"G1": 49,
"GS1": 52,
"A1": 55,
"AS1": 58,
"B1": 62,
"C2": 65,
"CS2": 69,
"D2": 73,
"DS2": 78,
"E2": 82,
"F2": 87,
"FS2": 93,
"G2": 98,
"GS2": 104,
"A2": 110,
"AS2": 117,
"B2": 123,
"C3": 131,
"CS3": 139,
"D3": 147,
"DS3": 156,
"E3": 165,
"F3": 175,
"FS3": 185,
"G3": 196,
"GS3": 208,
"A3": 220,
"AS3": 233,
"B3": 247,
"C4": 262,
"CS4": 277,
"D4": 294,
"DS4": 311,
"E4": 330,
"F4": 349,
"FS4": 370,
"G4": 392,
"GS4": 415,
"A4": 440,
"AS4": 466,
"B4": 494,
"C5": 523,
"CS5": 554,
"D5": 587,
"DS5": 622,
"E5": 659,
"F5": 698,
"FS5": 740,
"G5": 784,
"GS5": 831,
"A5": 880,
"AS5": 932,
"B5": 988,
"C6": 1047,
"CS6": 1109,
"D6": 1175,
"DS6": 1245,
"E6": 1319,
"F6": 1397,
"FS6": 1480,
"G6": 1568,
"GS6": 1661,
"A6": 1760,
"AS6": 1865,
"B6": 1976,
"C7": 2093,
"CS7": 2217,
"D7": 2349,
"DS7": 2489,
"E7": 2637,
"F7": 2794,
"FS7": 2960,
"G7": 3136,
"GS7": 3322,
"A7": 3520,
"AS7": 3729,
"B7": 3951,
"C8": 4186,
"CS8": 4435,
"D8": 4699,
"DS8": 4978
}
song = ["E5","G5","A5","E5","G5","B5","A5","E5","G5","A5","G5","E5"]
while True: # keeps running loop until button pressed
for i in song:
buzzer.freq(tones[i])
buzzer.duty_u16(1000)
time.sleep(0.1)
buzzer.duty_u16(0)
if b_1.value() == 0:
break
elif b_2.value() == 0:
break
elif b_3.value() == 0:
break
elif b_4.value() == 0:
break
else:
continue
time.sleep(1)
lcd.clear()
#Main loop
while Passed:
lcd.clear()
lcd.putstr("level "+str(level))
for i in range(level): # for loop to flash LED
flash = sequence[i]
if flash == 1: # flash LED 1
l_1.toggle()
time.sleep(0.5) # time delay of allow user to process the light
l_1.off()
if flash == 2: # flash LED 2
l_2.toggle()
time.sleep(0.5)
l_2.off()
if flash == 3: # flash LED 3
l_3.toggle()
time.sleep(0.5)
l_3.off()
if flash == 4: # flash LED 4
l_4.toggle()
time.sleep(0.5)
l_4.off()
count = 0
while count != level: # keeps running loop until button pressed
if b_1.value() == 0:
user_input.append(1) # checks if button 1 is pressed
count +=1
elif b_2.value() == 0:
user_input.append(2)# checks if button 2 is pressed
count +=1
elif b_3.value() == 0:
user_input.append(3)# checks if button 3 is pressed
count +=1
elif b_4.value() == 0:
user_input.append(4)# checks if button 4 is pressed
count +=1
else:
continue
time.sleep(0.35)
print(user_input)
if user_input == sequence[:level]: # checks if all the buttons were pressed correctly
Passed = True
l_5.toggle()
time.sleep(0.5)
l_5.off()
lcd.putstr(' Success!')
buzzer.freq(900)
buzzer.duty_u16(1000)
time.sleep(0.8)
buzzer.duty_u16(0)
else: # changes boolean controlling the game loop if the lists are the same
lcd.clear()
lcd.putstr("Game over")
l_1.toggle()
l_2.toggle()
l_3.toggle()
l_4.toggle()
time.sleep(1)
l_1.off()
l_2.off()
l_3.off()
l_4.off()
Passed = False
lcd.clear()
buzzer.freq(300)
buzzer.duty_u16(1000)
time.sleep(0.8)
buzzer.duty_u16(0)
user_input.clear()
time.sleep(1)
level += 1
sys.exit()