# Random walk on a 8x8 NeoPixel matrix
# https://wokwi.com/projects/405827644049669121

from machine import Pin
from neopixel import NeoPixel

from time import sleep
import random

# pin = Pin(13, Pin.OUT) # GPIO13
pin = Pin(23, Pin.OUT) # GPIO23
np = NeoPixel(pin, 64) # NeoPixel 8x8 matrix

matrix = [[0 for _ in range(8)] for _ in range(8)]
x,y = 4,4

def step():
    global x, y
    global direction
    if direction == 'up':
        x = max(0, x - 1)
    elif direction == 'down':
        x = min(7, x + 1)
    elif direction == 'left':
        y = max(0, y - 1)
    elif direction == 'right':
        y = min(7, y + 1)
    for i in range(8):
        for j in range(8):
            matrix[i][j] = max(0, matrix[i][j] - 1)  # Fade out all positions
    matrix[x][y] = 10  # Mark the new position


def paint():
    for i in range(8):
        for j in range(8):
            value = matrix[i][j]
            np[i * 8 + j] = (0, value, 0)
    np.write() # Update the LED strip


def walk(steps):
    for _ in range(steps):
        step()
        paint()
        sleep(0.1)

walk(256)
