delay of 2 seconds in switch case with pushbutton

hi everyone,
anybody can help me to add a delay of 2 seconds in the case 2 of below code.
i added delay function but it pauses all next codes. i need to add delay for output1 and 2, low state for 2 seconds then HIGH. next will be the case 3.
plz help how to add the delay without delay function.

int input = 2;
int output1 = 3;
int output2 = 4;
int output3 = 8;
int output4 = 9;
int output5 = 10;
int output6 = 11;
int state = 0;
int old =0;
int buttonpoll=0;

void setup() {
pinMode(input, INPUT_PULLUP);
pinMode(output1, OUTPUT);
pinMode(output2, OUTPUT);
pinMode(output3, OUTPUT);
pinMode(output4, OUTPUT);
pinMode(output5, OUTPUT);
pinMode(output6, OUTPUT);

digitalWrite(output1, LOW);
digitalWrite(output2, LOW);
digitalWrite(output3, LOW);
digitalWrite(output4, LOW);
digitalWrite(output5, LOW);
digitalWrite(output6, LOW);

}

void loop() {

buttonpoll = digitalRead (input);
if (buttonpoll ==1){
delay (80);
buttonpoll = digitalRead (input);
if (buttonpoll==0){
state = old + 1;
}}

else
{
delay(100);}
switch(state){
case 1:
digitalWrite(output3, LOW);
digitalWrite(output4, HIGH);
digitalWrite(output5, LOW);
digitalWrite(output6, HIGH);
digitalWrite(output1, HIGH);
digitalWrite(output2, HIGH);
old = state;
break;
case 2:
digitalWrite(output3, HIGH);
digitalWrite(output4, LOW);
digitalWrite(output5, HIGH);
digitalWrite(output6, LOW);
digitalWrite(output1, LOW);
digitalWrite(output2, LOW);

//i need here a break of 2 seconds and continue the output1 and 2 High, also continue to next switch case.

digitalWrite(output1, HIGH);
digitalWrite(output2, HIGH);

old = state;
break;
default:

digitalWrite(output1,LOW);
digitalWrite(output2, LOW);
digitalWrite(output3, LOW);
digitalWrite(output4, LOW);
digitalWrite(output5, LOW);
digitalWrite(output6, LOW);
old = 0;
break;

}

}

 //i need here a break of 2 seconds
  delay(2000);

and continue the output1 and 2 High, also continue to next switch case.

But output1 and output2 are NOT HIGH. And, you don't "continue to the next switch case. That indicates that you do not understand what a switch statement means/does.

You should probably be using if statements, with else or else if statements.

Please edit your post and add code tags around the code.

Type 
[code]before your code
Type [/code]
 after your code

One solution is to add a case 3 and a case 4. After you have set the outputs in case 2, go to case 3 and 'wait'. Once the 'wait' is over, go to case 4 and set the two outputs.

You need a millis() based approach in case 3 if you want to keep your code responsive.

You're designing / writing a statemachine. Usually the state determines what happens when conditions (e.g. input, delay lapsed) change; you're currently doing that unconditionally in the beginning of loop when you read the button. So the first change I would make is not changing the state there but set a flag indicating that a button was pressed. In the cases you can react on that flag if needed.

You also might want to draw out your complete statemachine. Something like below (to give you the idea)

topic538121.0x.jpg

Hello dear,

thanks you very much for your reply. I get your point but as I m new the coding area unable to put your
suggestion in the code. plz inform how could i move forward

thanks

Here is an example code of Blink without delay: Blink without delay

You must read it and follow through, I think this is what you are searching for.

you can also read the sticky on top of Programming questions "Using millis() for timing. A beginners guide"

Good luck :slight_smile: