I have the guru meditation trying to make the Snake dead

the code:

#include <U8g2lib.h>
#include <Wire.h>

int dlugosc = 5;

int leftpress = 0; // bialy kabel - D18 - lewy przycisk (go left)
int rightpress = 0; // niebieski kabel - D34 - środkowy przycisk (go right)
int uppress = 0; // czerwony kabel - D27 - górny przycisk (go up)
int downpress = 0; // czarny kabel - D15 - prawy przycisk (go down)

int rotation = 0;

int direction = 0;  //0 = lewo, 1 prawo, 2 góra, 3 dół;

int bonk = 0; // zmienna czy snek dedł

int clear = 0;  // zmienna czy ekran zostal wyczyszczony po ded

unsigned long aktualnyCzas = 0;
unsigned long zapamietanyCzas = 0;
unsigned long roznicaCzasu = 0;

int x = 64;
int y = 32;

struct Point {
  int x, y;
};

const int MAX_SNAKE_LENGTH = 100; // ograniczenie długości węża do bezpiecznej wartości
Point snake[MAX_SNAKE_LENGTH]; // tablica pozycji pixeli

U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE, /* clock=*/ 22, /* data=*/ 21);

void setup() {
  u8g2.begin();

  pinMode(18, INPUT); // lewy guzik
  pinMode(27, INPUT); // góra guzik
  pinMode(15, INPUT); // dół guzik
  pinMode(34, INPUT); // prawy guzik

  pinMode(32, INPUT); //kryntuo

  Serial.begin(9600);

  // Inicjalizacja węża na środku ekranu
  for (int i = 0; i < dlugosc; i++) {
    snake[i].x = x;
    snake[i].y = y;
  }
}

void loop() {
  rotation = analogRead(32);

  uppress = digitalRead(27);
  downpress = digitalRead(15);
  leftpress = digitalRead(18);
  rightpress = digitalRead(34);

  aktualnyCzas = millis();
  roznicaCzasu = aktualnyCzas - zapamietanyCzas;

  // Jeśli różnica wynosi ponad 1/10 sekundy
  if (roznicaCzasu >= 100UL) {
    // Zapamietaj aktualny czas
    zapamietanyCzas = aktualnyCzas;

    if (bonk == 0) {  // początek sterowania
    
      if (rightpress == 1 && direction != 0) {
        direction = 1;
      } else if (leftpress == 1 && direction != 1) {
        direction = 0;
      } else if (uppress == 1 && direction != 3) {
        direction = 2;
      } else if (downpress == 1 && direction != 2) {
        direction = 3;
      }

      if (downpress == 1 && uppress == 1) {  // to jest teporatacja na spawn
        x = 64;
        y = 32;
      }

      if (direction == 1) {
        x = x + 1;
      } else if (direction == 0) {
        x = x - 1;
      } else if (direction == 2) {
        y = y - 1;
      } else if (direction == 3) {
        y = y + 1;
      }

      if (x < 0 || x >= 128 || y < 0 || y >= 64) {
        bonk = 1;
        clear = 0; // Reset clear flag to ensure screen is cleared when snake dies
      }
    }
  }

  // Upewnij się, że dlugosc nie przekracza MAX_SNAKE_LENGTH
  dlugosc = min((rotation + 10) / 3, MAX_SNAKE_LENGTH);

  if (bonk == 0) {
    // Przesunięcie ciała węża
    for (int i = dlugosc - 1; i > 0; i--) {
      if (i < MAX_SNAKE_LENGTH) { // zabezpieczenie przed przekroczeniem granic tablicy
        snake[i] = snake[i - 1];
      }
    }
    snake[0].x = x;
    snake[0].y = y;
  }

  u8g2.clearBuffer();
  u8g2.drawFrame(0, 0, 128, 64);  // Rysowanie ramki ekranu

  if (bonk == 0) {
    // Rysowanie węża
    for (int i = 0; i < dlugosc; i++) {
      if (snake[i].x >= 0 && snake[i].x < 128 && snake[i].y >= 0 && snake[i].y < 64) {
        u8g2.drawPixel(snake[i].x, snake[i].y);
      }
    }
  } else {
    if (clear == 0) {
      clear = 1;
      u8g2.clearBuffer(); // Wyczyść ekran przy pierwszym wywołaniu po śmierci węża
    }
    u8g2.drawStr(4, 50, "snek ded");
  }

  u8g2.sendBuffer();

  Serial.print("bonk: ");
  Serial.println(bonk);

  Serial.print("direction: ");
  Serial.println(direction);

  Serial.print("dlugosc: ");
  Serial.println(dlugosc);
}

the error (appears in serial console):

Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.

Core 1 register dump:
PC : 0x400d1e45 PS : 0x00060830 A0 : 0x800d1ed9 A1 : 0x3ffb21f0
A2 : 0x00000000 A3 : 0x00000073 A4 : 0x00000060 A5 : 0x00000080
A6 : 0x00000000 A7 : 0x003fffff A8 : 0x00000017 A9 : 0x3ffb21d0
A10 : 0x00002cd0 A11 : 0x00000000 A12 : 0x00000038 A13 : 0x00002cd0
A14 : 0x00000000 A15 : 0x00000001 SAR : 0x00000019 EXCCAUSE: 0x0000001c
EXCVADDR: 0x00000018 LBEG : 0x40086270 LEND : 0x4008627b LCOUNT : 0x00000000

Backtrace: 0x400d1e42:0x3ffb21f0 0x400d1ed6:0x3ffb2210 0x400d1f62:0x3ffb2230 0x400d1fb1:0x3ffb2250 0x400d1816:0x3ffb2270 0x400d4585:0x3ffb2290

ELF file SHA256: 83dabb9b3a81652f

|
|
|
|

im using ESP32 board and an OLED 128 x 64 screen

i want the snake to die when touching the frame of the screen, disappear, and then i want a "snek ded" text appear, but i get the guru meditation always when snake touches frame

please help

(also i tried using chat gpt for bug fixing but he did not work, but he left some comments)

Run the ESP Exception Decoder to get the source line of the error. The exception comes from trying to use an invalid address - typically a bad pointer or trying to address outside array bounds (a form of bad pointer).

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.