element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum My first switch statement
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 14 replies
  • Subscribers 417 subscribers
  • Views 3587 views
  • Users 0 members are here
Related

My first switch statement

e14 Contributor
e14 Contributor over 13 years ago

Im trying to use a switch statement to jump to a different function.  All of the lessons I find just tell me how to turn on leds or outputs.  So I have no idea of what to do here.  So far I have this.

 

 

{

int range;     // I just used this cause it was in an example, I think I need it to name my switch statement. 

int flasha;  //  This is the subroutine I want to run.  I will have more late just trying to figure out the first one.

switch(range);

{

case 0:  //  I don't know why I have  to use "0"  it is just in all of the examples and nothing else works.

state==1, flasha;  // state is my input.  When a button is pressed it will count the number of presses and send it to the approiate subroutine.

break:

}

}

 

When I compile this program I get the error

 

case label '0' not within a switch staetment.

 

Can anyone explain what this means or what I'm doing wrong?

 

Thanks

  • Sign in to reply
  • Cancel

Top Replies

  • billabott
    billabott over 13 years ago +1
    Hey Dan & Nico. Looking at http://www.arduino.cc/en/Tutorial/SwitchCase Would not the range value be determined before entering the switch(range)&case structure? I guess you could set it up that for case…
  • ntewinkel
    ntewinkel over 13 years ago in reply to e14 Contributor +1
    Hi Dan, You are not defining and calling your functions correctly. Remove these two lines, as they define them as integers, which is not the intention: int flasha; int flashb; In the switch statement,…
  • ntewinkel
    ntewinkel over 13 years ago in reply to e14 Contributor +1
    Here's a good link too: http://diyroboticslab.wordpress.com/2009/06/04/ledblink-arduino-program/ It goes into the C programming language, and also has links to what error messages mean.
  • ntewinkel
    0 ntewinkel over 13 years ago in reply to e14 Contributor

    Good catch Billabot!

    That fun difference between the double equal sign == (compare) and the single equal sign = (assign) never gets old, does it?

     

    I'm a suspenders-and-belt guy myself so I usually use >=  just in case it advanced an extra buttonst count:


    if (buttonst >= 10) buttonst=0;            //   RIGHT HERE

     

     

    For the hard time pressing the button, it's because the delay(1000) calls halt execution for a full second while nothing else gets checked. See the "caveat" of the reference page: http://www.arduino.cc/en/Reference/delay

     

    You'd have to change the code quite a bit to make it multitask to catch the button press (or maybe the button can be handled via an interrupt? I haven't tried that in Arduino yet). Here is the example Arduino.cc gives: http://arduino.cc/en/Tutorial/BlinkWithoutDelay

     

    Hope that helps.

     

    Cheers,

    -Nico

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 13 years ago

    Well guys I tried both of you solutions and goot the same result.  And that is the same thing I had before.  It almost acts as if it doesn't even see that line of code.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • ntewinkel
    0 ntewinkel over 13 years ago in reply to e14 Contributor

    I only see you changing buttonst in this line:

        buttonst=digitalRead(8);

     

    With that it can only ever be HIGH or LOW, so it won't ever be 10 or more.

     

    If you're trying to increment buttonst with each keypress then you'd have to change your code to detect a button press (debounced so you don't interpret a single press as mutiple presses), and then increment buttonst by just 1.

     

    Also, this code:

    if (buttonst==HIGH)

        {//open state count

      

        if (buttonst==10)buttonst==0;                    RIGHT HERE

     

    you only enter the if... statement if buttonst is "HIGH", which is defined as a single value (probably just 1). Then right after that you check if it's 10. So it will never enter the "right here" statement because it will never be both of those separate values.

     

    Perhaps instead of buttonst you meant to use a separate counting variable like buttonCount ?

     

    Cheers,

    -Nico

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 13 years ago

    OK Guys I got it to work.  YEA!!!!!  The switch statement is working right and the counting is working just fine.  I made 3 different states and they loop around just like I want it to.  Thanks for all the help.  Anyone want to guess what I did?  Here it is.  I cookie for the first one to figure it out.  Now my next question is.  Can I put a switch statement inside a switch statement?  As it is now it will print both outputs.  The final version of the program will print one output at a time while remembering where the other output is at.  Also when 1 output gets to 3 both outputs will reset to case 0.

     

     

     

     

     

     

    int state=8;//set pin 8 as state

    int mi=9;  //set pin 9 as mi

    int lastState=LOW;  //

    int lastbutton=LOW;

    int countmi=0;

    int countst=0;

    int button=LOW;

    int buttonst=LOW;

     

     

    void setup()

    {// open void setup

    Serial.begin(9600);  //print to monitor

     

    pinMode(state, INPUT);//set pin 8

    pinMode(mi, INPUT);//set pin 9

    state=digitalRead(8);  //read pin 8

    button=digitalRead(9);  //read pin9

    }//close void setup

     

    void loop()

    {//open void loop

     

    if (buttonst==HIGH)  //when buttonst = high count countst

        {//open state count

        countst++; //count countst

       

     

        Serial.println(countst); //print countst

        //Serial.print("state =");                      

        }//closes state count

      //   Serial.print(buttonst); 

     

               {// open switch

              

              

                switch(countst) //start the switch

                       {//open case

                         case 0: //case 0 first subroutine

                         buttonst=0;

                         flasha();

                         break;

     

                         case 1:

                         buttonst=1;

                         flashb();

                         break;

                        

                          case 2:

                         buttonst=2;

                         flashc();

                         break;

                        

                        

                         case 3:

                         buttonst=3;

                         countst=0;

                         delay (1000);

                         break;

                        

                        

                        

                    

                        

                        }// close case

      

              }//close switch

         

    //   lastState=buttonst;

       

        buttonst=digitalRead(8);  // reaad pin 8

      

     

        if (button==HIGH)//pin 9 count

            {// open mi count

            countmi++;

            Serial.println(countmi);

            }//close mi count

           

                    {// open switch

              

              

                switch(countmi) //start the switch

                       {//open case

                         case 0: //case 0 first subroutine

                         button=0;

                         flasha();

                         break;

     

                         case 1:

                         button=1;

                         flashb();

                         break;

                        

                          case 2:

                         button=2;

                         flashc();

                         break;

                        

                        

                         case 3:

                         button=3;

                         countst=0;

                         delay (1000);

                         break;

                        

                        

                        

                    

                        

                        }// close case

      

              }//close switch

            

           

           

    //   lastbutton=button;

        button=digitalRead(9);

    }//close void loop

     

      //open flasha

        void flasha()

        {

        int flasha;

        Serial.println("Hellow world");

    delay (500);

      }//close flasha

     

      void flashb()

      {// open flashb

        int flashb;

        delay(500);

        Serial.println("Not gonna happen");

      }//close flashb

     

      void flashc()

      {

        int flashc;

        delay(500);

        Serial.println("I did it");

      }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
<
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube