# NeoPixel stops Refreshing after Array Usage

**URL:** <https://forum.arduino.cc/t/neopixel-stops-refreshing-after-array-usage/1151030>\
**Category:** Programming\
**Created:** [July 23, 2023, 9:27pm UTC](https://forum.arduino.cc/t/neopixel-stops-refreshing-after-array-usage/1151030 "2023-07-23T21:27:37Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![derbrummer](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/derbrummer/32/978285_2.png) [@derbrummer](https://forum.arduino.cc/u/derbrummer)\
**Post date:** [July 23, 2023, 9:27pm UTC](https://forum.arduino.cc/t/neopixel-stops-refreshing-after-array-usage/1151030/1 "2023-07-23T21:27:38Z")

</div>

As soon as i add in a if statement to gather data from an array the pixels stop refreshing.  
This could look like this:

if(array[x][y]==true){  
pixels.setPixelColor(getIndex(x, y),0, 200, 0);  
}

After adding that into my code, the pixels stoped refreshing after the upload.

Hope somebody knows how to resolve this issue.

---

<div class="post-metadata">

**Author:** ![pert](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/pert/32/217111_2.png) [@pert](https://forum.arduino.cc/u/pert)\
**Post date:** [July 23, 2023, 9:29pm UTC](https://forum.arduino.cc/t/neopixel-stops-refreshing-after-array-usage/1151030/2 "2023-07-23T21:29:11Z")

</div>

I moved your topic to an appropriate forum category @derbrummer.

In the future, please take some time to pick the [forum category](https://forum.arduino.cc/categories) that best suits the subject of your topic. There is an " **About the \_\_\_\_\_ category**" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in [the " **How to get the best out of this forum**" guide](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966). The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [July 23, 2023, 10:26pm UTC](https://forum.arduino.cc/t/neopixel-stops-refreshing-after-array-usage/1151030/3 "2023-07-23T22:26:46Z")

</div>

> [@derbrummer](#):
>
> Hope somebody knows how to resolve this issue.

Post the code, or post a minimal compilable we can run it for ourselves it demonstrates your issue sketch.

With no context whatsoever, there could be many things to explain this.

Certainly _if_ statements, arrays and functions are still known to be working quite well. 😑

Welcome to the community, BTW.

a7

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [July 23, 2023, 10:36pm UTC](https://forum.arduino.cc/t/neopixel-stops-refreshing-after-array-usage/1151030/4 "2023-07-23T22:36:37Z")

</div>

As a first step, try adding a serial debug statement like

```arduino
Serial.print("array value is ");
Serial.println(array[x][y]);
if(array[x][y]==true){
pixels.setPixelColor(getIndex(x, y),0, 200, 0);
}

```

---

<div class="post-metadata">

**Author:** ![david\_2018](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/david_2018/32/1013044_2.png) [@david\_2018](https://forum.arduino.cc/u/david_2018)\
**Post date:** [July 23, 2023, 10:56pm UTC](https://forum.arduino.cc/t/neopixel-stops-refreshing-after-array-usage/1151030/5 "2023-07-23T22:56:33Z")

</div>

Also print out the value of getIndex(x, y), x, and y. Writing outside the bounds of an array is a common reason for code to start misbehaving.

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [July 24, 2023, 4:32am UTC](https://forum.arduino.cc/t/neopixel-stops-refreshing-after-array-usage/1151030/6 "2023-07-24T04:32:25Z")

</div>

> [@derbrummer](#):
>
> As soon as i add in a if statement to gather data from an array the pixels stop refreshing.  
> This could look like this:

As soon as I put the key in the ignition and turn it, the car does not start. Hmm, battery flat? Or no petrol? Or ...

Translated to your problem:  
Out of memory? Or writing outside boundaries of array? Or ...

Please post your full code (please use code tags as described in [How to get the best out of this forum](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966/)).

Also please let us know which Arduino you are using.

---

<div class="post-metadata">

**Author:** ![derbrummer](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/derbrummer/32/978285_2.png) [@derbrummer](https://forum.arduino.cc/u/derbrummer)\
**Post date:** [July 24, 2023, 11:52am UTC](https://forum.arduino.cc/t/neopixel-stops-refreshing-after-array-usage/1151030/7 "2023-07-24T11:52:59Z")

</div>

Alright, firstly thanks everyone for the quick responses and suggestions.  
A little more context: im building a little arcade machine with a 11x15 neopixel matrix, an arduino nano 168 and a controller (just 6 buttons hooked up to digital ports) to move up, down, left, right, a game selection and a confirm button.

I want there to be 3 games on that arcade, and ive gotten the game selection and a tic tac toe game running. Now i want to have Snake on it. I managed to get a pixel to move around with the use of the controller, spawn fruit which the snake "consumes" when in contact, which then increases the snakes length varaible, but doesnt actually make it longer yet. I want to use a 2 dimensional array to store the information on where the snakes body parts are at to start makeing the snake longer.

Now, at the end of the code, now as soon as i add a function to check or write to that array and upload that new code the display refuses to refresh. Besides that the code still works in the background, which iwas able to see by adding serial debug statements when entering a game or a menue select.

Here the Code, at the end is an example of my problem:

```arduino
#include <Adafruit_NeoPixel.h>

//Anzahl LED's und Ausgang vom ESP festlegen
#define PIN 10 //DigitalPin 12
#define NUMBERS 165 //Menge LEDs 0-165 (166stck)

//Adresse der ersten und letzten LED
const int ersteLED = 0;
const int letzteLED = 164;

int x = 0;
int y = 0;

short activePlayer=0; //0 Kreuz / 1 Kreis

int red = 80;
int green = 80;
int blue = 80;

int TTTAltxFledIndex;
short TTTRotPunkte=0;
short TTTBlauPunkte=0;
short TTTZugZahl=0;
int TTTFeldIndex =0;
int TTTFelderMitte[3][3]{
{130,126,122},
{86,82,78},
{42,38,34}
};

int TTTFelderVergeben[3][3]{ //0 nicht - 1=Kreuz - 4= Kreis
{0,0,0},
{0,0,0},
{0,0,0}
};

short SnakeRichtung=0; //0Up 1R 2L 3Down
short SnakeLength=1;
bool SnakeGegessen=false;
short fy;
short fx;
bool SnakeBody[11][11];

int oldx = 0;
int oldy = 0;

int numPins = 6;
int pins[] = {0, 0, 0, 0, 0, 0}; // Pin-Nummern

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMBERS, PIN, NEO_GRB + NEO_KHZ800);

//Berechnung des Pixelindex mit x&y
int getIndex(int x, int y) {
  int offset = 0;  
  if(y % 2 != 0) {
    // Jede zweite Zeile geht von rechts aus
    offset = 10;
    x = -x;
  }
  return y * (11) + x + offset;
}

void setup() {
 Serial.begin(19200);
 pixels.begin();
 pixels.clear();
 pixels.show();
  for (int i = 2; i <= 7; i++) {
    pinMode(pins[i], INPUT);
  }

  for(short b=0; b<=10; b++){
    for(short c=0; c<=10; c++){
      SnakeBody[b][c]=false;
    }
  }
}

void loop() {
  Menue();
}

void TTTSpiel(){
  TTTFeldStreben();
  TTTPunkteZeichnen();
  TTTFeldWahl();
}
void inputLesen(){
    for (int i = 2; i <= 7; i++) {
    pins[i-2]=digitalRead(i);
  }
}
void TTTFeldStreben() { //Zeichnet Raster
  int red = 80;
  int green = 80;
  int blue = 80;
  x = 1;
  y = 0;
  
  // Querstreben
  for (int i = 0; i <= 3; i++) {
    x = 1 + i * 4;
    for (int spalte = 0; spalte <= 10; spalte++) {
      y = spalte;
      pixels.setPixelColor(getIndex(y, x), red, green, blue);
    }
  }

  for (int i = 3; i <= 7; i += 4) {
  for (int reihe = 1; reihe <= 13; reihe++) {
    y = i;
    x = reihe;
    pixels.setPixelColor(getIndex(y, x), red, green, blue);
  }
 }
  pixels.show();
}
void TTTFeldWahl(){
  int x=1;
  int y=1;
  bool done=false;
  while(done==false){

  pixels.setPixelColor(getIndex(3, 11),80,80,80); //BugAuslgiech fehlender Pixel
  pixels.show();

  pixels.setPixelColor(TTTFeldIndex,0,0,0);

  inputLesen();
  if(pins[0]==1){y--;}
  if(pins[3]==1){y++;}
  if(pins[1]==1){x--;}
  if(pins[2]==1){x++;}
  if(pins[4]==1){Menue();}

  if(x>2){x=2;}
  if(x<0){x=0;}
  if(y>2){y=2;}
  if(y<0){y=0;}

  delay(140);
  TTTFeldIndex=TTTFelderMitte[y][x];
  if(activePlayer==0){pixels.setPixelColor(TTTFeldIndex,150,70,0);}else{pixels.setPixelColor(TTTFeldIndex,0,100,100);}
  pixels.show();
  if(TTTFelderVergeben[y][x]==0){//FeldvonKreuz
    if(pins[5]==1){done=true;}
    }
  }
    if(activePlayer==1){
      TTTKreisZeichnen();
      TTTFelderVergeben[y][x]=4;
      }
      else{
        TTTKreuzZeichnen();
        TTTFelderVergeben[y][x]=1;
      }
  TTTCheckSieger();
  TTTSpiel();
}
void TTTPunkteZeichnen(){
  if(TTTRotPunkte==12 || TTTBlauPunkte==12){
    TTTRotPunkte=0;
    TTTBlauPunkte=0;
    } //volle Punkte bereits erreicht
  short i=0;
  //RotePunkteZeichen
  if(TTTRotPunkte>0){
    while(i<TTTRotPunkte){
      pixels.setPixelColor(i,200,0,0);
      i++;
    }
  }

  i=0;

  if(TTTBlauPunkte>0){
    while(i<TTTBlauPunkte){
      pixels.setPixelColor(i+154,0,0,200);
      i++;
    }  
  }

}
void TTTCheckSieger(){ //Sieg Blau =12 Sieg Rot=3
  bool spiellauft=true;
  if(
    TTTFelderVergeben[2][2] + TTTFelderVergeben[2][1] + TTTFelderVergeben[2][0] == 3 || //HReiheUnten
    TTTFelderVergeben[1][2] + TTTFelderVergeben[1][1] + TTTFelderVergeben[1][0] == 3 || //HReiheMitte
    TTTFelderVergeben[0][2] + TTTFelderVergeben[0][1] + TTTFelderVergeben[0][0] == 3 || //HReiheOben

    TTTFelderVergeben[2][2] + TTTFelderVergeben[1][2] + TTTFelderVergeben[0][2] == 3 || //VReiheRechts
    TTTFelderVergeben[2][1] + TTTFelderVergeben[1][1] + TTTFelderVergeben[0][1] == 3 || //VReiheMitte
    TTTFelderVergeben[2][0] + TTTFelderVergeben[1][0] + TTTFelderVergeben[0][0] == 3 || //VReiheLinks

    TTTFelderVergeben[2][2] + TTTFelderVergeben[1][1] + TTTFelderVergeben[0][0] == 3 || //ReiheUR nach OL
    TTTFelderVergeben[2][0] + TTTFelderVergeben[1][1] + TTTFelderVergeben[0][2] == 3 //ReiheUL nach OR
    ){
      TTTRotPunkte++;
      if(TTTRotPunkte==11){TTTBildReset();}
      TTTBildReset();
      activePlayer=1;
    }

  if(
    TTTFelderVergeben[2][2] + TTTFelderVergeben[2][1] + TTTFelderVergeben[2][0] == 12 || //HReiheUnten
    TTTFelderVergeben[1][2] + TTTFelderVergeben[1][1] + TTTFelderVergeben[1][0] == 12 || //HReiheMitte
    TTTFelderVergeben[0][2] + TTTFelderVergeben[0][1] + TTTFelderVergeben[0][0] == 12 || //HReiheOben

    TTTFelderVergeben[2][2] + TTTFelderVergeben[1][2] + TTTFelderVergeben[0][2] == 12 || //VReiheRechts
    TTTFelderVergeben[2][1] + TTTFelderVergeben[1][1] + TTTFelderVergeben[0][1] == 12 || //VReiheMitte
    TTTFelderVergeben[2][0] + TTTFelderVergeben[1][0] + TTTFelderVergeben[0][0] == 12 || //VReiheLinks

    TTTFelderVergeben[2][2] + TTTFelderVergeben[1][1] + TTTFelderVergeben[0][0] == 12 || //ReiheUR nach OL
    TTTFelderVergeben[2][0] + TTTFelderVergeben[1][1] + TTTFelderVergeben[0][2] == 12 //ReiheUL nach OR
    ){
      TTTBlauPunkte++;
      if(TTTBlauPunkte==11){TTTBildReset();}
      TTTBildReset();
      activePlayer=0;
    }

  for (int i = 0; i < 3 && spiellauft==true; i++) { //unentschieden
    for (int j = 0; j < 3 && spiellauft==true; j++) {
      if(TTTFelderVergeben[i][j] == 0){
        spiellauft=false;
      }
    }
  }
  if(spiellauft==true) {
    TTTBildReset();
  }
  TTTPunkteZeichnen();
}
void TTTBildReset(){
  pixels.clear();
    //ArrayMit0Füllen
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      TTTFelderVergeben[i][j] = 0;
    }
  } 
  delay(200);
}
void TTTKreuzZeichnen(){
  if(TTTFeldIndex==126 || TTTFeldIndex==82 || TTTFeldIndex==38){//Mitte
    pixels.setPixelColor(TTTFeldIndex+1,100,0,0);
    pixels.setPixelColor(TTTFeldIndex-1,100,0,0);
    pixels.setPixelColor(TTTFeldIndex+12,100,0,0);
    pixels.setPixelColor(TTTFeldIndex+11,100,0,0);
    pixels.setPixelColor(TTTFeldIndex+10,100,0,0);
    pixels.setPixelColor(TTTFeldIndex-11,100,0,0);
    pixels.setPixelColor(TTTFeldIndex-12,100,0,0);
    pixels.setPixelColor(TTTFeldIndex-10,100,0,0);
  }

    if(TTTFeldIndex==130 || TTTFeldIndex==86 || TTTFeldIndex==42){//Links
      pixels.setPixelColor(TTTFeldIndex+1,100,0,0);
      pixels.setPixelColor(TTTFeldIndex-1,100,0,0);
      pixels.setPixelColor(TTTFeldIndex+2,100,0,0);
      pixels.setPixelColor(TTTFeldIndex+3,100,0,0);
      pixels.setPixelColor(TTTFeldIndex+4,100,0,0);
      pixels.setPixelColor(TTTFeldIndex-18,100,0,0);
      pixels.setPixelColor(TTTFeldIndex-19,100,0,0);
      pixels.setPixelColor(TTTFeldIndex-20,100,0,0);
    }

    if(TTTFeldIndex==122 || TTTFeldIndex==78 || TTTFeldIndex==34){//Rechts
      pixels.setPixelColor(TTTFeldIndex+1,100,0,0);
      pixels.setPixelColor(TTTFeldIndex-1,100,0,0);
      pixels.setPixelColor(TTTFeldIndex+18,100,0,0);
      pixels.setPixelColor(TTTFeldIndex+19,100,0,0);
      pixels.setPixelColor(TTTFeldIndex+20,100,0,0);
      pixels.setPixelColor(TTTFeldIndex-2,100,0,0);
      pixels.setPixelColor(TTTFeldIndex-3,100,0,0);
      pixels.setPixelColor(TTTFeldIndex-4,100,0,0);
    }

    if(TTTFeldIndex==124){
    pixels.setPixelColor(TTTFeldIndex+1,100,0,0);
    pixels.setPixelColor(TTTFeldIndex-1,100,0,0);
    pixels.setPixelColor(TTTFeldIndex+14,100,0,0);
    pixels.setPixelColor(TTTFeldIndex+15,100,0,0);
    pixels.setPixelColor(TTTFeldIndex+16,100,0,0);
    pixels.setPixelColor(TTTFeldIndex-6,100,0,0);
    pixels.setPixelColor(TTTFeldIndex-7,100,0,0);
    pixels.setPixelColor(TTTFeldIndex-8,100,0,0);
    }

  activePlayer=1;
  pixels.show();
}
void TTTKreisZeichnen(){
  if(TTTFeldIndex==126 || TTTFeldIndex==82 || TTTFeldIndex==38){//Mitte
    pixels.setPixelColor(TTTFeldIndex+1,0,0,100);
    pixels.setPixelColor(TTTFeldIndex-1,0,0,100);
    pixels.setPixelColor(TTTFeldIndex+12,0,0,100);
    pixels.setPixelColor(TTTFeldIndex+11,0,0,100);
    pixels.setPixelColor(TTTFeldIndex+10,0,0,100);
    pixels.setPixelColor(TTTFeldIndex-11,0,0,100);
    pixels.setPixelColor(TTTFeldIndex-12,0,0,100);
    pixels.setPixelColor(TTTFeldIndex-10,0,0,100);
  }
  
    if(TTTFeldIndex==130 || TTTFeldIndex==86 || TTTFeldIndex==42){//Links
      pixels.setPixelColor(TTTFeldIndex+1,0,0,100);
      pixels.setPixelColor(TTTFeldIndex-1,0,0,100);
      pixels.setPixelColor(TTTFeldIndex+2,0,0,100);
      pixels.setPixelColor(TTTFeldIndex+3,0,0,100);
      pixels.setPixelColor(TTTFeldIndex+4,0,0,100);
      pixels.setPixelColor(TTTFeldIndex-18,0,0,100);
      pixels.setPixelColor(TTTFeldIndex-19,0,0,100);
      pixels.setPixelColor(TTTFeldIndex-20,0,0,100);
    }

    if(TTTFeldIndex==122 || TTTFeldIndex==78 || TTTFeldIndex==34){//Rechts
      pixels.setPixelColor(TTTFeldIndex+1,0,0,100);
      pixels.setPixelColor(TTTFeldIndex-1,0,0,100);
      pixels.setPixelColor(TTTFeldIndex+18,0,0,100);
      pixels.setPixelColor(TTTFeldIndex+19,0,0,100);
      pixels.setPixelColor(TTTFeldIndex+20,0,0,100);
      pixels.setPixelColor(TTTFeldIndex-2,0,0,100);
      pixels.setPixelColor(TTTFeldIndex-3,0,0,100);
      pixels.setPixelColor(TTTFeldIndex-4,0,0,100);
    }

    if(TTTFeldIndex==128){
      pixels.setPixelColor(TTTFeldIndex+1,0,0,100);
      pixels.setPixelColor(TTTFeldIndex-1,0,0,100);
      pixels.setPixelColor(TTTFeldIndex+6,0,0,100);
      pixels.setPixelColor(TTTFeldIndex+7,0,0,100);
      pixels.setPixelColor(TTTFeldIndex+8,0,0,100);
      pixels.setPixelColor(TTTFeldIndex-14,0,0,100);
      pixels.setPixelColor(TTTFeldIndex-15,0,0,100);
      pixels.setPixelColor(TTTFeldIndex-16,0,0,100);
    }

  activePlayer=0;
  pixels.show();
}
void Menue(){
 MenueZeichnen();
 MenueAuswahl();
}
void MenueZeichnen(){
  TTTBildReset();
  TTTPunkteZeichnen();

  // Querstreben
  for (int i = 0; i <= 3; i++) {
    x = 1 + i * 4;
    for (int spalte = 0; spalte <= 10; spalte++) {
      y = spalte;
      pixels.setPixelColor(getIndex(y, x), red, green, blue);
    }
  } 

  //TicTacToe Auswahl zeichnen
  TTTFeldIndex=124;
  TTTKreuzZeichnen();
  TTTFeldIndex=128;
  TTTKreisZeichnen();

  //Schlange Zeichnen
  for(short b; b<5;b++){
    pixels.setPixelColor(getIndex(b+2, 3), 0, 120, 0);
  }
  pixels.setPixelColor(getIndex(8, 3), 120, 0, 0);

  pixels.show();
}
void MenueAuswahl(){
  bool bestaetigt=false;
  short k=0;

  while(bestaetigt==false){
    inputLesen();
    if(pins[0]==1){k--;MenueZeichnen();}
    if(pins[3]==1){k++;MenueZeichnen();}
    if(pins[5]==1){bestaetigt=true;}
    if(k>2){k=0;}
    if(k<0){k=2;}
    delay(140);

    if(k==0){//TTT
    pixels.setPixelColor(getIndex(0, 10),128, 0, 128);
    pixels.setPixelColor(getIndex(0, 11),128, 0, 128);
    pixels.setPixelColor(getIndex(0, 12),128, 0, 128);
    pixels.setPixelColor(getIndex(10, 10),128, 0, 128);
    pixels.setPixelColor(getIndex(10, 11),128, 0, 128);
    pixels.setPixelColor(getIndex(10, 12),128, 0, 128);
  }

  if(k==1){//4Cnct
    pixels.setPixelColor(getIndex(0, 6),128, 0, 128);
    pixels.setPixelColor(getIndex(0, 7),128, 0, 128);
    pixels.setPixelColor(getIndex(0, 8),128, 0, 128);
    pixels.setPixelColor(getIndex(10, 6),128, 0, 128);
    pixels.setPixelColor(getIndex(10, 7),128, 0, 128);
    pixels.setPixelColor(getIndex(10, 8),128, 0, 128);
  }

  if(k==2){//Snake
    pixels.setPixelColor(getIndex(0, 2),128, 0, 128);
    pixels.setPixelColor(getIndex(0, 3),128, 0, 128);
    pixels.setPixelColor(getIndex(0, 4),128, 0, 128);
    pixels.setPixelColor(getIndex(10, 2),128, 0, 128);
    pixels.setPixelColor(getIndex(10, 3),128, 0, 128);
    pixels.setPixelColor(getIndex(10, 4),128, 0, 128);
  }

  pixels.show();
  }

  x=1;
  y=1;
  pixels.clear();
  if(k==0){//TTT
      TTTSpiel();
  }

  if(k==1){//4Cnct
    TTTSpiel();
  }

  if(k==2){//Snake
    SnakeSpiel();
  }
}
void SnakeSpiel(){
  SnakeBildReset();
  SnakeFruchtSpawn();
  SnakeBewegen();
}
void SnakeBildReset(){
  pixels.clear();
  for (int i = 0; i <= 3; i++){
    x = 1 + i * 12;
    for (int spalte = 0; spalte <= 10; spalte++) {
      y = spalte;
      pixels.setPixelColor(getIndex(y, x), red, green, blue);
    }
  }
  pixels.show();
}

void SnakeBewegen(){
  x=5;
  y=7;
  SnakeRichtung=5; 
  while(pins[4]==0){
    SnakeEssenCheck();
    if(SnakeGegessen==true){
     SnakeLength = SnakeLength +1;
     SnakeGegessen =false;
     SnakeFruchtSpawn();
     Serial.println(SnakeLength);
    }
    SnakeInputs();
    pixels.setPixelColor(getIndex(x, y),0, 100, 0);
    pixels.show();
    delay(100);
    pixels.setPixelColor(getIndex(x, y),0, 0, 0);
    pixels.show();
  }
}

void SnakeEssenCheck(){
  if(fx==x && fy == y){
    SnakeGegessen=true;
  }
} 

void SnakeFruchtSpawn(){
  fx = random(0, 11);
  fy = random(2, 13);
  if(fx != x || fy != y){
    pixels.setPixelColor(getIndex(fx, fy),100, 0, 0);
  }
}

void SnakeInputs(){//0Up 1R 2L 3Down
  inputLesen();
  if(pins[0]==1 && SnakeRichtung != 3){SnakeRichtung=0;}
  if(pins[3]==1 && SnakeRichtung != 0){SnakeRichtung=3;}
  if(pins[1]==1 && SnakeRichtung != 1){SnakeRichtung=2;}
  if(pins[2]==1 && SnakeRichtung != 2){SnakeRichtung=1;}
  if(pins[4]==1){Menue();}

  if(SnakeRichtung==0){
    y++;
    if(y>12) {y=2;}
    }
  if(SnakeRichtung==3){
    y--;
    if(y<2) {y=12;}
    }
  if(SnakeRichtung==2){
    x--;
    if(x<0) {x=10;}
    }
  if(SnakeRichtung==1){
    x++;
    if(x>10) {x=0;}
    }
    
//Examplfe of crash code
    //if(SnakeBody[x][y]==true){
    // Menue();
    //}

}

```

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [July 24, 2023, 12:07pm UTC](https://forum.arduino.cc/t/neopixel-stops-refreshing-after-array-usage/1151030/8 "2023-07-24T12:07:04Z")

</div>

> [@derbrummer](#):
>
> ```arduino
> //Examplfe of crash code
> //if(SnakeBody[x][y]==true){
> // Menue();
> //}
> 
> ```

You problem is not a using the array, your problem is calling the `Menue()` function.

Below the logical tree of the calling `SnakeInputs()` - the function where the problem code is located :

```arduino
Menue() ->
  MenueAuswahl() -> 
       SnakeSpiel() -> 
         SnakeBewegen() ->
                   SnakeInputs() -> Menue() 

```

Ultimately, through several intermediate levels, you try to call the `Menue() ` function from itself. This is called **recursion** , and you can only use it if you clearly understand what you are doing.  
In your code, you use it to return to the main menu in order to select a new menu item again later. You can't do that. This is actually an infinite loop and it is guaranteed to lead to a program hang, which is what happens to you.  
You need to change the logic of working with the menu. Instead of calling a new `Menue()` instance from the `SnakeInputs()`, you should return to first `Menue()` by exiting each lower function to the higher one sequentially.

---

<div class="post-metadata">

**Author:** ![derbrummer](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/derbrummer/32/978285_2.png) [@derbrummer](https://forum.arduino.cc/u/derbrummer)\
**Post date:** [July 26, 2023, 2:22pm UTC](https://forum.arduino.cc/t/neopixel-stops-refreshing-after-array-usage/1151030/9 "2023-07-26T14:22:07Z")

</div>

Hello. Thanks for your anwser. Sorry im fairly new to working with the arduino so i guess these things happen.  
Recursion might have been the problem, but i was too lazy to find all the mistakes in that code to fix that...so i rewrote the whole thing (menue and tictactoe work) but i run into the same problem with snake now.

Ive tried avoiding recursion as much as i can, but i can`t tell if i did the same mistake again.

When trying to call the  
line 459 - SnakeBodyPositionsSetzen(); function the leds freeze again. This function simply sets the first value of an array to 1 for now.

```arduino
#include <Adafruit_NeoPixel.h>

//Anzahl LED's und Ausgang vom ESP festlegen
#define PIN 10 //DigitalPin 12
#define NUMBERS 165 //Menge LEDs 0-165 (166stck)

//Adresse der ersten und letzten LED
const int ersteLED = 0;
const int letzteLED = 164;

int x = 0;
int y = 0;

short TTTFelder[3][3]{ //0 nicht - 1=Kreuz - 4= Kreis
{0,0,0},
{0,0,0},
{0,0,0}
};

short TTTAktuellerSpieler =0; //0Rot 1 Blau
short TTTSpielLaenge=0;
short TTTRotPunkte=0;
short TTTBlauPunkte=0;

short SnakeLength = 1;
int SnakeBody[122];
short SnakeRichtung =0; //1up 2down 3left 4 right

int red = 80;
int green = 80;
int blue = 80;

int numPins = 6;
int pins[] = {0, 0, 0, 0, 0, 0}; // Pin-Nummern

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMBERS, PIN, NEO_GRB + NEO_KHZ800);

//Berechnung des Pixelindex mit x&y
int getIndex(int x, int y) {
  int offset = 0;  
  if(y % 2 != 0) {
    // Jede zweite Zeile geht von rechts aus
    offset = 10;
    x = -x;
  }
  return y * (11) + x + offset;
}
void setup() {
 Serial.begin(19200);
 pixels.begin();
 pixels.clear();
 pixels.show();
  for (int i = 2; i <= 7; i++) {
    pinMode(pins[i], INPUT);
  }
}
void loop() {
  switch(Menue()){
    case 0:
    Serial.println("Snake");
    SnakeSpiel(); 
    break;
    case 1:
    Serial.println("mitte"); 
    break;
    case 2:
    Serial.println("TTT");
    TTTSpiel(); 
    break;
  }
}
short InputRueckgabe(){
  short InputWahl = 0;
    for (int i = 2; i <= 7; i++) {
    pins[i-2]=digitalRead(i);
  }

  if(pins[0]==1){InputWahl=1;}//up
  if(pins[3]==1){InputWahl=2;}//down
  if(pins[1]==1){InputWahl=3;}//left
  if(pins[2]==1){InputWahl=4;}//right
  if(pins[4]==1){InputWahl=6;}//confirm
  if(pins[5]==1){InputWahl=5;}//menue

  return InputWahl;
}
short Menue(){
  short Auswahl=0; // 0 nichts, 1 TTT, 2 Snake , 3 dunno
  MenueZeichnen();
  Auswahl=MenueAuswahl(); //loop bis confirm
  return Auswahl;
}
void MenueZeichnen(){
  pixels.clear();
  TTTPunkteZeichnen();
  // Querstreben
  for (int i = 0; i <= 3; i++) {
    x = 1 + i * 4;
    for (int spalte = 0; spalte <= 10; spalte++) {
      y = spalte;
      pixels.setPixelColor(getIndex(y, x), red, green, blue);
    }
  }
    //Schlange Zeichnen
  for(short b=0; b<5;b++){
    pixels.setPixelColor(getIndex(b+2, 3), 0, 120, 0);
  }
  pixels.setPixelColor(getIndex(8, 3), 120, 0, 0);

    //TTTZeichnen
  TTTBlauZeichnen(3,11);
  TTTRotZeichnen(7,11);
  pixels.show();
}
short MenueAuswahl(){//loop bis confirm
  short b=1;
  bool Confirm=false;
  while(Confirm==false){
  switch(InputRueckgabe()){
    default: break; //noInput
    case 1://up
    b++;
    if(b>2){b=0;}
    break;
    case 2://down
    b--;
    if(b<0){b=2;}
    break;
    case 5:
    Confirm=true;
    break;
  }
  MenueZeichnen();
    if(b==2){//TTT
    pixels.setPixelColor(getIndex(0, 10),128, 0, 128);
    pixels.setPixelColor(getIndex(0, 11),128, 0, 128);
    pixels.setPixelColor(getIndex(0, 12),128, 0, 128);
    pixels.setPixelColor(getIndex(10, 10),128, 0, 128);
    pixels.setPixelColor(getIndex(10, 11),128, 0, 128);
    pixels.setPixelColor(getIndex(10, 12),128, 0, 128);
  }

  if(b==1){//4Cnct
    pixels.setPixelColor(getIndex(0, 6),128, 0, 128);
    pixels.setPixelColor(getIndex(0, 7),128, 0, 128);
    pixels.setPixelColor(getIndex(0, 8),128, 0, 128);
    pixels.setPixelColor(getIndex(10, 6),128, 0, 128);
    pixels.setPixelColor(getIndex(10, 7),128, 0, 128);
    pixels.setPixelColor(getIndex(10, 8),128, 0, 128);
  }

  if(b==0){//Snake
    pixels.setPixelColor(getIndex(0, 2),128, 0, 128);
    pixels.setPixelColor(getIndex(0, 3),128, 0, 128);
    pixels.setPixelColor(getIndex(0, 4),128, 0, 128);
    pixels.setPixelColor(getIndex(10, 2),128, 0, 128);
    pixels.setPixelColor(getIndex(10, 3),128, 0, 128);
    pixels.setPixelColor(getIndex(10, 4),128, 0, 128);
  }

  pixels.show();
  delay(150);
 }
  return b;
}
void TTTSpiel(){
  short input=0;
  x=2;
  y=2;
  bool TTTKomplettVorbei=false;
  bool TTTRundeVorbei=false;
  while(TTTKomplettVorbei==false){
  TTTReset();
  TTTRasterZeichnen();
  TTTPunkteZeichnen();
  x=5;
  y=7;
  TTTRundeVorbei=false;
    while(TTTRundeVorbei==false){
      pixels.setPixelColor(getIndex(x,y),0,0,0);

      input=InputRueckgabe();
      if(input==1){y=y+4;}
      if(input==2){y=y-4;}
      if(input==3){x=x-4;}
      if(input==4){x=x+4;}

      if(x>9){x=1;}
      if(x<1){x=9;}
      if(y>11){y=3;}
      if(y<3){y=11;}

      if(TTTAktuellerSpieler==0){
        pixels.setPixelColor(getIndex(x,y),100,50,0);
        pixels.show();
      }
      if(TTTAktuellerSpieler==1){
        pixels.setPixelColor(getIndex(x,y),0,100,100);
        pixels.show();
      }
      delay(150);
      if(input==5){      
        TTTFeldEinnehmen(x,y);
          if(TTTCheckSieger()==true){
          TTTRundeVorbei=true;
        }
      }
      if(input==6){
        TTTSpielLaenge=0;
        TTTRundeVorbei==true;
        TTTKomplettVorbei=true;
        break;
      }
    }
  }
}
void TTTFeldEinnehmen(short xx, short yy){
  short tempx=xx;
  short tempy=yy;
  
  if(xx==1){xx=0;}
  if(xx==5){xx=1;}
  if(xx==9){xx=2;}

  if(yy==11){yy=0;}
  if(yy==7){yy=1;}
  if(yy==3){yy=2;}

  if(TTTFelder[yy][xx]==0){
    switch(TTTAktuellerSpieler){
      case 0:
      TTTRotZeichnen(tempx, tempy);
      TTTAktuellerSpieler=1;
      TTTFelder[yy][xx]=1;
      TTTSpielLaenge=TTTSpielLaenge+1;
      pixels.setPixelColor(getIndex(x,y),0,0,0);
      x=5;
      y=7;
      break;
      case 1:
      TTTBlauZeichnen(tempx,tempy);
      TTTAktuellerSpieler=0;
      TTTFelder[yy][xx]=4;
      TTTSpielLaenge=TTTSpielLaenge+1;
      pixels.setPixelColor(getIndex(x,y),0,0,0);
      x=5;
      y=7;
      break;
    }
  }
}
bool TTTCheckSieger(){
  bool Sieg=false;
  if(
      TTTFelder[2][2] + TTTFelder[2][1] + TTTFelder[2][0] == 3 || //HReiheUnten
      TTTFelder[1][2] + TTTFelder[1][1] + TTTFelder[1][0] == 3 || //HReiheMitte
      TTTFelder[0][2] + TTTFelder[0][1] + TTTFelder[0][0] == 3 || //HReiheOben

      TTTFelder[2][2] + TTTFelder[1][2] + TTTFelder[0][2] == 3 || //VReiheRechts
      TTTFelder[2][1] + TTTFelder[1][1] + TTTFelder[0][1] == 3 || //VReiheMitte
      TTTFelder[2][0] + TTTFelder[1][0] + TTTFelder[0][0] == 3 || //VReiheLinks

      TTTFelder[2][2] + TTTFelder[1][1] + TTTFelder[0][0] == 3 || //ReiheUR nach OL
      TTTFelder[2][0] + TTTFelder[1][1] + TTTFelder[0][2] == 3 //ReiheUL nach OR
    ){
      TTTRotPunkte++;
      TTTAktuellerSpieler=1;
      TTTSpielLaenge=0;
      Sieg=true;
      return Sieg;
    }
  if(
      TTTFelder[2][2] + TTTFelder[2][1] + TTTFelder[2][0] == 12 || //HReiheUnten
      TTTFelder[1][2] + TTTFelder[1][1] + TTTFelder[1][0] == 12 || //HReiheMitte
      TTTFelder[0][2] + TTTFelder[0][1] + TTTFelder[0][0] == 12 || //HReiheOben

      TTTFelder[2][2] + TTTFelder[1][2] + TTTFelder[0][2] == 12 || //VReiheRechts
      TTTFelder[2][1] + TTTFelder[1][1] + TTTFelder[0][1] == 12 || //VReiheMitte
      TTTFelder[2][0] + TTTFelder[1][0] + TTTFelder[0][0] == 12 || //VReiheLinks

      TTTFelder[2][2] + TTTFelder[1][1] + TTTFelder[0][0] == 12 || //ReiheUR nach OL
      TTTFelder[2][0] + TTTFelder[1][1] + TTTFelder[0][2] == 12 //ReiheUL nach OR
    ){
      TTTBlauPunkte++;
      TTTAktuellerSpieler=0;
      TTTSpielLaenge=0;
      Sieg=true;
      return Sieg;
    }
    if(TTTSpielLaenge==9){
      TTTSpielLaenge=0;
      Sieg=true;
      return Sieg;
    }
  Sieg=false;
  return Sieg;
}
void TTTRasterZeichnen(){
  pixels.clear();
  // Querstreben
  for (int i = 0; i <= 3; i++) {
    x = 1 + i * 4;
    for (int spalte = 0; spalte <= 10; spalte++) {
      y = spalte;
      pixels.setPixelColor(getIndex(y, x), red, green, blue);
    }
  }
  //Hochstreben
    for (int i = 3; i <= 7; i += 4) {
  for (int reihe = 1; reihe <= 13; reihe++) {
    y = i;
    x = reihe;
    pixels.setPixelColor(getIndex(y, x), red, green, blue);
  }
 }
 pixels.show();
}
void TTTReset(){
  for(short j=0;j<3;j++){
    for(short i=0;i<3;i++){
      TTTFelder[j][i]=0;
    }
  }
}
void TTTPunkteZeichnen(){
  if(TTTRotPunkte==12 || TTTBlauPunkte==12){
    TTTRotPunkte=0;
    TTTBlauPunkte=0;
    } //volle Punkte bereits erreicht
  short i=0;
  //RotePunkteZeichen
  if(TTTRotPunkte>0){
    while(i<TTTRotPunkte){
      pixels.setPixelColor(i,200,0,0);
      i++;
    }
  }
  i=0;
  if(TTTBlauPunkte>0){
    while(i<TTTBlauPunkte){
      pixels.setPixelColor(i+154,0,0,200);
      i++;
    }  
  }
}
void TTTBlauZeichnen(short x, short y){
  pixels.setPixelColor(getIndex(x+1,y+1), 0, 0, 100);
  pixels.setPixelColor(getIndex(x-1,y+1), 0, 0, 100);
  pixels.setPixelColor(getIndex(x+1,y-1), 0, 0, 100);
  pixels.setPixelColor(getIndex(x-1,y-1), 0, 0, 100);
  pixels.setPixelColor(getIndex(x+1,y), 0, 0, 100);
  pixels.setPixelColor(getIndex(x-1,y), 0, 0, 100);
  pixels.setPixelColor(getIndex(x,y+1), 0, 0, 100);
  pixels.setPixelColor(getIndex(x,y-1), 0, 0, 100);
  delay(30);
}
void TTTRotZeichnen(short x, short y){
  pixels.setPixelColor(getIndex(x+1,y+1), 100, 0, 0);
  pixels.setPixelColor(getIndex(x-1,y+1), 100, 0, 0);
  pixels.setPixelColor(getIndex(x+1,y-1), 100, 0, 0);
  pixels.setPixelColor(getIndex(x-1,y-1), 100, 0, 0);
  pixels.setPixelColor(getIndex(x+1,y), 100, 0, 0);
  pixels.setPixelColor(getIndex(x-1,y), 100, 0, 0);
  pixels.setPixelColor(getIndex(x,y+1), 100, 0, 0);
  pixels.setPixelColor(getIndex(x,y-1), 100, 0, 0);
  delay(30);
}
void SnakeReset(){
  pixels.clear();
  // Querstreben
  for (int i = 0; i <= 3; i++){
    x = 1 + i * 12;
    for (int spalte = 0; spalte <= 10; spalte++) {
      y = spalte;
      pixels.setPixelColor(getIndex(y, x), red, green, blue);
    }
  }
  pixels.show();
  x=5;
  y=7;
  SnakeRichtung =0;
  SnakeLength =1;
}

void SnakeSpiel(){//SnakeRichtung =0; //1up 2down 3left 4 right
  SnakeReset();
  bool zueruckMenue =false;
  while(zueruckMenue==false){
    switch(InputRueckgabe()){//Richtung ermitteln
    default: break; //noInput
    case 1://up
    if(SnakeRichtung !=2){
      SnakeRichtung =1;
    }
    break;
    case 2://down
    if(SnakeRichtung !=1){
    SnakeRichtung =2;
    }
    break;
    case 3://left
    if(SnakeRichtung !=4){
    SnakeRichtung =3;
    }
    break;
    case 4://right
    if(SnakeRichtung !=3){
    SnakeRichtung =4;
    }
    break;
    case 5://confirm
    break;
    case 6://menue
      zueruckMenue = true;
    break;
    }

    switch(SnakeRichtung){//nächste x/y koordinate mit Richtung bestimmen. Korrektur für überschuss
    default: break; //noInput
    case 1://up
    y++;
    if(y>12){
    y =2;
    }
    break;
    case 2://down
    y--;
    if(y<2){
    y =12;
    }
    break;
    case 3://left
    x--;
    if(x<0){
    x =10;
    }
    break;
    case 4://right
    x++;
    if(x>10){
    x =0;
    }
    break;
    }
    //SnakeBodyPositionsSetzen();
    SnakeBodyZeichnen();
  }
}
void SnakeFruchtSpawn(){
  short fx;
  short fy;
  fx = random(0, 11);
  fy = random(2, 13);
  if(fx != x || fy != y){
    pixels.setPixelColor(getIndex(fx, fy),100, 0, 0);
  }
}

void SnakeBodyPositionsSetzen(){
  SnakeBody[0]=1;
}

void SnakeBodyZeichnen(){
  for(short j=0;j<121;j++){
    if(SnakeBody[j]!=0){
      pixels.setPixelColor(SnakeBody[j],0,100,0);
    }
    if(SnakeBody[j]==0){
    pixels.setPixelColor(SnakeBody[j],0,0,0);
    }
  }
  pixels.show();
}

```

---

<div class="post-metadata">

**Author:** ![derbrummer](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/derbrummer/32/978285_2.png) [@derbrummer](https://forum.arduino.cc/u/derbrummer)\
**Post date:** [July 26, 2023, 2:34pm UTC](https://forum.arduino.cc/t/neopixel-stops-refreshing-after-array-usage/1151030/10 "2023-07-26T14:34:12Z")

</div>

```arduino
void SnakeBodyPositionsSetzen(short xx,short yy){
  if(SnakeLength<121){
  for(short i=SnakeLength;i>0;i--){
    SnakeBody[i]=SnakeBody[i-1];
  }
  SnakeBody[0]=getIndex(xx, yy);
 }
 Serial.println(SnakeBody[0]);
}

```

Using the serial print and blindly navigating to the game i can see that the correct Numbers are being written to the Array:

16:42:53.730 -\> slot0: 87 slot1: 77 slot2: 78  
16:42:53.902 -\> slot0: 86 slot1: 87 slot2: 77  
16:42:54.041 -\> slot0: 85 slot1: 86 slot2: 87  
16:42:54.198 -\> slot0: 84 slot1: 85 slot2: 86  
16:42:54.355 -\> slot0: 83 slot1: 84 slot2: 85  
16:42:54.493 -\> slot0: 82 slot1: 83 slot2: 84  
16:42:54.664 -\> slot0: 81 slot1: 82 slot2: 83  
16:42:54.803 -\> slot0: 80 slot1: 81 slot2: 82  
16:42:54.960 -\> slot0: 79 slot1: 80 slot2: 81  
16:42:55.117 -\> slot0: 78 slot1: 79 slot2: 80

Juding by the numbers it is moving the body parts of the snake in the correct manner.

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [July 26, 2023, 5:10pm UTC](https://forum.arduino.cc/t/neopixel-stops-refreshing-after-array-usage/1151030/11 "2023-07-26T17:10:45Z")

</div>

> [@derbrummer](#):
>
> I want to use a 2 dimensional array to store the information on where the snakes body parts are at to start makeing the snake longer.

Unless you have collisions, a snake doesn't need that. You only need to store the head and tail position. Each iteration, you add a head pixel and delete a tail pixel.

---

<div class="post-metadata">

**Author:** ![derbrummer](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/derbrummer/32/978285_2.png) [@derbrummer](https://forum.arduino.cc/u/derbrummer)\
**Post date:** [July 26, 2023, 5:27pm UTC](https://forum.arduino.cc/t/neopixel-stops-refreshing-after-array-usage/1151030/12 "2023-07-26T17:27:16Z")

</div>

True, but the snake needs collision with itself. An im guessing the Array should not be the problem.

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [July 26, 2023, 5:34pm UTC](https://forum.arduino.cc/t/neopixel-stops-refreshing-after-array-usage/1151030/13 "2023-07-26T17:34:20Z")

</div>

For that, yes you need to store all positions. You will need a circular buffer.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [January 22, 2024, 5:34pm UTC](https://forum.arduino.cc/t/neopixel-stops-refreshing-after-array-usage/1151030/14 "2024-01-22T17:34:52Z")

</div>

Closed on January 22, 2024, 5:34pm UTC

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