While loop won't stop after statement is false using a joystick and 8x8 matrix

I am brand new to arduino, and I am building a project for a class. The project is a digital die involving a joystick and 8x8 LED matrix. How I want it to work is when the joystick is moved in any direction or the button is pressed, the led runs through an animation and displays a random number 1-6 immediately after and holds that number on the LED screen until the joystick is hit again. Then the process repeats.

Right now, when the joystick is hit, the animation and random number run perfectly, but immediately following the random number, the animation starts over, so essentially, even when the condition is false, the while loop is still continuing.

I have tried using break, continue, etc. None of it worked. Please help.

Here is my code–most of the top is just the animation bytes:

#define joyX A0
#define joyY A1
int button=2;
int buttonState = 0;
int buttonState1 = 0;
#include <LedControl.h>
long randnumber;
const int DIN_PIN = 7;
const int CS_PIN = 6;
const int CLK_PIN = 5;
******Byte information here deleted to not exceed post max characters******
const int images_LEN = sizeof(images)/8;


LedControl display = LedControl(DIN_PIN, CLK_PIN, CS_PIN);

void setup() {
   pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
  pinMode(button,INPUT);
  digitalWrite(button, HIGH);
    randomSeed(analogRead(0));
  display.clearDisplay(0);
  display.shutdown(0, false);
  display.setIntensity(0, 5);
  Serial.begin(9600);


}

void displayImage(const byte* image) {
  for (int i = 0; i < 8; i++) {
    for (int j = 0; j < 8; j++) {
      display.setLed(0, i, j, bitRead(image[i], 7 - j));
    }
  }
}

int i = 0;

void loop() {
 int xValue = analogRead(joyX);
 int yValue = analogRead(joyY);
 
  
  buttonState = digitalRead(button);
while(xValue>=515 || xValue<=500 || yValue>=515 || yValue<=500 || buttonState==LOW)
{
    Serial.print(xValue);
  Serial.print("\t");
  Serial.println(yValue);
  Serial.println(buttonState);
    displayImage(images[i]);
   delay(100);
  if (++i >= images_LEN ) {
    i = 0;
    delay(100);
  randnumber = random(1,7);
 // Serial.println(randnumber);
if(randnumber == 1){        //Display Random Number 1
  displayImage(one[i]);
  Serial.println(randnumber);
  delay(500);}
  else
  if(randnumber == 2){        //Display Random Number 2
    displayImage(two[i]);
    Serial.println(randnumber);
  delay(500);}
  else
  if(randnumber == 3){        //Display Random Number 3
    displayImage(three[i]);
    Serial.println(randnumber);
  delay(500);}
  else
  if(randnumber == 4){        //Display Random Number 4
    displayImage(four[i]);
    Serial.println(randnumber);
  delay(500);}
  else
  if(randnumber == 5){       //Display Random Number 5
    displayImage(five[i]);
    Serial.println(randnumber);
  delay(500);}
  else
  if(randnumber == 6){       //Display Random Number 6
    displayImage(six[i]);
    Serial.println(randnumber);
  delay(500);}
  break;
  while (xValue<515 || xValue>500 || yValue<515 || yValue>500 || buttonState==HIGH){}
  
}

}

}

You’re not reading the joysticks inside the while() so once the conditions to go in there are satisfied, the values never change so it can’t get out.

I don’t think your code compiles, or you didn’t post it entirely. You use arrays that were never defined (such as image, one, two, three…). But maybe those are the bitmaps of the numbers on the dice so you didn’t add them for simplicity.

I think you should first make your code work only for the button press. Then when it works, add the joystick.

Also, you should use an array of arrays to define the bitmaps instead of 6 different arrays. This would be an array of dimension 6x8x8. Then you would get rid of this (ugly) series of ifs…

Then your loop would be simpler:

void loop() {
  buttonState = digitalRead(button);
  delay (30); // debounce
  while(buttonState==LOW)
  {
    Serial.println(buttonState);
    randnumber = random(1,7);
    for (byte k = 0; k< randnumber ; k++) 
    {
        byte side[8][8] = {0};
        for (k1 = 0;k1<8;k1++)  for (k2 = 0;k2<8;k2++) side[k1][k2] = image[k][k1][k2];
        displayImage(side);
        delay(500);
     }
     buttonState = digitalRead(button);
   }
}

And you need to declare and init the array image[6][8][8] at the beginning of the code.

@neiklot

How do I make it read the joystick in the while loop?

ddecker21: How do I make it read the joystick in the while loop?

Put these inside?

xValue = analogRead(joyX);
yValue = analogRead(joyY);

I inserted those into the code and now I have to hold the joystick down for the animation to run \

Here is my void loop code:

void loop() {
 int xValue = analogRead(joyX);
 int yValue = analogRead(joyY);
 
  buttonState = digitalRead(button);
while(xValue>=515 || xValue<=500 || yValue>=515 || yValue<=500 || buttonState==LOW)
{
  xValue = analogRead(joyX);
yValue = analogRead(joyY);
  buttonState = digitalRead(button);
    Serial.print(xValue);
  Serial.print("\t");
  Serial.println(yValue);
  Serial.println(buttonState);
    displayImage(images[i]);
   delay(100);
  if (++i >= images_LEN ) {
    i = 0;
    delay(100);
  randnumber = random(1,7);
 // Serial.println(randnumber);
if(randnumber == 1){        //Display Random Number 1
  displayImage(one[i]);
  Serial.println(randnumber);
  delay(500);}
  else
  if(randnumber == 2){        //Display Random Number 2
    displayImage(two[i]);
    Serial.println(randnumber);
  delay(500);}
  else
  if(randnumber == 3){        //Display Random Number 3
    displayImage(three[i]);
    Serial.println(randnumber);
  delay(500);}
  else
  if(randnumber == 4){        //Display Random Number 4
    displayImage(four[i]);
    Serial.println(randnumber);
  delay(500);}
  else
  if(randnumber == 5){       //Display Random Number 5
    displayImage(five[i]);
    Serial.println(randnumber);
  delay(500);}
  else
  if(randnumber == 6){       //Display Random Number 6
    displayImage(six[i]);
    Serial.println(randnumber);
  delay(500);}
 
  
  
  }

Does holding it down make it satisfy the conditions that take you into the while()?

You can't have it both ways....

If you have a set of conditions which when met take you into a while(), and don't change them therein, it will never leave. If you do change things so the conditions are no longer met, then it will leave.

If those conditions cause the animation to run (I assume that's the stuff inside the while()?) then what conditions cause it to stop? As it is now, you have to maintain those conditions by holding the joystick down, since that's what caused the animation to start in the first place.

How else could you tell it to stop the animation than by releasing the joystick?