Pages: [1] 2   Go Down
Author Topic: switch/case  (Read 1301 times)
0 Members and 1 Guest are viewing this topic.
Netherlands
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Ft. Worth, Texas
Offline Offline
God Member
*****
Karma: 0
Posts: 591
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

KF5RVR

Netherlands
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Shannon Member
****
Karma: 159
Posts: 10409
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can't mean "case '30':", perhaps you mean "case 30:" ??

Sounds like you need "if (wind_direction >= 25 && wind_direction <= 35)"
Logged

[ I won't respond to messages, use the forum please ]

New Jersey
Offline Offline
Faraday Member
**
Karma: 48
Posts: 3393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Netherlands
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Ft. Worth, Texas
Offline Offline
God Member
*****
Karma: 0
Posts: 591
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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;

}
Logged

KF5RVR

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24298
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You could put the ranges and direction string into an array of structs, then simply loop through the array.
Logged

"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.

New Jersey
Offline Offline
Faraday Member
**
Karma: 48
Posts: 3393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 52
Posts: 1778
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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 ) );
};

« Last Edit: December 16, 2011, 09:23:20 am by pYro_65 » Logged


Erie, Pa.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 53
Fair warning: I'm new, and I'll say stupid things.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
map( wind_direction, 0, 359, 0, 11 );
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 52
Posts: 1778
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Division is slower than multiplication.

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


Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Division is slower than multiplication.
So? Have you looked at the map function?
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 52
Posts: 1778
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Pages: [1] 2   Go Up
Jump to: