Search code examples
c++arduinoswitch-statementarduino-c++

default label apparently not working in switch statement


I've seen some answers to related posts about this issue, but not exactly an answer to my problem. Here is my code:

#include <VirtualWire.h>
uint8_t RFbuff[VW_MAX_MESSAGE_LEN];   
uint8_t RFbuflen = VW_MAX_MESSAGE_LEN;
const int REMOTE = 0; 
String remoteScriptName(){
  String scriptName;
  switch(REMOTE){

    case 1:
      vw_get_message(RFbuff, &RFbuflen);
      String RFSTRBUFF = RFbuff;
      scriptName = RFSTRBUFF; 
      delay(1200);
      break;
    default:
      scriptName = "script"; 
      delay(500); 
    }
  return(scriptName);
}

void setup() {
  pinMode(LED_BUILTIN, HIGH);
  switch(REMOTE){
    case 1:
      vw_set_rx_pin(8);
      vw_setup(2000);
      vw_rx_start();
      break; 
    default: 
      digitalWrite(LED_BUILTIN, LOW); 
      delay(500);
      digitalWrite(LED_BUILTIN, HIGH);
      delay(500);
      digitalWrite(LED_BUILTIN, LOW); 
      delay(500);
      digitalWrite(LED_BUILTIN, HIGH);
  }
}

void loop(){}

In the remoteScriptName() function at switch(REMOTE) there is case: and default:. When I upload my code to my Arduino micro it skips the default in switch(REMOTE). But then at void setup I have the same switch statement, but in this case (after uploading the code) it does execute default:!

I don't have much experience with switch(), so any recommendation is well received.


Solution

  • Your questions nudged me to provide a copy of code-with-stubs.

    developed on Lubuntu 18.10

    using g++ (Ubuntu 9.2.1-9ubuntu2) 9.2.1 20191008


    #ifndef                 DTB_CHRONO_HH
    #include "../../bag/src/dtb_chrono.hh"
    using  namespace std::chrono_literals; // support suffixes like 100ms, 2s, 30us
    #endif
    
    #include <iostream>
    using std::cout, std::cerr, std::endl;
    
    #include <string>
    using std::string, std::to_string;
    
    #include <thread>
    using std::thread, std::this_thread::sleep_for;
    
    #ifndef                 DTB_PCKLRT_HH
    #include "../../bag/src/dtb_pclkrt.hh"  // class PosixClockRT_t
    using  DTB::PClk_t;
    #endif
    
    #ifndef                 DTB_SOPS_HH
    #include "../../bag/src/dtb_sops.hh" // class StringOps_t
    using DTB::Sops_t;
    #endif
    
    
    namespace // anonymous
    {
       // functor
       class F838_t  // a UDT (user defined type)
       {
          PClk_t  pclk; // class PosixClockRT_t
          Sops_t  sops; // class StringOps_t
    
          enum { VW_MAX_MESSAGE_LEN = 10, LED_BUILTIN, LOW=0x05, HIGH=0x0a };
    
          uint8_t RFbuff     [VW_MAX_MESSAGE_LEN];
          uint8_t RFbuflen = VW_MAX_MESSAGE_LEN;
    
          const int REMOTE = 0;
    
       public:
          // ctor and dtor use compiler provided defaults
          int operator()(int argc, char* argv[]) { return exec(argc, argv);  }
    
       private:
    
          int exec(int , char** )
             {
                int retVal = 0;
                uint64_t start_ns = pclk.ns();
    
                setup();
                remoteScriptName();
    
                cout << "\n\n  F838_t::exec() duration "
                     << sops.digiComma(pclk.ns() - start_ns)
                     << " ns    (" <<  __cplusplus  << ")"
                     << std::endl;
                return retVal;
             }
    
          void setup()
             {
                pinMode(LED_BUILTIN, HIGH);
                switch(REMOTE)
                {
    
                case 1:
                {
                   vw_set_rx_pin(8);
                   vw_setup(2000);
                   vw_rx_start();
                }  break;
    
                default:
                {
                   digitalWrite(LED_BUILTIN, LOW);
                   sleep_for(500ms);  // delay(500);
                   digitalWrite(LED_BUILTIN, HIGH);
                   sleep_for(500ms);  // delay(500);
                   digitalWrite(LED_BUILTIN, LOW);
                   sleep_for(500ms);  // delay(500);
                   digitalWrite(LED_BUILTIN, HIGH);
                } break;
    
                }
             }
    
          string remoteScriptName()
             {
                string scriptName;
                switch(REMOTE)
                {
    
                case 1:
                {
                   vw_get_message(RFbuff, &RFbuflen);
                   //string RFSTRBUFF = RFbuff;
                   string RFSTRBUFF;
                   for (int i; i < RFbuflen; ++i)
                      RFSTRBUFF += static_cast<char>(RFbuff[i]);
                   scriptName = RFSTRBUFF;
                   sleep_for(1200ms); //delay(1200);
                }
    
                break;
    
                default:
                {
                   scriptName = "script";
                   sleep_for(500ms); // delay(500);
                }
                }
    
                return(scriptName);
             } // string remoteScriptName()
    
          // stubs:
          void pinMode(int led, int v) { cout << "\n  pinMode("
                                              << led << "," << v << ")"; }
    
          void vw_set_rx_pin(int v) { cout << "\n  vw_set_rx_pin("
                                           << v << ")"; }
          void vw_setup(int v)      { cout << "\n  vw_setup     ("
                                           << v << ")"; }
          void vw_rx_start()        { cout << "\n  vw_rx_start  ()"; }
    
          void digitalWrite(int w,
                            int v) { cout << "\n  digitalWrite  ("
                                          << w << "," << v << ")"; }
    
          void vw_get_message(uint8_t*  bf,
                              uint8_t*  ln)
             { cout << "\n  vw_get_message  ("
                    << bf << "," <<  *ln << ")"; }
    
       }; // class F838_t
    
    }  // anonymous namespace
    
    // one line main invokes functor ---------vvvvvvvv
    int main(int argc, char* argv[]) { return F838_t()(argc, argv); }
    

    Note: 142 lines, only 14 lines of stubs


    Build output to show compiler options:

    time make CC='g++ $(m) -O0 ' dumy838 ; ./dumy838
    
    >>>cvs-tools/lmbm/src800/Makefile.i686
    
    R_exe: dumy838.cc
    rm -f dumy838
    g++ -std=c++17 -m64 -ggdb -O3 -Wall -Wextra -Wshadow -pedantic -Werror=vla 
    -Wcast-align -Wcast-qual -Wconversion -Wsign-conversion -Wsign-compare 
    -Wsign-promo -Wpointer-arith -Wunused -Wold-style-cast -Woverloaded-virtual
    -Wsequence-point -Wdelete-incomplete -Wmaybe-uninitialized 
    -Wmisleading-indentation -Wunreachable-code -Wnon-virtual-dtor 
    -MMD -MP -O0   dumy838.cc  -o dumy838 
    -L../../bag/src -lbag_i686 -lrt -pthread -lncurses -lgmpxx -lgmp
    
    real    0m1.506s
    user    0m1.289s
    sys 0m0.181s
    

    Execution output (of stubs) including posix time measurement

      pinMode(11,10)
      digitalWrite  (11,5)
      digitalWrite  (11,10)
      digitalWrite  (11,5)
      digitalWrite  (11,10)
    
      F838_t::exec() duration 2,000,584,200 ns    (201703)
    
    Compilation finished at Sat Dec 28 11:06:06