Trying to get a Snake game running on a led matrix

Hello! For a school project, me and another student are working on a LED-matrix (12x12), made of WS2812b addressable leds. Somehow we are not getting the program to work properly. When we try to move the snake vertically it's all fine, but when the snake has to go horizontally, the leds will jump to various "lanes".

video

As seen in the video, the snake (1 pixel) moves perfectly up and down, but left and right gives problems.

I'll hope you guys have some tips and tricks!

Do you think that it might be an idea to post your code and details if you hardware and wiring ?

I attached the code below

#include <Snake.h>
#include "FastLED.h"

#define NUM_LEDS 144
#define LEDS_PER_ROW 12
#define DATA_PIN 6

Snake snakeGame(12,12,10);      //grootte van het veld             
CRGB leds[NUM_LEDS];

void setup() {
    snakeGame.setBodyColor(255,0,255);//kleur van lichaam slang
    snakeGame.setFoodColor(0,60,125);//kleur van voer
    snakeGame.setHeadColor(225,20,60);//kleur van hoofd slang

    delay(2000);
    FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
    Serial.begin(9600);
}

byte setPixel(byte x, byte y, byte r, byte g, byte b)
{
    byte ledID = NUM_LEDS - (y * LEDS_PER_ROW  ) - x - 1;
 
    leds[ledID].setRGB(r,g,b);
    return ledID;
}
void changeRGBtoGBR()
{
    for(int whiteLed = 0; whiteLed < NUM_LEDS; whiteLed++)
    {
      leds[whiteLed].setRGB(leds[whiteLed].g , leds[whiteLed].b, leds[whiteLed].r);
    }
}

void clearScreen()
{
    for(int whiteLed = 0; whiteLed < NUM_LEDS; whiteLed++)
    {
      leds[whiteLed].setRGB( 3, 3, 3);
    }
}

void loop() 
{     
    Snake::pixel* snakeLimbs=snakeGame.getSnakeLimbs();
    Snake::pixel* snakeFood = snakeGame.getFoodPositions();
    clearScreen();
    setPixel(snakeFood[0].posX,11-snakeFood[0].posY,snakeFood[0].pixelColor.r,snakeFood[0].pixelColor.g,snakeFood[0].pixelColor.b); // voedsel weergeven
    for(int i=0; i<snakeGame.getSnakeLenght(); i++)
    {
    
        setPixel(snakeLimbs[i].posX,11-snakeLimbs[i].posY,snakeLimbs[i].pixelColor.r,snakeLimbs[i].pixelColor.g,snakeLimbs[i].pixelColor.b);  // grootte scherm slang weergeven
    }
    FastLED.show();
    snakeGame.tick();
    if(snakeGame.wasGameReset())
    {
        for(int i=0; i<30; i++)
        {
            changeRGBtoGBR();
            FastLED.show();
            delay(40);
        }
    }
    else
        delay(30);
}

byte incomingByte=0;
void serialEvent() {
    while (Serial.available()) 
    {
        incomingByte = Serial.read();
        Serial.write(incomingByte);
    }
    switch(incomingByte)
    {
    case 'a':
        snakeGame.goLeft(); //slang naar links
        break;
    case 'd':
        snakeGame.goRight(); //slang naar rechts
        break;
    case 's':
        snakeGame.goDown(); //slang naar beneden
        break;
    case 'w':
        snakeGame.goUp(); //slang naar boven
        break;
    }
}

Our hardware is correct, when we load a code to run animations it runs perfectly.

and a link to this library#include <Snake.h>so we can see what is going wrong.

chrisdijkstra:
Our hardware is correct, when we load a code to run animations it runs perfectly.

It would still be useful if you showed how it was connected. e.g. is the sketch coded in a way that matches the way you've arranged the pixels into a matrix?

Deva_Rishi:
and a link to this library#include <Snake.h>so we can see what is going wrong.

I found the code thats in the library here.

GypsumFantastic:
It would still be useful if you showed how it was connected. e.g. is the sketch coded in a way that matches the way you've arranged the pixels into a matrix?

I made this simple fritzing diagram, hope it helps!

Snake-Game-Library-for-ArduinoHey guys, it's not just the obvious that the final led on the row (say the most right one) should be connected to the first of the next (The most left one) rather then having the ledstrip snake over the matrix L->R then R-L then L->R ?? the Library seems to assume that the rows are all L->R

What I'm trying to get at is this. I think you might have your pixels wired in boustrophedon type arrangement (as below). But the setpixel function is coded for a straightforward raster type arrangement.

IMG_0481 (1).jpg

IMG_0481 (1).jpg

Hey guys, I have added a picture of our wiring as it is now, can you please tell us how we should wire it? (With a sketch of something?) Thankyou in advance!

Wiring

I think it might be easier to adapt the setpixel() function, but if you wanted to re-wire then you need to have them something like the rough sketch below.

IMG_0483.jpg

IMG_0483.jpg

@GypsumFanatic - Thank you!

I'm not even out of bed and I already learned my word for the day

boustrophedon

Looking forward to using it.

BTW if you are stuck with a fixed panel that has boustrophedon wiring or worse (!), real programmers eschew mucking with the hardware and implement a correction layer with a map that un-snakes the addressing.

It has to be aprt of the tookit if you do much down on the ground here.

a7

Later, awake.

From a time when I had a boustrophedon 4x4 neopixel matrix.

the map:

unsigned char map[16] = {0, 1, 2, 3, 7, 6, 5, 4, 8, 9, 10, 11, 15, 14, 13, 12,};

You can see the first row goes in order, the next four are flipped and so forth.

Then find things like

strip.setPixelColor(X, 0xcc00cc);

and change them to "look up" the right physical pixel, vis:

strip.setPixelColor(map[X], 0xcc00cc);

Maps like this can also be used to rotate or flip the display whilst allowing us to refer to one upright rationally numbered abstract. Extension to larger arrays or stranger arrangements should be straight ahead.

alto777