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").
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.
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.
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.
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,
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.
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.
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 ) );
};
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.
Would this be an instance to use map()? Something like:
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.
jmnijsse:
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").
I'm not sure I recommend this, but this compiles and outputs ZW:
I only found out about it on this forum. Usually I would suggest if you want to do something like that you should redesign. But it does answer the original question. And a peek at the generated code is interesting. This code:
It looks like it is doing subtractions rather than generating all 11 possibilities. Although I don't quite see how they get away with a single compare to check both an upper and lower range.
A slightly different test, namely:
void test (int wind_direction)
{
Serial.println (wind_direction);
switch (wind_direction)
{
case 25 ... 35:
Serial.println("passed");
break;
default:
Serial.println("failed");
break;
}
}
void setup ()
{
Serial.begin (115200);
test (20);
test (24);
test (25);
test (34);
test (35);
test (36);
}
void loop () { }