How to on/off light dimmer using another pin ?

In recent project, I'm working with light dimmer.

So basically for that I made a code. For that, I used pin number 9 which is PWM pin means provide PWM value 0-255. Now, I want to dim my light and vise versa. For that I used TWO PUSH BUTTON. For that, I assigned pin 6 and 7 of Arduino. PUSH BUTTON connected to PIN 6 increase the value of PWM and PUSH BUTTON connected to PIN 7 decrease the value of PWM. Here, range of PWM is 0-255. So, I just divide this into 5 portion like 0, 64, 128, 192, 255. In initially when program start,the value of variable i is 0 then PWM pin 9 has a PWM value 0. Now, When I pressed PUSH BUTTON(PIN 6) then it's increase the value of variable i. So now value of i is 1 and PWM pin 9 has a PWM value 64. And again pressed PUSH BUTTON(PIN 6) then again increase the value of i and again PWM value change. It's happened until i reach maximum value means i = 4 and if I pressed again then again increase value of i, but I provide condition so that maximum value of i remain 4. And Same things happened when we pressed PUSH BUTTON(PIN 7) then value of i decreased and also value of PWM decreased. So, this is just dimming light and vise versa.

This is code portion which perform above operation.

if(digitalRead(kPinSW1) == LOW) // Pressed PUSH BUTTON to increase PWM value
{
   i++;

   if(i > 4)
   {
      i = 4;
   }

   while(digitalRead(kPinSW1) == LOW); //Until PUSH BUTTON pressed
}

if(digitalRead(kPinSW2) == LOW) // Pressed PUSH BUTTON to decrease PWM value
{
   i--;

   if(i < 0)
   {
      i = 0;
   }

   while(digitalRead(kPinSW2) == LOW); //Until PUSH BUTTON pressed
}

switch (i)
{
      case 0:
              analogWrite(kPinPWM, 0); // When Button pressed not pressed send 0 value
      break;

      case 1:
              analogWrite(kPinPWM, 64); // When Button pressed and i = 1 send 64 value
      break;

      case 2:
              analogWrite(kPinPWM, 128); // When Button pressed and i = 2 send 128 value
      break;

      case 3:
             analogWrite(kPinPWM, 192); // When Button pressed and i = 3 send 192 value
      break;

      case 4:
             analogWrite(kPinPWM, 255); // When Button pressed and i = 4 send 255 value
      break;
}

Until now, my code working well. So, now in further step, I have to attached one PUSH BUTTON which able to ON/OFF the PWM pin 9. For that, I assigned one PUSH BUTTON to PIN 10. Now, when program start, then no operation perform on PWM pin 9. When I press PUSH BUTTON(PIN 10) then and then all dimmer and vise versa operation perform. That mean when I press PUSH BUTTON first time, it's ON PWM pin 9 and then all operation perform. And when I again press button, then it's OFF pin 9.

Now for that, I used temp flag. Means initially flag value is 0. So, I just create condition like when PUSH BUTTON(PIN 10) pressed and checked flag value. If the flag value is 0 then PWM pin 9 going to be ON. And now here change flag value to 1. So, now again PUSH BUTTON(PIN 10) pressed then it's check value of flag. So, If the flag value is 1 then PWM pin 10 going to be OFF. And now here change flag value to 0. Here, for ON/OFF the PWM pin 9, I used HIGH/LOW logic. HIGH means ON and LOW means OFF.

Now, when I run a program and perform this things, I'm not able to perform anything. No dimmer and vise versa operation and not able to ON/OFF PWM pin 9.

Here, it's my full code.

int kPinPWM = 9; // This pin provides PWM values

int kPinSW1 = 6; // PUSH BUTTON // This pin used for increase PWM values
int kPinSW2 = 7; // PUSH BUTTON // This pin used for decrease PWM values

int kPinSw = 10; // PUSH BUTTON // This pin used for ON/OFF PWM pin means ON/OFF pin 9

int i = 0;  

int temp = 0; // Flag

void setup()
{
  pinMode(kPinPWM, OUTPUT);

  pinMode(kPinSW1, INPUT);
  pinMode(kPinSW2, INPUT);

  digitalWrite(kPinSW1, HIGH);
  digitalWrite(kPinSW2, HIGH);

  pinMode(kPinSw, INPUT);

  digitalWrite(kPinSw, HIGH);
}

void loop()
{
  if(digitalRead(kPinSw) == LOW && temp == 0) //If I pressed PUSH BUTTOPN(PIN 10) and temp = 0
  {
    temp = 1; // Change value to 1 // temp = 1

    digitalWrite(kPinPWM, HIGH); // Turn ON PWM pin (PIN 9)

    if(digitalRead(kPinSW1) == LOW) // Pressed PUSH BUTTON to increase PWM value
    {
      i++;

      if(i > 4)
      {
        i = 4;
      }

      while(digitalRead(kPinSW1) == LOW); //Until PUSH BUTTON pressed
    }

    if(digitalRead(kPinSW2) == LOW) // Pressed PUSH BUTTON to decrease PWM value
    {
      i--;

      if(i < 0)
      {
        i = 0;
      }

      while(digitalRead(kPinSW2) == LOW); //Until PUSH BUTTON pressed
    }

    switch (i)
    {
      case 0:
              analogWrite(kPinPWM, 0); // When Button pressed not pressed send 0 value
      break;

      case 1:
              analogWrite(kPinPWM, 64); // When Button pressed and i = 1 send 64 value
      break;

      case 2:
              analogWrite(kPinPWM, 128); // When Button pressed and i = 2 send 128 value
      break;

      case 3:
             analogWrite(kPinPWM, 192); // When Button pressed and i = 3 send 192 value
      break;

      case 4:
             analogWrite(kPinPWM, 255); // When Button pressed and i = 4 send 255 value
      break;
    }
  }

  if(digitalRead(kPinSw) == LOW && temp == 1) //If I pressed PUSH BUTTOPN(PIN 10) and temp = 1
  {
    temp = 0; // Change value to 0 // temp = 0

    digitalWrite(kPinPWM, LOW); //Turn OFF PWM pin (PIN 9)
  }
}

Some suggestions

Don't detect when a button is pressed, detect when it becomes pressed. See the StateChangeDetection example in the IDE. This removes the need for while loops which is what is causing your current problem I suspect.

Put the PWM values in an array and use the value of i as the index to the array. This will reduce the size of your code considerably and make it easier to understand.

Use meaningful names for the variables.

UKHeliBob:
Don't detect when a button is pressed, detect when it becomes pressed. See the StateChangeDetection example in the IDE. This removes the need for while loops which is what is causing your current problem I suspect.

Thank you so much. It's work like a charm. I will upload my code.

UKHeliBob:
Put the PWM values in an array and use the value of i as the index to the array. This will reduce the size of your code considerably and make it easier to understand.

Use meaningful names for the variables.

Thanks. I will keep this things in my mind.

Here, it's my code. It's working very fine. ON/OFF pwm pin and also increase/decrease PWM value.

const int buttonPin1 = 2;          
const int buttonPin2 = 3;
const int buttonPin3 = 4;

const int pwmPin = 9;

const int onOffPin = 10;

int buttonState1 = 0;         
int lastButtonState1 = 0;

int buttonState2 = 0;         
int lastButtonState2 = 0;

int buttonState3 = 0;         
int lastButtonState3 = 0;

int buttonPushCounter = 0;

int onOffCounter = 0;

void setup()
{
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin1, INPUT);
  
  pinMode(pwmPin, OUTPUT);

  pinMode(onOffPin, OUTPUT);
  
  Serial.begin(9600);
}

void loop() 
{
  buttonState3 = digitalRead(buttonPin3);

  if (buttonState3 != lastButtonState3)
  {
    if (buttonState3 == HIGH)
    {
      onOffCounter++;
      Serial.println("Button 3 on");
      Serial.print("number of button 3 pushes:  ");
      Serial.println(onOffCounter);
    }
    else 
    {
      Serial.println("Button 3 off");
    }
    delay(50);
  }
  
  lastButtonState3 = buttonState3;

  if(onOffCounter % 2 == 0)
  {
    Serial.println("Switch is OFF");

    analogWrite(pwmPin, 0);
  }
  else
  {
    PWM_fun();

    Serial.println("Switch is ON");
  }
}

void PWM_fun()
{
  buttonState1 = digitalRead(buttonPin1);

  if (buttonState1 != lastButtonState1)
  {
    if (buttonState1 == HIGH)
    {
      buttonPushCounter++;
      Serial.println("Button 1 on");
      Serial.print("number of button 1 pushes:  ");
      Serial.println(buttonPushCounter);

      if(buttonPushCounter > 4)
      {
        buttonPushCounter = 4;
      }
    }
    else 
    {
      Serial.println("Button 1 off");
    }
    delay(50);
  }
  
  lastButtonState1 = buttonState1;

  buttonState2 = digitalRead(buttonPin2);

  if (buttonState2 != lastButtonState2)
  {
    if (buttonState2 == HIGH)
    {
      buttonPushCounter--;
      Serial.println("Button 2 on");
      Serial.print("number of button 2 pushes:  ");
      Serial.println(buttonPushCounter);

      if(buttonPushCounter < 0)
      {
        buttonPushCounter = 0;
      }
    }
    else 
    {
      Serial.println("Button 2 off");
    }
    delay(50);
  }
  
  lastButtonState2 = buttonState2;

  switch (buttonPushCounter)
  {
      case 0:
              analogWrite(pwmPin, 0);
      break;

      case 1:
              analogWrite(pwmPin, 64);
      break;

      case 2:
              analogWrite(pwmPin, 128);
      break;

      case 3:
              analogWrite(pwmPin, 192);
      break;

      case 4:
              analogWrite(pwmPin, 255);
      break;
  }
}

Good that it works, but you can tidy it up and perhaps learn something.

Replace the switch/case lines in the program with these 2 lines

  const byte pwmValues[] = {0, 64, 128, 192, 255};
  analogWrite(pwmPin, pwmValues[buttonPushCounter]);

Also consider changing the data types of variables to the smallest applicable data type. For instance, do pin numbers need to be int or would byte be large enough ? The program will work either way but using variables of an appropriate type is usually regarded as good practice, can save memory and speed up program execution in some cases.