Go Down

Topic: switch/case (Read 1 time) previous topic - next topic

jmnijsse

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

magnethead794


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

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

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

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

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?

magnethead794


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

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

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 pm Last 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


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

You want to use a convoluted function to divide by 30? Whatever floats your boat, I guess.

pYro_65

Division is slower than multiplication.

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

PaulS

Quote
Division is slower than multiplication.

So? Have you looked at the map function?

pYro_65

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