Storing value

Hi

I´m using a Thumb Joystick to move some geometric figures in a 32X16 LED matrix.
I have this for loop where I run through the 16 dots of the LED matrix but since I´m always initializing "i" as cero, I´m stuck in that loop.

I have been trying to store a value so that it stays in that coordinate until I move the joystick instead of running the 16 values each time I move the joystick, any ideas on how to get out of this loop/storing a value that gets refreshed each time I move the joystick?

Here is the code:

void loop() {
      
    analogRead(0);
    analogRead(1);
    digitalRead(3);
    int mapY,i;
    int a[16]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
   
    
     mapY=map(analogRead(0),0,1023,0,16); 
     
     if(mapY>7){         
      for(i=0;i<16;i++){
           
    HT1632.drawTarget(BUFFER_BOARD(2));
    HT1632.clear();
    HT1632.drawImage(IMG_cuadro4X4, IMG_cuadro4X4_WIDTH,  IMG_cuadro4X4_HEIGHT, 20,a[i]);
    HT1632.render(); 
    
    Serial.print(a[i]);
    Serial.println();

      }   
} 
}

Thanks

Any variables that you put outside your loop() function will keep their values between executions of loop()

So you have a variable "the value of mapY last time I drew the sprite". You read analog0 into mapY, same as you do now, and go "if mapy is not the same sa the previous one, then draw the sprite and record this new map y as being the previous".

 analogRead(0);
    analogRead(1);
    digitalRead(3);

This does nothing. You "read" the value and then throw it away without recording it anywhere.

Why use map() when you could just test the value returned by analogRead() ?

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

An array of values where the ith value is i is a complete waste of SRAM.

I am not really clear what you want to do but if you need to detect that the Y value has changed by a certain amount you need to save the value each time that you read it and compare it with the previous value. If it differs by more than a certain value then act on it. If not then go round again.

void loop()
{
  newY = analogRead(0);
  if (newY > 512 && abs(newY - prevY) > 100)
    {
      //Y has changed so execute code here
    }
  prevY = newY;  //save the current Y value
}

Hey, thanks guys

It seems we have an improvement.
The values are stored in the variable "actPos", so when I move the joystick the sprite stays in the stored value.
The problem now is that every time I move again the joystick it starts from the first value, in this case 7.

void loop() {
 
   newY=map(analogRead(0),0,1023,0,16);    

  if (newY>7 && newY>actPos){
         
   HT1632.drawTarget(BUFFER_BOARD(2));
   HT1632.clear();
   HT1632.drawImage(IMG_cuadro4X4, IMG_cuadro4X4_WIDTH,  IMG_cuadro4X4_HEIGHT, 20,actPos);
   HT1632.render(); 
  }
   actPos=newY;
   Serial.print(actPos);
   Serial.println(); 
 }

PD: I´m mapping the values because that´s the dimension of the LED matrix I´m using.