Short and Long duration controls with a single button

I am programming if a button is pressed for less than 5 Secs then it should turn LED off for 1 sec and if the button is pressed for more than 5 secs then the LED should be permanently off. Please check the code attached.

int ledPin = 13;
int switchPin = 3;
unsigned long longDuration = 5000;
unsigned long switchTime = 0,pressLength=0;
int switchStateFlag=0;

void setup() {
pinMode(switchPin, INPUT);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin,HIGH);
Serial.begin(9600);
}

void loop(){
while(digitalRead(switchPin)==HIGH)
{
delay(10);
pressLength = pressLength + 10;
//switchTime = millis();
switchStateFlag=1;}

if((pressLength<=longDuration) && switchStateFlag==1)
{
Serial.print("ms = ");
Serial.println(pressLength);

digitalWrite(ledPin, LOW);
delay(2000);
digitalWrite(ledPin,HIGH);
pressLength = 0;
switchStateFlag=0;
}

if (pressLength>longDuration && switchStateFlag==1)
{ Serial.print("ms = ");
Serial.println(pressLength);

digitalWrite(ledPin, LOW);
delay(2000);
pressLength = 0;
switchStateFlag=0;
}
}

Do you have a question?

Code tags?

niteshkumaronline:
Please check the code attached.

What happens when you run the program?

I suspect it is unlikely to work as you expect because it is using the delay() function. The functions delay() and delayMicroseconds() block the Arduino until they complete.
Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

...R

PS ... To make it easy for people to help you please modify your post and use the code button </>
codeButton.png

so your code looks like this and is easy to copy to a text editor. See How to use the Forum