Go Down

### Topic: switch/case (Read 2119 times)previous topic - next topic

#### jmnijsse

##### Dec 16, 2011, 12:58 pm
Hello,

I had the following code (not complete):

switch (wind_direction)
{
case'30':
Serial.print("ZW");
break;
}

Is it possible to define the '30' as a value of 25 - 35?
The value 30 is in some circumstances a little bit lower or higher, but in all this range he must Serial.print("ZW").

Ideas?

Gr.
Johan

#1
##### Dec 16, 2011, 01:00 pm

Hello,

I had the following code (not complete):

switch (wind_direction)
{
case'30':
Serial.print("ZW");
break;
}

Is it possible to define the '30' as a value of 25 - 35?
The value 30 is in some circumstances a little bit lower or higher, but in all this range he must Serial.print("ZW").

Ideas?

Gr.
Johan

You would be better off using if statements if you have to check range. 99.2% sure switch case is steady-state.
KF5RVR

#### jmnijsse

#2
##### Dec 16, 2011, 01:10 pm
Yes, we do in the first time.
But we have twelve situations (its a winddirectionflag, contacted with reedcontacts and different resistors).
So, i thought, the if-solution became a long code.

J.

#### MarkT

#3
##### Dec 16, 2011, 01:10 pm
You can't mean "case '30':", perhaps you mean "case 30:" ??

Sounds like you need "if (wind_direction >= 25 && wind_direction <= 35)"
[ I won't respond to messages, use the forum please ]

#### wildbill

#4
##### Dec 16, 2011, 01:18 pm
Is the output of the reed switches and resistors a voltage that you get with analogread and that is the need for a range to test for?

#### jmnijsse

#5
##### Dec 16, 2011, 01:24 pm
Yes, I read a voltage of the resistor.

With the if-solution:
Have i write it like this:

if (wind_direction >= 25 && wind_direction <= 35) {
Serial.print("ZW");
}
if (wind_direction >=40 && wind_direction <=50) {
Serial.print("W")
}
...and this twelve times?

#6
##### Dec 16, 2011, 01:32 pm

Yes, we do in the first time.
But we have twelve situations (its a winddirectionflag, contacted with reedcontacts and different resistors).
So, i thought, the if-solution became a long code.

J.

This will look redundant with If and switch, but will be much cleaner- particularly if you're doing the same thing, but with different numbers.

Use 12 if statements to set an int, then switch on that int. IE,

Code: [Select]
`if ((-15 <= wind_direction) && ((wind_direction <= 15)){evalDir = "N"} else if ((16 <= wind_direction) && ((wind_direction <= 30)){evalDir = "NNE"} else if ((31 <= wind_direction) && ((wind_direction <= 60)){evalDir = "NE"} else if ((61 <= wind_direction) && ((wind_direction <= 75)){evalDir = "ENE"} else if ((76 <= wind_direction) && ((wind_direction <= 105)){evalDir = "E"} else if ((106 <= wind_direction) && ((wind_direction <= 120)){evalDir = "ESE"} else if ((121 <= wind_direction) && ((wind_direction <= 150)){evalDir = "SE"} else if ((151 <= wind_direction) && ((wind_direction <= 165)){evalDir = "SSE"} else if ((166 <= wind_direction) && ((wind_direction <= 195)){evalDir = "S"} else if ((196 <= wind_direction) && ((wind_direction <= 210)){evalDir = "SSW"} else if ((211 <= wind_direction) && ((wind_direction <= 240)){evalDir = "SW"} else if ((241 <= wind_direction) && ((wind_direction <= 255)){evalDir = "WSW"} else if ((256 <= wind_direction) && ((wind_direction <= 285)){evalDir = "W"} else if ((286 <= wind_direction) && ((wind_direction <= 300)){evalDir = "WNW"} else if ((301 <= wind_direction) && ((wind_direction <= 330)){evalDir = "NW"} else if ((331 <= wind_direction) && ((wind_direction <= 345)){evalDir = "NNW"} else if ((346 <= wind_direction) && ((wind_direction <= 360)){evalDir = "N"}switch (evalDir){case n:serial.print(evalDir);break;}`
KF5RVR

#### AWOL

#7
##### Dec 16, 2011, 01:33 pm
You could put the ranges and direction string into an array of structs, then simply loop through the array.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#### wildbill

#8
##### Dec 16, 2011, 01:53 pm
What are the actual ranges? It might be possible to reduce them with a simple formula to give you an int in the 0 to 11 range to use as an index into a string array.

#### pYro_65

#9
##### Dec 16, 2011, 02:32 pmLast Edit: Dec 16, 2011, 03:23 pm by pYro_65 Reason: 1
Oops, went to modify my post and removed it by accident.

Wildbill is right, no if's or switch needed.

This is assuming the values you read are relative to degrees.

Code: [Select]
`const char *c_Text[] = { "N", "NNE",  "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW", "N" };int deg; //Provided from sensor.#define WHICH_DIRECTION( sensor_degrees )        ( c_Text[ ( int ) ( ( 17.0f / 360.0f )  * ( float ) sensor_degrees ) ] )  #define WHICH_DIRECTION__SAFE( sensor_degrees )  ( c_Text[ ( int ) ( ( ( 17.0f / 360.0f )  * ( float ) sensor_degrees ) - 0.0001f ) ] )  void loop(){  //Get value into deg  deg = 34;  //If sensor data is in range 0 to 359  Serial.print( WHICH_DIRECTION( deg ) );  //If you receive a value of 360 instead of 0, use this instead. ( prevents overflow ).  //Shouldn't need this. A sensor should only give one result for a discrete position.  Serial.print( WHICH_DIRECTION__SAFE( deg ) );};`

#### ryschwith

#10
##### Dec 17, 2011, 12:11 am

What are the actual ranges? It might be possible to reduce them with a simple formula to give you an int in the 0 to 11 range to use as an index into a string array.

Would this be an instance to use map()? Something like:

Code: [Select]
`map( wind_direction, 0, 359, 0, 11 );`

#### PaulS

#11
##### Dec 17, 2011, 02:29 am
You want to use a convoluted function to divide by 30? Whatever floats your boat, I guess.

#### pYro_65

#12
##### Dec 17, 2011, 02:54 am
Division is slower than multiplication.

( 17.0f / 360.0f ) is a compile time constant, only operation being emitted is 0.0472 * deg

#### PaulS

#13
##### Dec 17, 2011, 03:11 am
Quote
Division is slower than multiplication.

So? Have you looked at the map function?

#### pYro_65

#14
##### Dec 17, 2011, 03:42 am
Nope, didn't realise you were referring to it.

Have now tho. map contains an operation not needed for this topic. Division would be faster due to the overhead and the fact that gcc will optimise most / operations to * operations.

Go Up

Please enter a valid email to subscribe

To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy