Storing a value

I'm trying to use a joystick to dim a LED. This part works fine. The idea is to use the push button for on/off. The idea is when I push the button the value of brightness is stored in the variable brightnessMemory and them brightness=0. But for some reason that I don't understand the value of brightnessMemory goes back to 0. What I'm doing wrong?

Thanks

int xPin = A1;
int yPin = A0;
int buttonPin = 2;

//*********************************
int led = 7;           // the PWM pin the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by
int brightnessMemory;
//*********************************




int xPosition = 0;
int yPosition = 0;
int buttonState = 0;

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 

//*********************************
 // declare pin 9 to be an output:
  pinMode(led, OUTPUT);
//*********************************

  
  pinMode(xPin, INPUT);
  pinMode(yPin, INPUT);

  //activate pull-up resistor on the push-button pin
  pinMode(buttonPin, INPUT_PULLUP); 
  
  // For versions prior to Arduino 1.0.1
  // pinMode(buttonPin, INPUT);
  // digitalWrite(buttonPin, HIGH);
  
}

void loop() {
  xPosition = analogRead(xPin);
  yPosition = analogRead(yPin);
  buttonState = digitalRead(buttonPin);

//*********************************
// set the brightness of pin 9:
  analogWrite(led, brightness);
  //analogWrite(led, 255);
//*********************************
  
  

  
if (yPosition >=540)  {
    brightness=brightness+5;
  }

if (yPosition <=500)  {
    brightness=brightness-5;
  }
  
if (brightness >=255) {
  brightness=255;
}

if (brightness <=0) {
  brightness=0;
}
Serial.print("X: ");
Serial.print(xPosition);
Serial.print(" | Y: ");
Serial.print(yPosition);
Serial.print(" brightness: ");
Serial.print(brightness);
Serial.print(" | Button: ");
Serial.print(buttonState);
Serial.print(" memory ");
Serial.print(brightnessMemory);
Serial.println("");


//-------------------------------------------------------PROBLEM?--------------------------
if (buttonState == 0) {
brightnessMemory=brightness;
brightness=0;
//-------------------------------------------------------PROBLEM?--------------------------


}
delay(100); // add some delay between reads


}

jucasan:
What I'm doing wrong?

_Assuming _ you want to save the brightness value when the Arduino is shut off, you need to understand that calling something brightnessMemory does not mean it will be saved when power goes away. It's like any other variable stored in RAM, it's lost when power is removed.

You need to store the value of brightnessMemory in the onboard EEPROM before shutdown and then retrieve that value upon powerup, usually in setup().

I sort of find what the problem was. If I hold the push button the program stays in a loop turning brightnessMemory to 0.

How can I replace this part of code just to read the button once?

Thanks

if (buttonState == 0) {
brightnessMemory=brightness;
brightness=0;
}

jucasan:
How can I replace this part of code just to read the button once?

In the IDE look under file/examples/digital/state change detection. What has to happen is to detect when the switch *becomes * pressed/unpressed. You use the resulting indirect flag value, which is true for only one scan through loop(), to increment, switch, or any number of other one-shot programming tasks.

see below, some other safe place in your code you have to set initialize back to 0 to enter the if statement again.

if (buttonState == 0 & initialize==0) {
brightnessMemory=brightness;
brightness=0;
initialize=1;
}

Thank you guys. Need to do some experimentation now.