Switch not executing some cases

Hello!
I am trying to get this sketch to work as intended, but there are a few problems:

  1. in switch (cmd) only case 't': works; case 'l' and default do not work;
  2. Serial.read() in case 'l' does not read anything when sending l1, but actually 1 in l1 is read by the first Serial.read() on the next loop() iteration;
  3. The same happens if I use if - else if - else: only the t command can be invoked.

I am using Arduino IDE 1.8.19 on Majaro Linux and I selected Arduino AVR Boards -> Arduino Pro or Pro Mini.

The sketch is compiling without errors.
What could be the problem here?

// Use SI!

// Device: t
#include "OneWire.h"
#include "DallasTemperature.h"
#define T 5
OneWire w1(T);
DallasTemperature sensors(&w1);
// end device

// Device: l
// LED_BUILTIN, pin 13
// end device

void setup() {
  Serial.begin(115200);
  sensors.begin();
  pinMode(LED_BUILTIN, OUTPUT);
}

char cmd;
char arg;
void loop() {
  if (Serial.available()) {
    cmd = Serial.read();
    switch (cmd) {
      case 't':
        sensors.requestTemperatures();
        float t = sensors.getTempCByIndex(0);
        Serial.print("t:");
        Serial.println(t);
        break;
      case 'l':
        if (Serial.available()) {
          arg = Serial.read();
          switch (arg) {
            case '0': digitalWrite(LED_BUILTIN, LOW);
            case '1': digitalWrite(LED_BUILTIN, HIGH);
          }
        }
        break;
      default:
        Serial.print("Available commands:_");
        Serial.print("\tt  -> read temperature;_");
        Serial.print("\tl0 -> turn built-in led off;_");
        Serial.print("\tl1 -> turn built-in led on;_");
        Serial.println();
        break;
    }
  }
  delay(1000);
}

If you declare a variable with an initializer within a case, the enclose the statements within that case in brackets to limit the scope of the variable. Otherwise, the scope of the variable is the remainder of the switch/case, but the compiler will not let you execute any subsequent case because it sees a variable that has skipped its initialization.

Your problem with reading L1 is that the code executes so fast that the 1 is still being sent when you have read the L and are checking to see if there is another character in the serial buffer.

missing break for each case

Bummer. I just converted it from ifs and forgot to add breaks. But I will add them and retest.
Cheers!

If you declare a variable with an initializer within a case, the enclose the statements within that case in brackets to limit the scope of the variable.

This actually fixed it. However, the compiler did not complain.

    switch (cmd) {
      case 't':
        {
        sensors.requestTemperatures();
        float t = sensors.getTempCByIndex(0);
        Serial.print("t:");
        Serial.println(t);
        }
        break;

Thank you so much david_2018 and gcjr!

The working version:

// Use SI!

// Device: t
#include "OneWire.h"
#include "DallasTemperature.h"
#define T 5
OneWire w1(T);
DallasTemperature sensors(&w1);
// end device

// Device: l
// LED_BUILTIN, pin 13
// end device

void setup() {
  Serial.begin(115200);
  sensors.begin();
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  if (Serial.available()) {
    char cmd = Serial.read();
    switch (cmd) {
      case 't':
        {
          sensors.requestTemperatures();
          float t = sensors.getTempCByIndex(0);
          Serial.print("t:");
          Serial.println(t);
        }
        break;
      case 'l':
        if (Serial.available()) {
          char arg = Serial.read();
          switch (arg) {
            case '0':
              digitalWrite(LED_BUILTIN, LOW);
              break;
            case '1':
              digitalWrite(LED_BUILTIN, HIGH);
              break;
          }
        }
        break;
      default:
        Serial.print("Available commands:_");
        Serial.print("\tt  -> read temperature;_");
        Serial.print("\tl0 -> turn built-in led off;_");
        Serial.print("\tl1 -> turn built-in led on;_");
        Serial.println();
        break;
    }
  }
  delay(1000);
}

It does here

C:\Users\Bob2\AppData\Local\Temp\.arduinoIDE-unsaved2024127-12464-1syj54n.dqsuh\sketch_feb27a\sketch_feb27a.ino: In function 'void loop()':
C:\Users\Bob2\AppData\Local\Temp\.arduinoIDE-unsaved2024127-12464-1syj54n.dqsuh\sketch_feb27a\sketch_feb27a.ino:37:18: warning: jump to case label [-fpermissive]
C:\Users\Bob2\AppData\Local\Temp\.arduinoIDE-unsaved2024127-12464-1syj54n.dqsuh\sketch_feb27a\sketch_feb27a.ino:33:23: note:   crosses initialization of 'float t'
C:\Users\Bob2\AppData\Local\Temp\.arduinoIDE-unsaved2024127-12464-1syj54n.dqsuh\sketch_feb27a\sketch_feb27a.ino:48:13: warning: jump to case label [-fpermissive]
C:\Users\Bob2\AppData\Local\Temp\.arduinoIDE-unsaved2024127-12464-1syj54n.dqsuh\sketch_feb27a\sketch_feb27a.ino:33:23: note:   crosses initialization of 'float t'
C:\Users\Bob2\AppData\Local\Temp\.arduinoIDE-unsaved2024127-12464-1syj54n.dqsuh\sketch_feb27a\sketch_feb27a.ino:43:47: warning: this statement may fall through [-Wimplicit-fallthrough=]
C:\Users\Bob2\AppData\Local\Temp\.arduinoIDE-unsaved2024127-12464-1syj54n.dqsuh\sketch_feb27a\sketch_feb27a.ino:44:25: note: here```

Win 10, IDE 2.2.1 with verbose compiler warnings turned on in IDE Preferences

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