DOUBT WITH SWITH...CASE

Hello everyone,

I have a question about the structure mentioned in the title and is as follows:

Imagine that the next piece of code , there are two or more case that are true in the same program cycle ... how do I do ?, since it is assumed that can only be truly one of the functions .

myservo.write(90);
switch(pulso){
case 0:
left();
delay(250);
break;
case 45 :
left();
delay(500);
STOP();
break;
case 135 :
right();
delay(250);
break;
case 180 :
right();
delay(500);
break;

Thanks

How is that? You are saying that pulso is equal to 0 and equal to 45 at the same time?

pulso can only be one value.

If you want to catch multiple values you will need to change the case to a series of if … then statements so that every check gets executed and you can also introduce multiple variables in the boolean statement for the if.

Evidently, the delay() varies even when the same direction call is made. Because they are different, then you do need different case statement blocks for each pulso.

Also, please read Nick Gammon's notes at the top of this Forum telling you how to post code properly. That makes it easier for us to read, and for you to get more help.

You may come to regret using the delay() function. Use millis() to manage timing. See several things at a time.

...R

luisilva:
How is that? You are saying that pulso is equal to 0 and equal to 45 at the same time?

The impossible he can do immediately, miracles take a little longer.

Thanks for the replies ,
I have an ultrasound robot mounted on a servo motor, rotating from the 0º through 180º to detect objects and sometimes when it reaches a corner you get stuck .
I wanted to schedule it when it detects objects around that range of angles, reversing give and why I asked the question .
What if .... I'll try them.

Sorry for my bad English .

Let me reformat your code so it's a little easier to read (in the future, please use code tags when posting code, it makes it much easier to read and you code doesn't get reformatted by the forum software)

  myservo.write(90);

  switch(pulso){
     case 0:
         left();
         delay(250);
         break;

     case 45 :
         left();
         delay(500);
         STOP();
         break;

     case 135 :
         right();
         delay(250);
         break;

     case 180 :
         right();
         delay(500);
         break;

(By the way, you are missing the closing brace for the case statement. I'm guessing it's there in your sketch and you just didn't copy that far when you posted the code.)

Now, for your question:

McAndrew:
Imagine that the next piece of code , there are two or more case that are true in the same program cycle ... how do I do ?, since it is assumed that can only be truly one of the functions .

I don't understand the concern. A variable can only have one value at a time, and the case statement evaluates the value and selects the one case alternative that matches. How could it possibly match more than one of the case alternatives at once? To do so, the variable being tested would need to have two values at once, and that simply isn't possible.

When the case statement is evaluated, it checks the value of pulse0. If it is 0, 45, 90, 135, or 180, the corresponding case alternative is executed. If it's something else, nothing is executed (because you have no default: alternative.)

Perhaps one of those functions you call might change the pulseo value? Your concern might be that it would then match a different case alternative? For example, what happens if pulseo is zero, and calling left() changes pulseo to 45? In that case, when you reach the case statement, pulseo is evaluated, found to be zero, so the zero case alternative is executed. That alternative calls left() and changes pulseo, but that doesn't matter since the case statement will not re-evaluate pulseo again (until the next time the code loops around and the case statement is encountered again.) Even though the value of pulseo might now be 45 by the time the code completes the case 0 alternative, the remaining case alternative will not be considered during this pass.

You can think of a case statement to be a simpler way of writing this:

if (pulseo == 0)
{
   // do something
}
else if (pulseo == 45)
{
   // do something
}
else if (pulseo == 90)
{
   // do something
}
else if (pulseo == 135)
{
   // do something
}
else if (pulseo == 180)
{
   // do something
}

In this construct, only one of the "do something" sections will ever be executed during a single pass through the code.

Now, suppose left() is changing the value of pulseo and you DID want the subsequent alternatives to be considered. Then, instead of the case statement, you'd need to write a complex if statement like the above example, but leave out the "else" keywords. That makes each if statement stand on its own, and each one will be evaluated in turn, regardless of which previous ones evaluated to true.

if (pulseo == 0)
{
   // do something
}

if (pulseo == 45)
{
   // do something
}

if (pulseo == 90)
{
   // do something
}

if (pulseo == 135)
{
   // do something
}

if (pulseo == 180)
{
   // do something
}

If puslseo started out as 0, and each "do something" incremented pulseo by 45, then ALL of the if statements would evaluate true and all of them would be executed.

Did this cover your concern, or is it something else?