-3

I am experiencing some very strange behavior with my arduino. I have a switch-case statement in the loop() method, and the break methods are not only breaking out of the case block, but also out of the loop() statement. Is this normal, and if so, how can I edit the structure so it doesn't do that? My code structure looks like this:

void loop(){
  switch(int i){
    case 1:{
      //do something
      break;// this also breaks out of the loop() method.
    }
  }
}
  • I'm closing this because the source of the problem turned out to be unrelated to the original question. – Peter Bloomfield Sep 10 '14 at 15:04
1

Your sample is too simple to test your hypothesis (which is wrong, by the way.)

Set up serial output so you can test:

void loop()
{
  Serial.println("Before switch statement");
  int i = 1;
  switch (i)
  {
    case 1:
      Serial.println("In case 1");
      break;
    default:
      Serial.println("In default case");
  }
  Serial.println("After switch statement.");
}

You can even add an outer for loop to make things a little more complicated:

void loop()
{
  Serial.println("Before for loop statement");
  for (int i = 1; i< 10; i++)
  {
    Serial.print("In for loop before switch, i = ");
    Serial.println(i);
    switch (i)
    {
      case 1:
        Serial.println("In case 1");
        break;
      default:
        Serial.println("In default case");
    }
    Serial.println("After switch statement.");
  }
}

Examine the output on the serial monitor of your computer. You should find that the "After switch statement" line always appears after the "before switch..." line in both versions of the code.

Break breaks from the innermost scope. If you have nested levels of scope, like a function and then a switch statement inside the function, the break statement breaks out of that inner level. In the second example, the break broke out of the switch but the for loop kept running.

This is very basic C programming and not specific to Arduino.

  • The thing is, I'm running the MCU of my arduino in a completely different board, so I can't use the serial monitor. – Jaca Sep 2 '14 at 23:20
  • Uh-oh. Hook up an LCD display then? Or better yet, get a C compiler for your computer and learn the basics of C that way. As I say, your questions are on vanilla C, and not at all specific to the Arduino. You're going to spin yourself in circles trying to develop when you don't know the language and can't even add debugging statements. (I do my day job with an integrated symbolic debugger that lets me add breakpoints, examine variables, etc. The Arduino with it's limited debugging tools is a step backwards. Take away print statements and you're stumbling around in the dark.) – Duncan C Sep 2 '14 at 23:26
  • Never mind!! I just realized the sketch isn't broken, but instead one of my SD cards was corrupted. – Jaca Sep 3 '14 at 0:06
1

I just realized it wasn't the sketch, but a faulty SD card reader on my computer corrupted my SD card. BIG TIME.

  • Ok, I guess you should accept your own answer. That or delete the question. I still stand by my statement that you need a way of displaying debug information or you're going to drive yourself crazy. – Duncan C Sep 3 '14 at 1:16
  • @DuncanC Yeah, good point. – Jaca Sep 3 '14 at 3:27
  • @DuncanC I have to wait one more day. – Jaca Sep 4 '14 at 4:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.