PUSHBUTTON+SWITCH CASE

Hi,

I am developing a project for my college.

PROJECT OUTLINE:
There are 3 sensors (Temp., Accelerometer & Humidity) will read the respective parameters continuously. in between if I press ‘PushButton1’ for 2 seconds the Arduino must enter into programming mode.

The programming mode has 6 Switch cases. Each case gets selected one after one on the even of ‘PushButton1’ gets pressed for short time.

Also if i press ‘PushButton1’ again for 2 seconds in Programming mode the Arduino should come out of the programming mode.

PROBLEM:
In my current sketch I have to press ‘PushButton1’ for 2 seconds in Programming Mode for 2 seconds to change the case. Also I don’t understand how to get out of the Switch Case loop.

The sketch is attached with this topic.

PRESS_BUTTON_8.ino (1.32 KB)

swa8788:
Hi,

I am developing a project for my college.

PROJECT OUTLINE:
There are 3 sensors (Temp., Accelerometer & Humidity) will read the respective parameters continuously. in between if I press 'PushButton1' for 2 seconds the Arduino must enter into programming mode.

The programming mode has 6 Switch cases. Each case gets selected one after one on the even of 'PushButton1' gets pressed for short time.

Also if i press 'PushButton1' again for 2 seconds in Programming mode the Arduino should come out of the programming mode.

PROBLEM:
In my current sketch I have to press 'PushButton1' for 2 seconds in Programming Mode for 2 seconds to change the case. Also I don’t understand how to get out of the Switch Case loop.

const int btn = 2;
const int ledPin = 13;
int selector = 0;
boolean volatile wait = false;
const unsigned long event=2000;
unsigned long a=0;
unsigned long b=0;
unsigned long c=0;
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(btn, INPUT);
attachInterrupt(digitalPinToInterrupt(btn),decoder,CHANGE);
}

void loop()
{
while(wait) { }
if (digitalRead(btn) == LOW) {
a = millis();
}
else
{
b = millis();
c=b-a;
Serial.println(c);
if (c>=event)
{
doSwitchStatement();
selector++;
if (selector >6)
{
selector = 0;
delay(500);
}
}
}
wait = true;
interrupts();

}

void decoder()
{
noInterrupts();
wait = false;
}

void doSwitchStatement()
{
switch(selector)
{
case 0:
Serial.println("Case 0");
break;

case 1:
Serial.println("Case 1");
break;

case 2:
Serial.println("Case 2");
break;

case 3:
Serial.println("Case 3");
break;

case 4:
Serial.println("Case 4");
break;

case 5:
Serial.println("Case 5");
break;

case 6:
Serial.println("Case 6");
break;

}
}

You’re not getting stuck in the case statement. You’re getting stuck here....

 while(wait) {  }

That just loops forever if wait == true
That gets set the first time through your loop function.

Why are you using interrupts for a push-button?
Does it have hardware debounce?

Please remember to use code tags when posting code