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
  • 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 402 subscribers
  • Views 2965 views
  • Users 0 members are here
Related

My first switch statement

Former Member
Former Member 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 Former Member +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 Former Member +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

    Hi Dan,

     

    Remove the semicolon from this line:

         switch(range);

     

    Having that ; there ends that switch statement, after which the rest gets a whole new meaning to the compiler.

     

    Hope that helps!

     

    Cheers,

    -Nico

     

    edit:

    ps, "state==1, flasha;" is not valid syntax either...

     

    maybe you meant something like this?

     

    switch (range) {

     

         case 0:

              state = 1;

              function1();

         break;

     

         case 1:

              state = 2;

              function2();

         break;

     

    }


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

    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 0:  to cause the count from the button press(es) to modify the range value and as long that is contained in the main loop() function.  If you do it that way then the other cases would need to reset the range to 0 as the last step within each case #: segment (ie before the break statements;  but not in the case 0: segment).  Then again there may be some rule in the complier that stops the range value from being modified inside the case structure.   This would not be the standard way to do this anyway.   Would be much better to determine the number of button presses, store it in the range variable, and then execute the case statement.   

    Dan,  please create a setup();  a loop(), and a flasha()  which will all be of type void unless they return() a value.       Declare your global variables above the setup() and after the #include file.  

    image

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

    Hi Billabot,

     

    Yes, the value for range must definitely be set before entering the switch, as the switch depends on the value of range.

    I assumed Dan had posted just a snippet of his code.

     

    Great example in your picture!

     

    Cheers,

    -Nico

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

    Hi Guys,

     

    Frist thanks for all your help, gave me alot to think about.  I have made some progress but it is still not working wright.  So far this is what I have.

     

     

    int state=8;

    int mi=9;

    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);

     

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

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

    state=digitalRead(8);

    button=digitalRead(9);

    }//close void setup

     

    void loop()

    {//open void loop

     

    if (buttonst==HIGH && lastState==LOW)

        {//open state count

        countst++; 

        if (buttonst==10)

        { buttonst==0;

        Serial.println(countst);

        Serial.print("state =");                      

        }//closes state count

         Serial.print(buttonst); 

     

               {// open switch

                int range;

                int  flasha;

                int  flashb;

                switch(range)

                       {//open case

                         case 0:

                         buttonst=0;

                         flasha;

                         break;

     

                         case 1:

                         buttonst=2;

                         flashb;

                         break;

                        }// close case

      

              }//close switch

         

        lastState=buttonst;

       

        buttonst=digitalRead(8);

      

     

        if (button==HIGH && lastbutton==LOW)

            {// open mi count

            countmi++;

            Serial.println(countmi);

            }//close mi count

        lastbutton=button;

        button=digitalRead(9);

    }//close void loop

     

      {//open flasha

        int flasha;

        Serial.println("Hellow world");

    delay (1000);

      }//close flasha

      {// open flashb

        int flashb;

        Serial.println("Not gonna happen");

      }}//close flashb

     

     

     

    When you run it and open your Serial Monitor this is what happens.

     

    It will print

    Hello World

    delay

    then it prints

    Not gonna happen

     

    it does this over and over.  I think my problem is it is not polling my switch statements.  I think it is just going to the ends where I have my subroutines.  The worst part about this is it does it automatically.  Thats right you dont need to make a circuit.  Just cut and paste it and you will see what I mean.

     

    Please help before I choke someone out.

     

    Thanks

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

    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, change how you call the functions - they need opening and closing parentheses to show that it is a function call:

                switch(range)

                       {//open case

                         case 0:

                         buttonst=0;

                         flasha();

                         break;

     

                         case 1:

                         buttonst=2;

                         flashb();

                         break;

                        }// close case

     

    And to define your functions you need to give them names and identify what they give back (the first void) and what they receive (the second void)...

     

    void flasha(void)

      {//open flasha

        int flasha;

        Serial.println("Hellow world");

    delay (1000);

      }//close flasha

     

    void flashb(void)

      {// open flashb

        int flashb;

        Serial.println("Not gonna happen");

      }}//close flashb

     

    I haven't really looked at what the rest is doing or should be doing, but this will get you on your way.

     

    Cheers,

    -Nico

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

    Ok Nico

     

    I tried your changes.  Now when I go to compile I get the error 'flasha' was not declared in this scope  it is highlighting this section

     

       switch(range)

                       {//open case

                         case 0:

                         buttonst=0;

                         flasha();

                         break;

     

                         case 1:

                         buttonst=2;

                         flashb();

                         break;

                        }// close case

     

    one of the big problems I am having is I dont understand what these errors are talking about.  Is there a place I can go to that will expalin what this means?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • billabott
    0 billabott over 13 years ago in reply to Former Member

    To arrive HERE

    image

    One must first go HERE and do THIS:

    image

     

    -=Syntax Matters=-

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

    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.

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

    Thanks for all the help.  Just made a big improvement and I almost got it working.  There are only 2 problems now.  1 I have to hold the button down for about 2 or 3 seconds for it to switch subroutines.  I can deal with that but can I speed it up?  2. I made a note called "RIGHT HERE" I try to reset they count to 0.  It just keeps counting.  What did I do wrong?

     

     

     

     

    int state=8;

    int mi=9;

    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);

     

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

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

    state=digitalRead(8);

    button=digitalRead(9);

    }//close void setup

     

    void loop()

    {//open void loop

     

    if (buttonst==HIGH)

        {//open state count

       

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

         countst++;

        Serial.println(countst);

        Serial.print("state =");                      

        }//closes state count

      //   Serial.print(buttonst); 

     

               {// open switch

              

              

                switch(countst)

                       {//open case

                         case 0:

                         buttonst=0;

                         flasha();

                         break;

     

                         case 1:

                         buttonst=2;

                         flashb();

                         break;

                        

                          case 2:

                         buttonst=3;

                         flashc();

                         break;

                        

                        

                        

                        }// close case

      

              }//close switch

         

        lastState=buttonst;

       

        buttonst=digitalRead(8);

      

     

        if (button==HIGH && lastbutton==LOW)

            {// open mi count

            countmi++;

            Serial.println(countmi);

            }//close mi count

        lastbutton=button;

        button=digitalRead(9);

    }//close void loop

     

      //open flasha

        void flasha()

        {

        int flasha;

        Serial.println("Hellow world");

    delay (1000);

      }//close flasha

     

      void flashb()

      {// open flashb

        int flashb;

        delay(1000);

        Serial.println("Not gonna happen");

      }//close flashb

     

      void flashc()

      {

        int flashc;

        delay(1000);

        Serial.println("I did it");

      }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • billabott
    0 billabott over 13 years ago in reply to Former Member
    if (buttonst==10)buttonst==0;                RIGHT HERE

     

    verses

     

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

     

     

    // you need a whole bunch more comments

    // so you can remember how it is supposed to work

    • Cancel
    • Vote Up +1 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 © 2025 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.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube