Problem decreasing LED brightness

I'm a beginner, both in electronics and Arduino, so sorry if I say stupid things. Plese, may you help me?
This is my 2nd project. Simply, I'm trying to change the brightness of a led using a IR controller. When user press a button, I check if led is on. If so, I read current led value and I decrease it one unit. This is my code:

if (digitalRead(redLed) == 1) 
      Serial.println("Red led On");
      currentValue = analogRead(redLed);
          Serial.println("Current value: " + String(currentValue));
          analogWrite(redLed, currentValue - 1);
          Serial.println("New value: " + String(analogRead(redLed)));

And this is serial monitor result:

//first button press
Red led On
Current value: 490
New value: 489

//second button press
Red led On
Current value: 296
New value: 297

1, Why 2nd time a I press the button, current value is 296 and not 489?

  1. Why new value is 297 and not 295?? (296-1)


  1. you are using the analogRead() function, the value will be changed, and even unpredictable if you do not connect the input pin to a sensor.
  2. The value from analogRead() is obviously different from analogWrite(). These function are do unrelated things (read voltage and write PWM, respectively)

AnalogWrite is a badly named function. It generates PWM that works like this
It can only be used in PWM capable pins, denoted by a ~ in front of the pin number.
The range you can use to set the brightness is 0 to 255.

AnalogRead will read an analogue voltage on a pin. It can only be used on pins that can be changed into an analogue input. It takes the voltage range 0 to 5V and returns a number from 0 to 1023.

No pin on a Uno or Nano is capable of both functions. Even if it were you could not use both functions one after the other like you are trying to do. You have to keep the brightness number in a variable, increment that variable and write it out to the LED. You can not read the brightness of an LED like you are trying to do.

When you post code please post it all. An isolated piece of code is not capable of telling the whole story. In your case we don’t know what pin you are using.

Thankyou for your answer guys. Today I try to use a variable to encrease and decrease led brightness.