Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Arduino Switch Case.

Arduino Switch Case.

Scheduled Pinned Locked Moved C / C++ / MFC
questionhardwaretutorial
9 Posts 5 Posters 15 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    glennPattonPub
    wrote on last edited by
    #1

    Hi All, Question about the Switch Case, I always treated the default section of a switch case a 'I have no idea what to do'

    for(i=0; i<= 100; i++)
    {
    myServo.write(i);
    val = myServo.read();

    switch(val)
    {
    case 0:
    break;

    case 50:
    break;

    case 100:
    break;

    default:
    //Here??
    break;
    }

    What appears to be happening it goes to the default when not caught by a catch. In the example when i = 50, I need a method for catching values out side of the the range 1 to 100. As if the servo returns value outside of the range 0 to 100. Could I do with an if... if(( i < 0) && (i >100)) would this take care of anything 0 - 100??? I will try that.

    J Richard Andrew x64R L R G 5 Replies Last reply
    0
    • G glennPattonPub

      Hi All, Question about the Switch Case, I always treated the default section of a switch case a 'I have no idea what to do'

      for(i=0; i<= 100; i++)
      {
      myServo.write(i);
      val = myServo.read();

      switch(val)
      {
      case 0:
      break;

      case 50:
      break;

      case 100:
      break;

      default:
      //Here??
      break;
      }

      What appears to be happening it goes to the default when not caught by a catch. In the example when i = 50, I need a method for catching values out side of the the range 1 to 100. As if the servo returns value outside of the range 0 to 100. Could I do with an if... if(( i < 0) && (i >100)) would this take care of anything 0 - 100??? I will try that.

      J Offline
      J Offline
      jeron1
      wrote on last edited by
      #2

      In your instance (if this is the actual code), all values that are not 0, 50, or 100 will end up at the default statement. There's also a missing closing bracket for the switch statement, probably a copy/paste thing.

      "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

      1 Reply Last reply
      0
      • G glennPattonPub

        Hi All, Question about the Switch Case, I always treated the default section of a switch case a 'I have no idea what to do'

        for(i=0; i<= 100; i++)
        {
        myServo.write(i);
        val = myServo.read();

        switch(val)
        {
        case 0:
        break;

        case 50:
        break;

        case 100:
        break;

        default:
        //Here??
        break;
        }

        What appears to be happening it goes to the default when not caught by a catch. In the example when i = 50, I need a method for catching values out side of the the range 1 to 100. As if the servo returns value outside of the range 0 to 100. Could I do with an if... if(( i < 0) && (i >100)) would this take care of anything 0 - 100??? I will try that.

        Richard Andrew x64R Offline
        Richard Andrew x64R Offline
        Richard Andrew x64
        wrote on last edited by
        #3

        glennPattonWork3 wrote:

        Could I do with an if... if(( i < 0) && (i >100))

        I'm afraid that test will never be true. No value of i can be less than zero AND greater than 100 at the same time.

        The difficult we do right away... ...the impossible takes slightly longer.

        G 1 Reply Last reply
        0
        • G glennPattonPub

          Hi All, Question about the Switch Case, I always treated the default section of a switch case a 'I have no idea what to do'

          for(i=0; i<= 100; i++)
          {
          myServo.write(i);
          val = myServo.read();

          switch(val)
          {
          case 0:
          break;

          case 50:
          break;

          case 100:
          break;

          default:
          //Here??
          break;
          }

          What appears to be happening it goes to the default when not caught by a catch. In the example when i = 50, I need a method for catching values out side of the the range 1 to 100. As if the servo returns value outside of the range 0 to 100. Could I do with an if... if(( i < 0) && (i >100)) would this take care of anything 0 - 100??? I will try that.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          According to that code as written, any value that is not 0, 50 or 100, will go to the default statement. If that is not happening then there must be some other code that we can't see.

          G 1 Reply Last reply
          0
          • G glennPattonPub

            Hi All, Question about the Switch Case, I always treated the default section of a switch case a 'I have no idea what to do'

            for(i=0; i<= 100; i++)
            {
            myServo.write(i);
            val = myServo.read();

            switch(val)
            {
            case 0:
            break;

            case 50:
            break;

            case 100:
            break;

            default:
            //Here??
            break;
            }

            What appears to be happening it goes to the default when not caught by a catch. In the example when i = 50, I need a method for catching values out side of the the range 1 to 100. As if the servo returns value outside of the range 0 to 100. Could I do with an if... if(( i < 0) && (i >100)) would this take care of anything 0 - 100??? I will try that.

            R Offline
            R Offline
            RedDk
            wrote on last edited by
            #5

            ... oops (wrong forum red-flag-accessed-by-moi) For some reason I thought I was in the Lounge and have disappropriated some points that shouldn't be mine. Sorry! Is "What do I do now" an appropriate C/C++/MFC question? Thanks (answered myself),

            G 1 Reply Last reply
            0
            • G glennPattonPub

              Hi All, Question about the Switch Case, I always treated the default section of a switch case a 'I have no idea what to do'

              for(i=0; i<= 100; i++)
              {
              myServo.write(i);
              val = myServo.read();

              switch(val)
              {
              case 0:
              break;

              case 50:
              break;

              case 100:
              break;

              default:
              //Here??
              break;
              }

              What appears to be happening it goes to the default when not caught by a catch. In the example when i = 50, I need a method for catching values out side of the the range 1 to 100. As if the servo returns value outside of the range 0 to 100. Could I do with an if... if(( i < 0) && (i >100)) would this take care of anything 0 - 100??? I will try that.

              G Offline
              G Offline
              glennPattonPub
              wrote on last edited by
              #6

              I am a dummy, I should have seen the problem, in fact I did as I was writing the question. I helps writing out the problem to see it more clearly, You can see the light starting to dawn in the last sentance problem solved.:thumbsup:

              1 Reply Last reply
              0
              • Richard Andrew x64R Richard Andrew x64

                glennPattonWork3 wrote:

                Could I do with an if... if(( i < 0) && (i >100))

                I'm afraid that test will never be true. No value of i can be less than zero AND greater than 100 at the same time.

                The difficult we do right away... ...the impossible takes slightly longer.

                G Offline
                G Offline
                glennPattonPub
                wrote on last edited by
                #7

                It could, if it was val not i as I typed. Solved

                1 Reply Last reply
                0
                • L Lost User

                  According to that code as written, any value that is not 0, 50 or 100, will go to the default statement. If that is not happening then there must be some other code that we can't see.

                  G Offline
                  G Offline
                  glennPattonPub
                  wrote on last edited by
                  #8

                  That was the thing. Typing it out made me see it. :sigh:

                  1 Reply Last reply
                  0
                  • R RedDk

                    ... oops (wrong forum red-flag-accessed-by-moi) For some reason I thought I was in the Lounge and have disappropriated some points that shouldn't be mine. Sorry! Is "What do I do now" an appropriate C/C++/MFC question? Thanks (answered myself),

                    G Offline
                    G Offline
                    glennPattonPub
                    wrote on last edited by
                    #9

                    It was a need to think carefully, code blindness issue. I knew if I posted it the lounge I would get cruified by all and sundry needed somewhere to write think and take notice of the abortion I has created. :omg:

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups