I have a situation where the default case in my switch statement doesn't appear to execute as expected. I wonder if there is some subtlety I'm missing here?
Given the following enum:
enum HomingPhase
{
Idle = 0,
Detecting,
Stopping,
Reversing,
AtHome,
};
and the following methods:
void HomeSensor::setPhase(HomingPhase newPhase)
{
phase = newPhase;
Serial.print("Phase ");
Serial.println(phase);
}
void HomeSensor::onMotorStopped() const
{
std::cout << "Hstop " << phase << std::endl;
switch (phase)
{
case Reversing:
setPhase(AtHome);
break;
case Stopping:
setPhase(Reversing);
const auto target = commandProcessor.targetStepPosition(homeSettings->position);
motor->moveToPosition(target);
break;
default:
setPhase(Idle);
return;
}
}
At runtime, onMotorStopped()
is called and I see the following output, which indicates that phase
is AtHome
(4) on entry to the method.
Hstop 4
I expect the default case to execute, setting the phase to Idle
but I do not see the output from setPhase()
, which suggests that the default case isn't executing. Subsequent output from elsewhere in the code confirms that phase
in fact remains set to AtHome
(4) and isn't being set to Idle
as expected.
I think I may have gone code blind. Can anyone see the issue?
std::cout
(works). The other toSerial
. I don't knowstd::cout
on an Arduino... Are you really running it on an Arduino?Serial.println
does produce output in other circumstances so it's not that the print statement isn't working.case
anddefault
?