Convert from if/else statement to switch case

Hi guys,

I have an Arduino UNO, I have validated the below code works. What I want obtain in my outcome is that, whenever I pressed the switch once, the value changes. I have faced certain issues.

  1. In my coding, the loop just keeps on running even if I placed a delay in it. The delay does not stop at all in the Serial monitor.

  2. I tried switching from if/else statement to switch case instead but now, it cannot display on my Serial monitor.

  3. I have tried putting debounce statement into my coding but it still does not work at all.

Here are my codings:

sketch_jun15a.ino (1.52 KB)

Are we missing some braces?

lastDebounceTime = millis();
if ((millis() - lastDebounceTime) > debounceDelay) // need a { and a } somewhere

switch (buttonPin) {
case '(counter == 1)':
digitalWrite(11, LOW);
digitalWrite(12, LOW);
Serial.print("0");
Serial.println("0");
break;
. . .

What is this?
case '(counter == 1)':
. . .
case '(counter == 2)':

etc.

Because I tried using the codes below, it does not work at all.

case 'a':
digitalWrite(11, LOW);
digitalWrite(12, LOW);
Serial.print("0");
Serial.println("0");
break;
etc....

case '(counter == 1)':

For each "case" you need a unique integer value.

See:
http://www.arduino.cc/en/Reference/SwitchCase

You define buttonPin to be 10 and it never changes. Why then would you expect the statement:

switch (buttonPin) {

to ever evaluate to anything but 10? The statements similar to:

case '(counter == 1)':

I didn't even know would compile. However, since it appears to compile, the expression resolved to logic true when counter equals 1. Putting the logic true result in character quotes baffles me. However, none of them will ever be executed because buttonPin is alway 10 and counter cannot exceed 5, so there's no relevant case statement anyway.

Take a little time and read up on how a switch/case block works.

Is it possible to add Boolean and debounce in the coding??

JasonInstincto95:
Is it possible to add Boolean and debounce in the coding??

Yes, why?

aarg:
Yes, why?

Because I tried putting into my codes, but it doesnt managed to work at all. Plus there is certain parts in my codings seems to go wrong but I tried managed time, but I still cant locate my problem. I'm also unsure is it I have to place the debounce inside my counter to let it work.

int buttonState = LOW;//current state of the LED
unsigned long timeOfLastLedEvent = 0;//the last time the LED was updated
boolean currentState = LOW;//stroage for current measured button state
boolean lastState = LOW;//storage for last measured button state
boolean debouncedState = LOW;//debounced button state

int debounceInterval = 20;//wait 20 ms for button pin to settle
int intervalON = 1000;
int intervalOFF = 500;
int pin11 = 11;
int pin = 12;
int buttonPin = 10;
int counter = 0;
boolean running = false;

void setup() {
Serial.begin(9600);
pinMode(pin11, OUTPUT);
digitalWrite(11, buttonState);
pinMode(pin, OUTPUT);
digitalWrite(pin, buttonState);
pinMode(buttonPin, INPUT);
}

void loop()
{
//Handle input
buttonState = digitalRead(buttonPin);
// digitalRead(buttonPin);
if(buttonState == HIGH)
{

counter++;
//Reset count if over max mode number
if(counter == 5)
{
counter = 0;
}

}

//Change mode

{
if(counter == 1)
{

digitalWrite(11, LOW);
digitalWrite(12, LOW);
Serial.print("0");
Serial.println("0");
delay(1000); // delay to debounce switch
running = !running; // toggle running variable
digitalWrite(buttonPin, running); // indicate via LED

}
else if(counter == 2)
{
digitalWrite(11, LOW);
digitalWrite(12, HIGH);
Serial.print("0");
Serial.println("1");
delay(1000); // delay to debounce switch
running = !running; // toggle running variable
digitalWrite(buttonPin, running); // indicate via LED

}
else if(counter == 3)
{
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
Serial.print("1");
Serial.println("0");
delay(1000); // delay to debounce switch
running = !running; // toggle running variable
digitalWrite(buttonPin, running); // indicate via LED

}
else if(counter == 4)
{
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
Serial.print("1");
Serial.println("1");
delay(1000); // delay to debounce switch
running = !running; // toggle running variable
digitalWrite(buttonPin, running); // indicate via LED

}
}
unsigned long currentMillis = millis();
currentState = digitalRead(buttonPin);
unsigned long currentTime = millis();
if (buttonState == LOW){//if the LED is already off
if (currentMillis - timeOfLastLedEvent > intervalOFF){//and enough time has passed
digitalWrite(pin11, HIGH);//turn it on
buttonState = HIGH;//store its current state
timeOfLastLedEvent = currentMillis;//update the time of this new event
}
} else {//if the LED is already on
if (currentMillis - timeOfLastLedEvent > intervalON){
digitalWrite(pin, LOW);
buttonState = LOW;
timeOfLastLedEvent = currentMillis;
}
}
}

Please edit your last post to put the code inside code tags.

Also I see that you're using delay() with millis() in the same program. It's usually a recipe for disaster.

aarg:
Please edit your last post to put the code inside code tags.

Also I see that you're using delay() with millis() in the same program. It's usually a recipe for disaster.

So even if I remove delay and leave with millis(), must I placed inside counter??

Please edit and use code tags, as explained in the sticky threads at the top of the forum. I really don't know what you mean, "inside counter". To help you, it will be necessary to clarify your problems and objectives.

LarryD:
case '(counter == 1)':

For each "case" you need a unique integer value.

See:
http://www.arduino.cc/en/Reference/SwitchCase

check this

void setup()
{
  Serial.begin(115200);
  Serial.println("Start ");

  for (int i = 0; i < 100; i++)
  {
    Serial.print("10log(");
    Serial.print(i);
    Serial.print(") = ");
    switch (i)
    {
      case 0:
        Serial.println("error");
        break;
      case 1 ... 9:
        Serial.println("0,...");
        break;
      case 10 ... 99:
        Serial.println("1,...");
        break;
      case 100:
        Serial.println(2);
        break;
      default:
        break;
    }
  }
}

void loop()
{
}

My poor wording.
Yes you can indeed double up with a range of integers.
Same effect as having different cases with similar code.
The numbers within the range however, are unique though. :wink:

Counter == 1
Counter == 2
Etc.
These could give 0/1
Not sure if this even complies.

boolean currentState = LOW;//stroage for current measured button state

It is more common to assign values of true or false to boolean variables. You can do things differently but it can soon become confusing.

Using booleans does not save space if that is what you had in mind. Each boolean variable takes one byte even if its value can only be true or false. You might as well use byte variables. You can potentially save space by using the 8 bits of a byte to hold 8 separate true/false/HIGH/LOW states but it makes the code more complicated.

I have tried adding those solutions all of you provided, but it still cannot display values in my serial monitor? All my timings and readings shown on my mulitmeter are correct but it just cant display anything on my serial monitor.

int buttonState = LOW;//current state of the LED
unsigned long timeOfLastLedEvent = 0;//the last time the LED was updated
boolean currentState = LOW;//stroage for current measured button state
boolean lastState = LOW;//storage for last measured button state
boolean debouncedState = LOW;//debounced button state
int debounceInterval = 20;//wait 20 ms for button pin to settle
int intervalON = 1000;//how long we want the switch to stay on
int intervalOFF = 500;//how long we want the switch to stay off
int pin11 = 11; //Define arduino pin 11
int pin = 12; //Define arduino pin 12
int buttonPin = 10; //Define button pin 10
int counter = 0; //no. of counters
boolean running = false;
int var;

void setup() {
Serial.begin(9600);
pinMode(pin11, OUTPUT);
digitalWrite(11, buttonState);
pinMode(pin, OUTPUT);
digitalWrite(pin, buttonState);
pinMode(buttonPin, INPUT);
switch (var) {
case 1:
digitalWrite(11, LOW);
digitalWrite(12, LOW);
Serial.print("0");
Serial.println("0");
break;
case 2:
digitalWrite(11, LOW);
digitalWrite(12, HIGH);
Serial.print("0");
Serial.println("1");
break;
case 3:
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
Serial.print("1");
Serial.println("0");
break;
case 4:
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
Serial.print("1");
Serial.println("1");
break;
default:
break;
}
}

void loop()
{
//Handle input
buttonState = digitalRead(buttonPin);
if(buttonState == HIGH)
{

counter++;
//Reset count if over max mode number
if(counter == 5)
{
counter = 0;
}

}
}
/*{
if(counter == 1)
{
digitalWrite(11, LOW);
digitalWrite(12, LOW);
Serial.print("0");
Serial.println("0");
delay(1000); // delay to debounce switch
running = !running; // toggle running variable
digitalWrite(buttonPin, running); // indicate via buttonPin

}
else if(counter == 2)
{
digitalWrite(11, LOW);
digitalWrite(12, HIGH);
Serial.print("0");
Serial.println("1");
delay(1000); // delay to debounce switch
running = !running; // toggle running variable
digitalWrite(buttonPin, running); // indicate via buttonPin

}
else if(counter == 3)
{
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
Serial.print("1");
Serial.println("0");
delay(1000); // delay to debounce switch
running = !running; // toggle running variable
digitalWrite(buttonPin, running); // indicate via buttonPin

}
else if(counter == 4)
{
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
Serial.print("1");
Serial.println("1");
delay(1000); // delay to debounce switch
running = !running; // toggle running variable
digitalWrite(buttonPin, running); // indicate via buttonPin
}
}

{
unsigned long currentMillis = millis();
currentState = digitalRead(buttonPin);
unsigned long currentTime = millis();
if (buttonState == LOW){//if the buttonPin is already off
if (currentMillis - timeOfLastLedEvent > intervalOFF){//and enough time has passed
digitalWrite(pin11, HIGH);//turn it on
buttonState = HIGH;//store its current state
timeOfLastLedEvent = currentMillis;//update the time of this new event
}
} else {//if the buttonPin is already on
if (currentMillis - timeOfLastLedEvent > intervalON){
digitalWrite(pin, LOW); //turn it off
buttonState = LOW;
timeOfLastLedEvent = currentMillis;
}
}
}
}
*/

You still haven't used code tags.
You still have delay() functions.

  1. For the delay() functions, I have excluded out the part using /*

  2. For the code tags, it is found inside the switch case section.
    it is displayed by //..... etc.

int buttonState = LOW;//current state of the LED
unsigned long timeOfLastLedEvent = 0;//the last time the LED was updated
boolean currentState = LOW;//stroage for current measured button state
boolean lastState = LOW;//storage for last measured button state
boolean debouncedState = LOW;//debounced button state
int debounceInterval = 20;//wait 20 ms for button pin to settle
int intervalON = 1000;//how long we want the switch to stay on
int intervalOFF = 500;//how long we want the switch to stay off
int pin11 = 11; //Define arduino pin 11
int pin = 12; //Define arduino pin 12
int buttonPin = 10; //Define button pin 10
int counter = 0; //no. of counters
boolean running = false;
int var;

void setup() {
Serial.begin(9600);
pinMode(pin11, OUTPUT);
digitalWrite(11, buttonState);
pinMode(pin, OUTPUT);
digitalWrite(pin, buttonState);
pinMode(buttonPin, INPUT);
switch (var) {
case 1:
digitalWrite(11, LOW);
digitalWrite(12, LOW);
Serial.print("0");
Serial.println("0");
break;
//This is to display that if I pressed the push button once, on the Serial Monitor, it will display (0,0) only once.
case 2:
digitalWrite(11, LOW);
digitalWrite(12, HIGH);
Serial.print("0");
Serial.println("1");
break;
//This is to display that if I pressed the push button 2 times, on the Serial Monitor, it will display (0,1) only once.
case 3:
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
Serial.print("1");
Serial.println("0");
break;
//This is to display that if I pressed the push button 3 times, on the Serial Monitor, it will display (1,0) only once.
case 4:
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
Serial.print("1");
Serial.println("1");
break;
//This is to display that if I pressed the push button 4 times, on the Serial Monitor, it will display (1,1) only once.
default:
break;
//And if I pressed the push button once, it will return back to (0,0) and the loop continues
}
}

void loop()
{
//Handle input
buttonState = digitalRead(buttonPin);
if(buttonState == HIGH)
{

counter++;
//Reset count if over max mode number
if(counter == 5)
{
counter = 0;
}

}
}
/*{
if(counter == 1)
{
digitalWrite(11, LOW);
digitalWrite(12, LOW);
Serial.print("0");
Serial.println("0");
delay(1000); // delay to debounce switch
running = !running; // toggle running variable
digitalWrite(buttonPin, running); // indicate via buttonPin

}
else if(counter == 2)
{
digitalWrite(11, LOW);
digitalWrite(12, HIGH);
Serial.print("0");
Serial.println("1");
delay(1000); // delay to debounce switch
running = !running; // toggle running variable
digitalWrite(buttonPin, running); // indicate via buttonPin

}
else if(counter == 3)
{
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
Serial.print("1");
Serial.println("0");
delay(1000); // delay to debounce switch
running = !running; // toggle running variable
digitalWrite(buttonPin, running); // indicate via buttonPin

}
else if(counter == 4)
{
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
Serial.print("1");
Serial.println("1");
delay(1000); // delay to debounce switch
running = !running; // toggle running variable
digitalWrite(buttonPin, running); // indicate via buttonPin
}
}

{
unsigned long currentMillis = millis();
currentState = digitalRead(buttonPin);
unsigned long currentTime = millis();
if (buttonState == LOW){//if the buttonPin is already off
if (currentMillis - timeOfLastLedEvent > intervalOFF){//and enough time has passed
digitalWrite(pin11, HIGH);//turn it on
buttonState = HIGH;//store its current state
timeOfLastLedEvent = currentMillis;//update the time of this new event
}
} else {//if the buttonPin is already on
if (currentMillis - timeOfLastLedEvent > intervalON){
digitalWrite(pin, LOW); //turn it off
buttonState = LOW;
timeOfLastLedEvent = currentMillis;
}
}
}
}
*/

Comments are not code tags. Please check out:
how to use this forum
Pay particular attention to item 7.

switch (var) {
      case 1:
      digitalWrite(11, LOW);
      digitalWrite(12, LOW);
      Serial.print("0");
      Serial.println("0");
      break;
      //This is to display that if I pressed the push button once, on the Serial Monitor, it will display (0,0) only once.
      case 2:
      digitalWrite(11, LOW);
      digitalWrite(12, HIGH);
      Serial.print("0");
      Serial.println("1");
        break;
      //This is to display that if I pressed the push button 2 times, on the Serial Monitor, it will display (0,1) only once.
      case 3:
      digitalWrite(11, HIGH);
      digitalWrite(12, LOW);
      Serial.print("1");
      Serial.println("0");
        break;
        //This is to display that if I pressed the push button 3 times, on the Serial Monitor, it will display (1,0) only once.
      case 4:
      digitalWrite(11, HIGH);
      digitalWrite(12, HIGH);
      Serial.print("1");
      Serial.println("1");
        break;
       //This is to display that if I pressed the push button 4 times, on the Serial Monitor, it will display (1,1) only once.
      default:
        break;
      //And if I pressed the push button once, it will return back to (0,0) and the loop continues
}
}