Switch cases not breaking properly!

I’m in the process of testing my plotter before I get into the real applications.

Heres my code so far:

#include <Makeblock.h>
#include <SoftwareSerial.h>
#include <Wire.h>

MeLimitSwitch limitSwitch1(PORT_3, SLOT2); //intially 1
MeLimitSwitch limitSwitch2(PORT_3, SLOT1);
MeLimitSwitch limitSwitch3(PORT_6, SLOT2);
MeLimitSwitch limitSwitch4(PORT_6, SLOT1); //initially 0

int xdirPin = mePort[PORT_1].s1;//the direction pin connect to Base Board PORT1 SLOT1, 1 is toward us
int xstpPin = mePort[PORT_1].s2;//the Step pin connect to Base Board PORT1 SLOT2
int ydirPin = mePort[PORT_2].s1;//the direction pin connect to Base Board PORT1 SLOT1, 1 is toward us
int ystpPin = mePort[PORT_2].s2;//the Step pin connect to Base Board PORT1 SLOT2
int xcount;
int ycount;
char inread;

void setup()
{
  pinMode(xdirPin, OUTPUT);
  pinMode(xstpPin, OUTPUT);
  pinMode(ydirPin, OUTPUT);
  pinMode(ystpPin, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  if (Serial.available() > 0) {
    inread = Serial.read();
  }
  switch (inread) {

    case 'U':
      {
        xcount = 0;
        if (!limitSwitch3.touched()) {
          digitalWrite(xdirPin, LOW);
        }
        while (!limitSwitch3.touched()) {
          digitalWrite(xstpPin, HIGH);
          delayMicroseconds(400);
          digitalWrite(xstpPin, LOW);
          delayMicroseconds(400);
          xcount++;
        }
        if (limitSwitch3.touched()) {
          Serial.println(xcount);
        }
        break;
      }
    case 'D':
      {
        xcount = 0;
        if (!limitSwitch4.touched()) {
          digitalWrite(xdirPin, HIGH);
        }
        while (!limitSwitch4.touched()) {
          digitalWrite(xstpPin, HIGH);
          delayMicroseconds(400);
          digitalWrite(xstpPin, LOW);
          delayMicroseconds(400);
          xcount++;
        }
        if (limitSwitch4.touched()) {
          Serial.println(xcount);
        }
        break;
      }
  }

}

What happens in the serial monitor is this for example:

13601
0
0
0
0
… continued prints of 0

Can anyone explain why this is the case?
after the limit switch is hit, it prints out the actual x count, then instead of breaking it starts the case again. so x count is reset to 0, but because the limit switch is hit, it prints out xcount which is infact 0.

So the question is, why isn’t the break line working?

Don't put braces around the different cases. I suspect that break is breaking out of that brace but not out of the containing case.

case 'U':
        xcount = 0;
        if (!limitSwitch3.touched()) {
          digitalWrite(xdirPin, LOW);
        }
        while (!limitSwitch3.touched()) {
          digitalWrite(xstpPin, HIGH);
          delayMicroseconds(400);
          digitalWrite(xstpPin, LOW);
          delayMicroseconds(400);
          xcount++;
        }
        if (limitSwitch3.touched()) {
          Serial.println(xcount);
        }
        Serial.println("it got here");
        break;

tried this, but still not working! serial monitor reports as : 0 it got here 0 it got here 0 ....

MorganS: Don't put braces around the different cases. I suspect that break is breaking out of that brace but not out of the containing case.

No, break couldn't care less about the extra braces.

Regards, Ray L.

ahhh, this problem is really annoying. I have no idea what's wrong. Anyone else?

I see several things that impress me as wrong. First, when you receive a character, you act on it, but never clear the character. So, next time loop() executes, you'll do it all over again. Second, when you act on a character, you block, by doing a while() on the limit switch. NOTHING will happen as long as the limit switch state remains unchanged. You should not be blocking, but rather going away until the limit switch changes state.

Add print statements that let you see what is actually happening. One thing I can guarantee: the breaks ARE working correctly, and the problem is in your logic.

Regards, Ray L.

RayLivingston: I see several things that impress me as wrong. First, when you receive a character, you act on it, but never clear the character. So, next time loop() executes, you'll do it all over again. Second, when you act on a character, you block, by doing a while() on the limit switch. NOTHING will happen as long as the limit switch state remains unchanged. You should not be blocking, but rather going away until the limit switch changes state.

Add print statements that let you see what is actually happening. One thing I can guarantee: the breaks ARE working correctly, and the problem is in your logic.

Regards, Ray L.

Got it, thanks dude!

Added " inclear = "0"; " to start of void loop.