button and switch

hi, i am trying to implement a button and a switch for driving one LED. The problem statement is : The button is used to turn on and off the led and switch is used to change the intensity of the led. if the switch is pressed it has one particular intensity and if it is released it has another particular intensity. the switch should work only when the button is on. the button is like if i press and release the the button once, it should turn on the led and if i gain press and release the button it should turn the LED off. I was trying to use the switch as an interrupt but the LED light flickers. I am stuck. i am using PWM output for varying the intensity of the LED. Can anyone please help me ? Thanks a lot :)

No code, no help.

#define DEBOUNCE_VALUE 20
#define MIN_PRESS_THRESHOLD 100
#define PRESS_THRESHOLD 1000
#define LED1 3
#define BUTTONPIN 4
#define DOORPIN 5

enum BUTTON_PRESS_TYPE{ NO_PRESS, BUTTON_ON, BUTTON_OFF};
enum DOOR_STATUS_TYPE{ DOOR_OPEN, DOOR_CLOSE};



BUTTON_PRESS_TYPE decideButton();
BUTTON_PRESS_TYPE decideB;

DOOR_STATUS_TYPE decideDoor();
DOOR_STATUS_TYPE decideD;


void setup()
{
  Serial.begin(9600);
  pinMode(LED1, OUTPUT);
  pinMode(BUTTONPIN, INPUT);
  pinMode(DOORPIN, INPUT); 
}

void loop()
{
  decideD=decideDoor();
  switch(decideD)//add current door mode
  {
    case DOOR_CLOSE: {
                       decideB=decideButton();
                       switch(decideB)// add cuurent button mode
                       {
                         case BUTTON_ON: close_and_on();
                                         Serial.print("on and close");
                                         break;
                         case BUTTON_OFF: door_is_off();
                                          Serial.print("off and close");
                                          break;
                       }
                       
                     }
                     break; 
    case DOOR_OPEN:  {
                       decideB=decideButton();
                       switch(decideB)// add cuurent button mode
                       {
                         case BUTTON_ON: open_and_on();
                                         Serial.print("on and open");
                                         break;
                         case BUTTON_OFF: oven_is_off();
                                          Serial.print("off and open");
                                          break;
                       }
                                             
                     }
                     break;
  }
}

BUTTON_PRESS_TYPE decideButton()
{
  unsigned long start=0;
  unsigned long finish=0;
  int old=HIGH;
  while(digitalRead(BUTTONPIN)==old)
  {}
  {
    start=millis();
    delay(DEBOUNCE_VALUE);
    old=LOW;
  }
  while(digitalRead(BUTTONPIN)==old)
  {}
  {
    finish=millis();
    delay(DEBOUNCE_VALUE);
    
  }
  unsigned long duration=finish-start; Serial.print(duration);Serial.print("\n");
  
  
  if(duration<PRESS_THRESHOLD && duration>=MIN_PRESS_THRESHOLD)
  {
    return BUTTON_OFF;
  }
  else if (duration >=PRESS_THRESHOLD)
  {
    return BUTTON_ON;
  }
}

DOOR_STATUS_TYPE decideDoor()
{
  if(digitalRead(DOORPIN)==HIGH)
  {
    return DOOR_CLOSE;
  }
  else if(digitalRead(DOORPIN)==LOW)
  {
    return DOOR_OPEN;
  }
  else
  {}
}

void open_and_on()
{
  analogWrite(LED1, 127);
}

void door_is_off()
{
  analogWrite(LED1, 0);
}

void close_and_on()
{
  analogWrite(LED1, 255);
}

thats the latest thing i tried for this so far

Moderator edit: CODE TAGS. I despair.

The problem statement is ........

Is this an assignment for your education?

Its just my free time thing i was trying to do. can you help me on this ?

I was trying to use the switch as an interrupt

I see no evidence of that in the code, and I see no reason to use interrupts at all.

The code looks very very complex for what you say you want to do. I am having difficulty in matching up waht you wrote with what you want.

So what does that code actually do and what do you want it to do?

Do you know that int old=HIGH means that the variable 'old' is destroyed when the function is exited and a new one created when the function is entered again. Should that not be a static int ?

DOOR_STATUS_TYPE decideDoor()
{
  if(digitalRead(DOORPIN)==HIGH)
  {
    return DOOR_CLOSE;
  }
  else if(digitalRead(DOORPIN)==LOW)
  {
    return DOOR_OPEN;
  }
  else
  {}
}

Hint: If a digital pin isn't HIGH, it's a pretty good bet that it's LOW - there's no need to read it again a couple of microseconds later.

DOOR_STATUS_TYPE decideDoor()
{
  return (digitalRead(DOORPIN)==HIGH) ? DOOR_CLOSE : DOOR_OPEN;
}