"freeze" a variable

Hello,
I would like to know how one can "freeze" a variable.
Let me explain.
Imagine I have a potar and a button. I would like each time I press the button, the current value of the potar is placed in a variable (or a constant) and does not change even if I change the value of the potar.
How can we do that?

if (buttonPressed) var = analogRead(potar);

Assuming a potar is a potentiometer

Your code goes line by line. A variable won't change until it hits a line that has that variables name followed by an equal sign followed by whatever value you want to set it to. Or until you use the ++ or -- operator on it. So if you only want the value of the variable to change when you press the button, then put the line of code that sets that variables value inside an if statement that checks to see if the button is pressed. If you only want it to change once for each press then you need to detect state change instead of just the press. I think a visit to the "State Change Example" included in the IDE would be good for you. It deals with counting on button presses. But you could easily replace the code that increments the variable with code that reads your pot.

Thank you for this quick response.
But if I want to press several times to keep several values in several variables, is it possible?

If you can tell us how to do it, you can program it into an Arduino.

Yes. Keep track of the number of times it was pressed. Pressed once, save to var1, twice var2, etc.

Definitely want to debounce .

evanmars:
Yes. Keep track of the number of times it was pressed. Pressed once, save to var1, twice var2, etc.

Definitely want to debounce .

Almost. Use an array. Save them in var[0], var[1], var[2] ...

Thank you very much

Delta_G:
Almost. Use an array. Save them in var[0], var[1], var[2] ...

Is there any way an array's size can be dynamic?- it needs to have the size known when it's declared, which means you have to know or guess how many elements will be collected.

manor_royal:
Is there any way an array's size can be dynamic?- it needs to have the size known when it's declared, which means you have to know or guess how many elements will be collected.

C++ vectors are dynamic, but not implemented on AVR gcc, I guess because of the issues related ot the tiny memory footprint.

You can create a container for a "variable array" but just allocate the maximum size of the array at compile time.

@manor-sorry can't quote

In reality it doesn't matter. Since you have such limited memory on the micro, even if you don't allocate the memory to the array you'll still have to decide how much memory the rest of the program should leave for it. There exists a maximum size that it could be before it crashes the rest of the program. At that point you might as well just go ahead and allocate that size. You can have a variable to keep up with how much you actually used.

I'm wondering what OP's intention is here.

Presumably not datalogging, since volatile memory's not much good for that.

Maybe planning on a running average? If that's the case have a look at the RunningAverage library in the Playground.