# If statement does not work in Switch case

**URL:** <https://forum.arduino.cc/t/if-statement-does-not-work-in-switch-case/864610>\
**Category:** General Guidance\
**Created:** [May 21, 2021, 3:27am UTC](https://forum.arduino.cc/t/if-statement-does-not-work-in-switch-case/864610 "2021-05-21T03:27:50Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![btjh](https://avatars.discourse-cdn.com/v4/letter/b/e9a140/32.png) [@btjh](https://forum.arduino.cc/u/btjh)\
**Post date:** [May 21, 2021, 3:27am UTC](https://forum.arduino.cc/t/if-statement-does-not-work-in-switch-case/864610/1 "2021-05-21T03:27:50Z")

</div>

Hi everybody, I am doing this project about a exercise counter with different modes such as push-up and planking. These two modes can be selected via an MIT application which connects to the arduino uno board through bluetooth connection.

The problem I am facing is that the application is able to select the modes and display the initial texts, however, it **does not continue to count the push-up**. The items used are arduino uno, HC-05 bluetooth device, HC-SR04 ultrasonic sensor and a LCD I2C. Any advices on this issue?

```cpp
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>

LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2); 
SoftwareSerial BT(0,1); //TX, RX respectively for bluetooth connected to RX and TX pin on arduino board
const int trigPin = 8; //set pins for the ultrasonic sensor, button, buzzer and LCD
const int echoPin = 9;
const int buttonPin = 2;
const int buzzer = 13;

int value; //for switch case
long signalDuration; //defining signalDuration as long as it can potentially store a large number
int signalDistance; //distance detected by sensor
int a; //number of counts
int z = 1;
int x = 1;
int buttonState = 0;
int GREEN = 5;
int YELLOW = 3;
int RED = 4;
int DELAY_GREEN = 5000;
int DELAY_YELLOW = 2000;
int DELAY_RED = 5000;

void setup() 
{
  Serial.begin(9600); // begin in 9600 baud 
  BT.begin(9600); //initialise BT module
  pinMode(trigPin, OUTPUT); //set pin modes
  pinMode(echoPin, INPUT);
  pinMode(buttonPin, INPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(YELLOW, OUTPUT);
  pinMode(RED, OUTPUT);
  lcd.init();
  lcd.backlight();
  lcd.begin (16,2);
  red_light();
}

void green_light()
{
  digitalWrite(GREEN, HIGH);
  digitalWrite(YELLOW, LOW);
  digitalWrite(RED, LOW);
}

void yellow_light()
{
  digitalWrite(GREEN, LOW);
  digitalWrite(YELLOW, HIGH);
  digitalWrite(RED, LOW);
}

void yellow_blink()
{
  digitalWrite(YELLOW,HIGH);
  delay(300);
  digitalWrite(YELLOW,LOW);
  delay(100);
}

void red_light()
{
  digitalWrite(GREEN, LOW);
  digitalWrite(YELLOW, LOW);
  digitalWrite(RED, HIGH);
}

void loop() 
{
  digitalWrite(trigPin, LOW); // send out an ultra sonic sound for 10 microseconds and measure the time it took for the sound to go from the trigpin to the echo pin
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  signalDuration = pulseIn(echoPin, HIGH);
  signalDistance = signalDuration * 0.034/2; //convert the time the signal took to travel to distance in cm
  
  buttonState = digitalRead(buttonPin); //these lines of code resets every integer and the lcd to the original state by the press of a button
  if (buttonState == HIGH) 
  {
    a = 0; //no. of push up counts
    z = 1;
    x = 1;
    tone(buzzer, 1000); // Send 1KHz sound signal...
    delay(100); // ...for 1 sec
    noTone(buzzer); // Stop sound...
    delay(100); // ...for 1sec
    Serial.println(a);
    BT.println("Reset");
    BT.println("Count:");
    BT.println(a);
    lcd.clear ();
    lcd.setCursor(3,0);
    lcd.print("Reset"); 
    lcd.setCursor(3,1);
    lcd.print("Count:");
    lcd.setCursor(10,1);
    lcd.print(a);
    digitalWrite(RED,LOW);
    delay(100);
    digitalWrite(RED,HIGH);
    delay(300);
    digitalWrite(RED,LOW);
    delay(100);
    digitalWrite(RED,HIGH);
    delay(300);
    digitalWrite(RED,LOW);
  
  } 

  if (Serial.available() > 0) 
  {
    value = Serial.read();
    
     switch (value) 
     {
      case '1': //push-up mode 
    BT.println("Push-Up Mode Selected");
    BT.println("Begin in:");
    delay(1000);
    BT.println("3");
    delay(1000);
    BT.println("2");
    delay(1000);
    BT.println("1");
    delay(1000);
    BT.println("Go!");
  
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Push-Up Selected");
    delay(1000);
    lcd.clear();
    lcd.print("Begin in:");
    lcd.setCursor(0,1);
    lcd.print("3");
    delay(1000);
    lcd.clear();
    lcd.print("2");
    delay(1000);
    lcd.clear();
    lcd.print("1");
    delay(1000);
    lcd.clear();
    lcd.print("Go!");
    
    if (signalDistance < 12 ) //counts a push-up is distance detected is below 12cm
   {
    a ++; //print a point if a pushup has been done
    Serial.println(a);
    BT.println("Push-Up");
    BT.println("Counts:");
    BT.println(a);
    lcd.clear();
    lcd.setCursor(3,0);
    lcd.print("Push-Up");
    lcd.setCursor(3,1);
    lcd.print("Counts: ");
    lcd.setCursor(10,1);
    lcd.print(a);
    yellow_blink(); 
  }
  
  
     if (a == (1 * x) && z == (1 * x)) //this if statement plays a sound for every pushup
  { 
  tone(buzzer, 10000); // Send 1KHz sound signal...
  delay(100); // ...for 1 sec
  noTone(buzzer); // Stop sound...
  delay(100); // ...for 1sec
    z ++;
    x ++;
  }

   while (signalDistance <= 12) 
   { //if the distance stays smaller then twelve for a while, this piece of code makes sure that only one point is given for one pushup
      digitalWrite(trigPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(trigPin, LOW);
      signalDuration = pulseIn(echoPin, HIGH);
      signalDistance = signalDuration * 0.034/2;
      delay(100);
   }
   while (signalDistance >= 13 && signalDistance <= 35) 
   { //prevents ultrasonic sensor from accidentally counting more as the distance fluctuates between 12cm
      digitalWrite(trigPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(trigPin, LOW);
      signalDuration = pulseIn(echoPin, HIGH);
      signalDistance = signalDuration * 0.034/2;
      delay(100);
   }
   break;
  }
  }
 

}

```

---

<div class="post-metadata">

**Author:** ![LFRED](https://avatars.discourse-cdn.com/v4/letter/l/ecc23a/32.png) [@LFRED](https://forum.arduino.cc/u/LFRED)\
**Post date:** [May 21, 2021, 3:36am UTC](https://forum.arduino.cc/t/if-statement-does-not-work-in-switch-case/864610/2 "2021-05-21T03:36:40Z")

</div>

> [@btjh](#):
>
> `value = Serial.read();`

int or text....

I think switch works with INT only.

---

<div class="post-metadata">

**Author:** ![btjh](https://avatars.discourse-cdn.com/v4/letter/b/e9a140/32.png) [@btjh](https://forum.arduino.cc/u/btjh)\
**Post date:** [May 21, 2021, 3:39am UTC](https://forum.arduino.cc/t/if-statement-does-not-work-in-switch-case/864610/3 "2021-05-21T03:39:46Z")

</div>

hi yes, i have initialised " int value;"

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [May 21, 2021, 3:40am UTC](https://forum.arduino.cc/t/if-statement-does-not-work-in-switch-case/864610/4 "2021-05-21T03:40:45Z")

</div>

It’s perfectly fine as a char

---

<div class="post-metadata">

**Author:** ![LFRED](https://avatars.discourse-cdn.com/v4/letter/l/ecc23a/32.png) [@LFRED](https://forum.arduino.cc/u/LFRED)\
**Post date:** [May 21, 2021, 3:41am UTC](https://forum.arduino.cc/t/if-statement-does-not-work-in-switch-case/864610/5 "2021-05-21T03:41:02Z")

</div>

ok dont used "1" only 1

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [May 21, 2021, 3:42am UTC](https://forum.arduino.cc/t/if-statement-does-not-work-in-switch-case/864610/6 "2021-05-21T03:42:41Z")

</div>

No, not 1, not "1"… ‘1‘ was spot on…(the character 1 in ASCII)

For the rest OP Don’t post snippets ([Snippets R Us!](https://snippets-r-us.com))

---

<div class="post-metadata">

**Author:** ![btjh](https://avatars.discourse-cdn.com/v4/letter/b/e9a140/32.png) [@btjh](https://forum.arduino.cc/u/btjh)\
**Post date:** [May 21, 2021, 3:46am UTC](https://forum.arduino.cc/t/if-statement-does-not-work-in-switch-case/864610/7 "2021-05-21T03:46:00Z")

</div>

> [@J-M-L](#):
>
> ‘1‘ was spot on…(the character 1 in ASCII)

yes this works. However, the issue is that it stops working at the if statement.

```arduino
 if (signalDistance < 12 ) //counts a push-up is distance detected is below 12cm
   {
    a ++; //print a point if a pushup has been done
    Serial.println(a);
    BT.println("Push-Up");
    BT.println("Counts:");
    BT.println(a);
    lcd.clear();
    lcd.setCursor(3,0);
    lcd.print("Push-Up");
    lcd.setCursor(3,1);
    lcd.print("Counts: ");
    lcd.setCursor(10,1);
    lcd.print(a);
    yellow_blink(); 
  }

```

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [May 21, 2021, 3:56am UTC](https://forum.arduino.cc/t/if-statement-does-not-work-in-switch-case/864610/8 "2021-05-21T03:56:35Z")

</div>

As I said Don’t post snippets ([Snippets R Us!](https://snippets-r-us.com))

---

<div class="post-metadata">

**Author:** ![btjh](https://avatars.discourse-cdn.com/v4/letter/b/e9a140/32.png) [@btjh](https://forum.arduino.cc/u/btjh)\
**Post date:** [May 21, 2021, 3:59am UTC](https://forum.arduino.cc/t/if-statement-does-not-work-in-switch-case/864610/9 "2021-05-21T03:59:24Z")

</div>

yes i've posted the **full code** in the OP above

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [May 21, 2021, 4:07am UTC](https://forum.arduino.cc/t/if-statement-does-not-work-in-switch-case/864610/10 "2021-05-21T04:07:47Z")

</div>

OP is you (Original Poster)  
If there is only one case, the switch is overkill. Use an if

Once you exit the case you won’t go back in unless there is a new ’1’ coming through Serial, is that intended?

---

<div class="post-metadata">

**Author:** ![btjh](https://avatars.discourse-cdn.com/v4/letter/b/e9a140/32.png) [@btjh](https://forum.arduino.cc/u/btjh)\
**Post date:** [May 21, 2021, 4:16am UTC](https://forum.arduino.cc/t/if-statement-does-not-work-in-switch-case/864610/11 "2021-05-21T04:16:59Z")

</div>

As for now, I only have the codes for a single case. The other cases i am still working on it.

The **main problem** now is that the code does not work after printing the LCD display. Meaning, it stops working at the if(signalDistance \< 12).

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [May 21, 2021, 4:19am UTC](https://forum.arduino.cc/t/if-statement-does-not-work-in-switch-case/864610/12 "2021-05-21T04:19:00Z")

</div>

Answer this

> [@J-M-L](#):
>
> Once you exit the case you won’t go back in unless there is a new ’1’ coming through Serial, is that intended?

---

<div class="post-metadata">

**Author:** ![btjh](https://avatars.discourse-cdn.com/v4/letter/b/e9a140/32.png) [@btjh](https://forum.arduino.cc/u/btjh)\
**Post date:** [May 21, 2021, 4:21am UTC](https://forum.arduino.cc/t/if-statement-does-not-work-in-switch-case/864610/13 "2021-05-21T04:21:19Z")

</div>

> [@J-M-L](#):
>
> is that intended?

yes friend

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [May 21, 2021, 4:23am UTC](https://forum.arduino.cc/t/if-statement-does-not-work-in-switch-case/864610/14 "2021-05-21T04:23:56Z")

</div>

So your BT sends 1 over and over again?

---

<div class="post-metadata">

**Author:** ![btjh](https://avatars.discourse-cdn.com/v4/letter/b/e9a140/32.png) [@btjh](https://forum.arduino.cc/u/btjh)\
**Post date:** [May 21, 2021, 4:27am UTC](https://forum.arduino.cc/t/if-statement-does-not-work-in-switch-case/864610/15 "2021-05-21T04:27:05Z")

</div>

It only sends 1 if i press the button in the application.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [May 21, 2021, 4:36am UTC](https://forum.arduino.cc/t/if-statement-does-not-work-in-switch-case/864610/16 "2021-05-21T04:36:16Z")

</div>

So isn’t that a problem for your code

Run it manually and see how the serial available will get in your way as the loop() loops

---

<div class="post-metadata">

**Author:** ![johnerrington](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnerrington/32/340106_2.png) [@johnerrington](https://forum.arduino.cc/u/johnerrington)\
**Post date:** [May 21, 2021, 4:40am UTC](https://forum.arduino.cc/t/if-statement-does-not-work-in-switch-case/864610/17 "2021-05-21T04:40:18Z")

</div>

This may help:

> [@Beginners: using the switch - case statement](https://forum.arduino.cc/t/beginners-using-the-switch-case-statement/680177/7):
>
> Hii, This approach looks good and can I know whether the implementation of this code with other Arduino boards effects anything.

I can see why you want to use the switch-case structure, as you have lots of different modes.

I'd recommend you break your code up into functional modules, eg

```arduino
void countPushup()
{
    a ++; //print a point if a pushup has been done
    Serial.println(a);
    BT.println("Push-Up");
    BT.println("Counts:");
    BT.println(a);
    lcd.clear();
    lcd.setCursor(3,0);
    lcd.print("Push-Up");
    lcd.setCursor(3,1);
    lcd.print("Counts: ");
    lcd.setCursor(10,1);
    lcd.print(a);
    yellow_blink(); 
  }

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 18, 2021, 4:40am UTC](https://forum.arduino.cc/t/if-statement-does-not-work-in-switch-case/864610/18 "2021-09-18T04:40:30Z")

</div>

Closed on September 18, 2021, 4:40am UTC

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.
