Arduino Atmega8 PWM problem

Hi all :slight_smile:
I made a white LED strip PWM dimmer controled by RC5 , and i have a problem with PWM.
Here is my code :

#include <IRremote.h>

byte RECV_PIN = 13;

int bri ;
byte led = 11;

#define UP      0xFFA05F
#define DOWN    0xFF40BF
#define OFF      0xFFB24D
#define ON       0xFF6897


IRrecv irrecv(RECV_PIN);

decode_results results;


void setup() {
  bri = 0;
  
  irrecv.enableIRIn();
  pinMode(led, OUTPUT);


}

void loop() {
  if (irrecv.decode(&results)) {
    if ( results.value != 0xFFFFFFFF) {
      switch (results.value){

      case ON :

        for(bri = bri;  bri <= 254; bri+=1){
          analogWrite(led, bri);
         delay(800);
         if(bri==254){
           bri= 254;
           analogWrite(led,bri);
         }
        }


        break;

      case OFF :
        for(bri = bri;  bri >=0; bri -=1){
          analogWrite(led, bri);
          
          delay(500);
         if(bri == 0){
           bri = 0;
           analogWrite(led, bri);
         }
                   
        }

        break;


      case UP    :
        bri = bri + 10;
        analogWrite(led,bri);
        if (bri > 255){
          bri = 254;
          analogWrite(led,bri);
        }
        break;

      case DOWN  :
        bri = bri - 10;
        analogWrite(led, bri);
        if(bri < 5){
          bri = 10;
          analogWrite(led,bri);
        }
        break;

      }
    }

    irrecv.resume();
  }

}

And my problem is that when i first time power on the leds it fades in to full brightness and when i power off it fades out to off and its ok but when i press power on again its not starting from zero brightness but its give a full brigthness flash and then starts to fade in. How to get rid off that stupid blink ?

I think when you fade out you are leaving 'bri' set to -1. That looks like 255 when you send it to analogWrite().

Hmm when i fade in its incrementing bri to 254 and when it goes to 254 it should save it to bri varialble, and when i power off i take bri variable and decrement it to 0 ,and it work. But when i try to power it up again it should remember last state that is 0 and should increment it from 0 but it behave as it give a shot of 255 value to led then its start to fade in . Im confused now what i have to change to get it working?

But when i try to power it up again it should remember last state that is 0 and should increment it from 0 but it behave as it give a shot of 255 value to led then its start to fade in . Im confused now what i have to change to get it working?

You need to change your fundamental misunderstanding of how an Arduino works. "It should remember last state" is rubbish. If you want the Arduino to remember something, YOU must make that happen. You could, for instance, store a value in EEPROM, and then read from EEPROM in setup().

But, don't expect the Arduino to do that automatically.

szychor:
I'm confused now. What do I have to change to get it working?

When you are done with the fade out, set 'bri' to 0.