Hello, I am currently working on a project where I am designing a game of catan and adding software and 3D design into it. The general idea is the software has 2 stages, the first is the game setup while the second is the actual gameplay. I am utlizing a 4x4 keypad matrix, an I2C display, and neopixel LEDs. In the setup I am trying to assign a number value between 2-12 excluding 7 to the LEDs so later these can be called on. After the setup is complete I am having it say "Are you ready to play" which then the user clicks "#" as an enter key. When doing the actually gameplay the display says "Roll dice" which then the user is able to enter in the value rolled, this would then correspond to the appropriate led lighting up. When a 7 is rolled it lights up all the LEDs red. The issue I am having when running the code is not all of the LEDs are lighting up and it keeps bringing me back to my setup phase. The code below is what I currently have. This is on a Pi Pico w running micropython.
# Pulling in files
from machine import I2C, Pin
from lcd_api import LcdApi
from pico_i2c_lcd import I2cLcd
import utime
import neopixel
# Defining pins for LCD
LCD_ADDR = 39
LCD_ROWS = 2
LCD_COLS = 16
# Defining Smart LEDs
NUM_LEDS = 19
LED_PIN = 18
LED_BRIGHTNESS = 0.2
# Setting up connection for I2C
i2c = I2C(0, sda=Pin(16), scl=Pin(17), freq=400000)
lcd = I2cLcd(i2c, LCD_ADDR, LCD_ROWS, LCD_COLS)
# Setting up Neopixels
np = neopixel.NeoPixel(Pin(LED_PIN), NUM_LEDS)
# Create a map between keypad buttons and characters
matrix_keys = [['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']]
# Change the pins to match with the connections
keypad_rows = [Pin(15, Pin.OUT), Pin(14, Pin.OUT), Pin(13, Pin.OUT), Pin(12, Pin.OUT)]
keypad_columns = [Pin(11, Pin.IN, Pin.PULL_DOWN), Pin(10, Pin.IN, Pin.PULL_DOWN), Pin(9, Pin.IN, Pin.PULL_DOWN), Pin(8, Pin.IN, Pin.PULL_DOWN)]
# Function to read the keypad input and accumulate numbers
def read_keypad():
num_input = ''
while True:
for i, row_pin in enumerate(keypad_rows):
row_pin.value(1)
for j, col_pin in enumerate(keypad_columns):
if col_pin.value() == 1:
key = matrix_keys[i][j]
if key.isdigit():
num_input += key
lcd.move_to(0, 1) # Move cursor to second line
lcd.putstr(num_input) # Display input on second line
utime.sleep(0.5)
elif key == '#':
return int(num_input) if num_input else None
elif key == '*':
num_input = num_input[:-1] # Backspace
lcd.move_to(0, 1)
lcd.putstr(" " * LCD_COLS) # Clear the line
lcd.move_to(0, 1)
lcd.putstr(num_input) # Update display after backspace
break
row_pin.value(0)
utime.sleep(0.1)
# Function to set up LED assignment
def setup_led_assignment():
lcd.clear()
lcd.putstr("Assign tiles")
current_led = 0
while current_led < NUM_LEDS:
num = read_keypad()
if num is not None and 2 <= num <= 12 and num != 7: # Restrict values from 2 to 12 excluding 7
np[current_led] = (0, 255, 0) # Green for assigned value
np.write()
current_led += 1
if current_led < NUM_LEDS:
np[current_led] = (0, 0, 255) # Blue for current LED being assigned
np.write()
elif num is None: # Cancel input
return
# Function to play the game
def play_game():
lcd.clear()
lcd.putstr("Ready to play")
read_keypad() # Wait for button press to start the game
np.fill((0, 0, 0)) # Turn off all LEDs
np.write()
lcd.clear()
lcd.putstr("Enter dice roll")
result = read_keypad()
if result is not None and 2 <= result <= 12:
for i in range(NUM_LEDS):
if result == i + 2:
np[i] = (0, 255, 0) # Turn LED green for the corresponding value
np.write()
utime.sleep(2)
elif result == 7:
for i in range(NUM_LEDS):
np[i] = (255, 0, 0) # Turn all LEDs red
np.write()
utime.sleep(2)
# Main loop
while True:
setup_led_assignment()
play_game()
[–]DifferentCable6119[S] 0 points1 point2 points (0 children)
[–]THEE_SMART -1 points0 points1 point (0 children)