My switch/case statement isn't working?

Ok I'm trying to to use the switch/case statement but can't seem to figure out what is wrong with it. I thought I was doing everything right, cause I looked around. I'm using an Arduino.

 

void mydirections (int myAngle, int distance)
{
switch(myAngle)

case 10:

if (distance < range)
{
right();
break;
}

case 20:

if (distance < range)

{

right();

break;

}

default:
forward();
break;
}

It say that 20 is not within a switch statement. So what that mean?

 

I R absolute beginner also

I don’t even recognize the language of this code. I’m sure anyone who actually uses this language would recognize it. But I come here to learn as much a I come here to preach lecture teach splain stuph.

BTW, does this language not require a closing keyword (or character) for each case?

switch()
{
case rik:
soStupid()
endcase

case demonic_crow:
soSmart()
endcase
}

break; is what stop the
break; is what stop the case and keep it from trying to run all at once.

Lol, I don’t know I’m lost,
Lol, I don’t know I’m lost, cant seem to figure it out. I been looking at a bunch of different languages. Its seem like they have something like if you pick option 1 then do case 1 and pick option 2 do case 2. I figure that I could have it pick my servo angle though. Just weird though why its saying its not within a switch statement. Seem like I wrote it just like I seen other places, then again I never worked with switch/case statements.

OK, use break;

But don’t put the break; between the parentheses of a block within the case/break routine.

case 10:
if (distance < range)
{
right();
}
break;

case 20:
if (distance < range)
{
right();
}
break;

Yea I had it that way the
Yea I had it that way the first time and it said the break statement not within loop or switch. So I place it in like I have it and it takes that error away.

it is weird, as I used your

it is weird, as I used your code for the most part, slightly modded and it works fine for me. do you get the error at compile time?

here is the code I used: this is the modified blink code with the switch/case

/*
* Blink
*modified to use switch/case adn output to serial
*/

int ledPin = 8; // LED connected to digital pin 13
int myAngle;
void setup() // run once, when the sketch starts
{
pinMode(ledPin, OUTPUT); // sets the digital pin as output
Serial.begin(9600);
myAngle = 20;
}

void loop() // run over and over again
{

switch(myAngle){
case 10:
digitalWrite(ledPin, HIGH); // sets the LED on
delay(300); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(1000); // waits for a second
myAngle = 20;
Serial.println(myAngle, DEC); // print as an ASCII-encoded decimal
break;

case 20:
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(500);
// waits for a second
myAngle = 30;
Serial.println(myAngle, DEC); // print as an ASCII-encoded decimal
break;
default:
digitalWrite(ledPin, HIGH); // sets the LED on
delay(100); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(100); // waits for a second
myAngle =10;
Serial.println(myAngle, DEC); // print as an ASCII-encoded decimal
}

}

Ok, last night while I was
Ok, last night while I was at while I was going over the code on lunch. Well I figure out the reason why it wasn’t working and telling me it wasn’t within a switch statement. I forgot to add a bracket after switch so now that I did that everything seem to be fine now. Those bracket and ; can really mess up your code if you forget them.

A good rule of thumb is
A good rule of thumb is whenever you add an open bracket { ALWAYS add the close bracket } right away. Then add the code in between. This way you always have both.

Another trick, use the fact

Another trick, use the fact that the editor has bracket highlighting. This means that when you have an opening bracket, the editor will highlight the closing one(if it exists).

Try it out on a blank sketch. add a { then add the closer }.

Move the cursor to the right side of the opening bracket. the closing bracket should now highlight. this works with the closing bracket as well.

 

Code Style

It is also good to use indentations to seperate your code:

Switch( variable ) {

case 0:

code

break;

case n:

code

break;

default:

}

Good programming style will help you keep track of your code flow.

Good point. Maybe a topic
Good point. Maybe a topic should be started on this, but good coding style can make a big difference in readablility. It’s a good idea to go over the coding style recommendations that are in the manuals for whatever proc you are using.

Yea I must double space is
Yea I must always (double space) is the word so I see everything clearly. I still sometime forget the ; or bracket. Also I did start clicking to the right of the bracket to make sure I have the right closing one for the one I need. Like they say you have to learn from your mistake.

Ok now my new issue is
Ok now my new issue is trying to figure a way to code it to make it decide what direction to choose to go after it see something in front of it. I try to think of a way by using an if/else statement and switch/case statement. I know I have to take the angle on the left and check the angle on the right and see if which have a greater distance. I been thinking about it for now 2 days. I’m just not really sure how I can just add it in. I thought about using a switch with the distance but would have to figure out the angles to check with have more but having really trouble doing so. I know I might be a little off on my thinking but then again I could be going in the wrong directions. So wondering if someone to give me some tips on how to go about it. Thanks a bunch.

Look leftLeft = distanceLook

Look left
Left = distance

Look right
Right = distance

If left > right
turn robot left
else
turn robot right
endif

forward = distance
if forward > dangerlevel
move robot forward
endif

** for (myAngle = 10;**

for (myAngle = 10; myAngle <= 160; myAngle+=10)
{
servoPulse(myservo, myAngle);
delay(wait);
distance = analogRead(0);

}

this what I have so far. It scan at 10 to 160. Now how would I write myAngle = 10 is left and myAngle = 160 is right. Thats really the main issues I’m having. I keep on wanting to say myAngle = 10; distance > myAngle = 160; distance turn left. Just that the hard part for me. also I thought about

switch (myAngle)

{

case 10:

if (distance > myangle 160)

turn right

}

I want to put a case inside a case but know it wouldn’t be right. Am I’m kinda on the right track or far off.

It seems you are keeping the

It seems you are keeping the value for only 1 angle. You might want to use an array to store values for each angle sampled in your loop:

int distance[16];

int i = 0;

for (myAngle = 10; myAngle <= 160; myAngle+=10)
{
servoPulse(myservo, myAngle);
delay(wait);
distance[i] = analogRead(0); i++;

}

switch (myAngle)

{

case 10:

if (distance[0] > distance[15]) //distance[0] = 10deg measurement and distance[15] = 160deg measurement

turn right

break;

}

thanks, I was going to do
thanks, I was going to do this. I figure it seem like the only way but wasn’t sure if it would work like that. Now I know even more stuff that I can do to the switch/case that I didn’t think was possible. Thanks a bunch. I’m going to test it all out.

I’m still having a bit of an
I’m still having a bit of an issues with choosing the right direction. I did put it in array. I also gave it case 10 to turn left if 10deg was greater the 160 deg and case 160 to turn right if 160 degree was greater then 10 deg. What it seem like its doing is scaning left then right and then it will just turn right all the time. Seem like it just jump to case 160 instead of taking a reading and comparing it. What could be the problem? I just been trying a few different things out so not sure.