Char Function

Hi Guys,

How can i put this if statement into a function and get back a char value which i can read in the void loop and print on serial monitor

if (((Wind_Direction > 348.75) && (Wind_Direction < 11.26)) || (Wind_Direction == 0.00))
{
Serial.println(“N”);
}
else if ((Wind_Direction > 11.25) && (Wind_Direction < 33.76))
{
Serial.println(“NNE”);
}
else if ((Wind_Direction > 33.75) && (Wind_Direction < 56.26))
{
Serial.println(“NE”);
}
else if ((Wind_Direction > 56.25) && (Wind_Direction < 78.76))
{
Serial.println(“ENE”);
}
else if ((Wind_Direction > 78.75) && (Wind_Direction < 101.26))
{
Serial.println(“E”);
}
else if ((Wind_Direction > 101.25) && (Wind_Direction < 123.76))
{
Serial.println(“SE”);
}
else if ((Wind_Direction > 123.25) && (Wind_Direction < 146.76))
{
Serial.println(“SE”);
}
else if ((Wind_Direction > 146.25) && (Wind_Direction < 168.76))
{
Serial.println(“SSE”);
}
else if ((Wind_Direction > 168.25) && (Wind_Direction < 191.76))
{
Serial.println(“S”);
}
else if ((Wind_Direction > 191.25) && (Wind_Direction < 213.76))
{
Serial.println(“SSW”);
}
else if ((Wind_Direction > 213.25) && (Wind_Direction < 236.76))
{
Serial.println(“SW”);
}
else if ((Wind_Direction > 236.25) && (Wind_Direction < 258.76))
{
Serial.println(“WSW”);
}
else if ((Wind_Direction > 258.25) && (Wind_Direction < 281.76))
{
Serial.println(“W”);
}
else if ((Wind_Direction > 281.25) && (Wind_Direction < 303.76))
{
Serial.println(“WNW”);
}
else if ((Wind_Direction > 303.25) && (Wind_Direction < 326.76))
{
Serial.println(“NW”);
}
else if ((Wind_Direction > 326.25) && (Wind_Direction < 348.76))
{
Serial.println(“NNW”);
}

Thank you in advance :slight_smile:

char *dirName[] = { "N","NNE","NE","ENE","E","ESE","SE","SSE",
"S","SSW", "SW","WSW","W","WNW","NW","NNW"};


int dir = (int)(Wind_Direction/360.0*16.0+.5) % 16;
Serial.println(dirName[dir]);

If your direction thingy might potentially have negative numbers (-180 to +180), do:

int dir = ((int)(Wind_Direction/360.0*16.0+.5) + 16) % 16;

String *dirName[] = { "N","NNE","NE","ENE","E","ESE","SE","SSE", "S","SSW", "SW","WSW","W","WNW","NW","NNW"};

You misspelled char.

maybe my old thread is an interestig read - https://forum.arduino.cc/index.php?topic=94507.0

BTW

  1. I notice there is overlap in the if statements

else if ((Wind_Direction > 213.25) && (Wind_Direction < 236.76))
{
Serial.println(“SW”);
}
else if ((Wind_Direction > 236.25) && (Wind_Direction < 258.76))

  1. you can reduce 50% of the ifs by starting with smallest

if (wind < 236) …
else if (wind < 258) …
else if (wind < 275) …

the lower values are allready excluded because of the order of the if’s

Thank you so much guys. I will try the suggestion you have made and make my program shorter. I did manage to get the if statemets into a function. I made a mistake using char. I was to use String instead.

This is my working function

//...
//Wind Direction
String getdirection()
{
   Wind_Direction = getWindVane(); //Gets Potentiometer value from getWindVane function
   
 if (((Wind_Direction > 348.75) && (Wind_Direction < 11.26)) || (Wind_Direction == 0.00))
  {
    return direction ='N';
  }
  else if ((Wind_Direction > 11.25) && (Wind_Direction < 33.76))
  {
    return direction="NNE";
  }
  else if ((Wind_Direction > 33.75) && (Wind_Direction < 56.26))
  {
    return direction="NE";
  }
  else if ((Wind_Direction > 56.25) && (Wind_Direction < 78.76))
  {
    return direction="ENE";
  }
  else if ((Wind_Direction > 78.75) && (Wind_Direction < 101.26))
  {
    return direction ='E';
  }
  else if ((Wind_Direction > 101.25) && (Wind_Direction < 123.76))
  {
   return direction ="SE";
  }
  else if ((Wind_Direction > 123.25) && (Wind_Direction < 146.76))
  {
    return direction ="SE";
  }
  else if ((Wind_Direction > 146.25) && (Wind_Direction < 168.76))
  {
    return direction ="SSE";
  }
  else if ((Wind_Direction > 168.25) && (Wind_Direction < 191.76))
  {
    return direction ='S';
  }
  else if ((Wind_Direction > 191.25) && (Wind_Direction < 213.76))
  {
    return direction ="SSW";
  }
  else if ((Wind_Direction > 213.25) && (Wind_Direction < 236.76))
  {
    return direction ="SW";
  }
  else if ((Wind_Direction > 236.25) && (Wind_Direction < 258.76))
  {
    return direction ="WSW";
  }
  else if ((Wind_Direction > 258.25) && (Wind_Direction < 281.76))
  {
    return direction ='W';
  }
  else if ((Wind_Direction > 281.25) && (Wind_Direction < 303.76))
  {
    return direction ="WNW";
  }
  else if ((Wind_Direction > 303.25) && (Wind_Direction < 326.76))
  {
    return direction ='N';
  }
  else if ((Wind_Direction > 326.25) && (Wind_Direction < 348.76))
  {
    return direction ="NNW";
  }
}

and i just call the function in the main loop.

I will try to reduce it thou.

Thanks again,

Regards,
Vishal.

Oh and by the way here is the wind direction code. I am using the Weather meter from sparkfun

// Wind direction (vane) 

//Map potentiometer values to compass direction 
static const int vaneValues[] PROGMEM = {66, 84, 92, 127, 184, 244, 287, 406, 461, 600, 631, 702, 786, 827, 889, 946};
static const int vaneDirections[] PROGMEM = {1125, 675, 900, 1575, 1350, 2025, 1800, 225, 450, 2475, 2250, 3375, 0, 2925, 3150, 2700};

double getWindVane()
{
  analogReference(DEFAULT);
  digitalWrite(VANE_PWR, HIGH);
  delay(100);
  for (int n = 0; n < 10; n++)
  {
    analogRead(VANE_PIN);
  }

  unsigned int reading = analogRead(VANE_PIN);
  digitalWrite(VANE_PWR, LOW);
  unsigned int lastDiff = 2048;

  for (int n = 0; n < 16; n++)
  {
    int diff = reading - pgm_read_word(&vaneValues[n]);
    diff = abs(diff);
    if (diff == 0)
      return pgm_read_word(&vaneDirections[n]) / 10.0;

    if (diff > lastDiff)
    {
      return pgm_read_word(&vaneDirections[n - 1]) / 10.0;
    }

    lastDiff = diff;
  }

  return pgm_read_word(&vaneDirections[15]) / 10.0;

}

The function gives the degree value which i read in the second direction function

//Wind Direction
String getdirection()
{
   Wind_Direction = getWindVane(); //Gets Potentiometer value from getWindVane function
   
 if (((Wind_Direction > 348.75) && (Wind_Direction < 11.26)) || (Wind_Direction == 0.00))
  {
    return direction ='N';
  }
  else if ((Wind_Direction > 11.25) && (Wind_Direction < 33.76))
  {
    return direction="NNE";
  }
  else if ((Wind_Direction > 33.75) && (Wind_Direction < 56.26))
  {
    return direction="NE";
  }
  else if ((Wind_Direction > 56.25) && (Wind_Direction < 78.76))
  {
    return direction="ENE";
  }
  else if ((Wind_Direction > 78.75) && (Wind_Direction < 101.26))
  {
    return direction ='E';
  }
  else if ((Wind_Direction > 101.25) && (Wind_Direction < 123.76))
  {
   return direction ="SE";
  }
  else if ((Wind_Direction > 123.25) && (Wind_Direction < 146.76))
  {
    return direction ="SE";
  }
  else if ((Wind_Direction > 146.25) && (Wind_Direction < 168.76))
  {
    return direction ="SSE";
  }
  else if ((Wind_Direction > 168.25) && (Wind_Direction < 191.76))
  {
    return direction ='S';
  }
  else if ((Wind_Direction > 191.25) && (Wind_Direction < 213.76))
  {
    return direction ="SSW";
  }
  else if ((Wind_Direction > 213.25) && (Wind_Direction < 236.76))
  {
    return direction ="SW";
  }
  else if ((Wind_Direction > 236.25) && (Wind_Direction < 258.76))
  {
    return direction ="WSW";
  }
  else if ((Wind_Direction > 258.25) && (Wind_Direction < 281.76))
  {
    return direction ='W';
  }
  else if ((Wind_Direction > 281.25) && (Wind_Direction < 303.76))
  {
    return direction ="WNW";
  }
  else if ((Wind_Direction > 303.25) && (Wind_Direction < 326.76))
  {
    return direction ='N';
  }
  else if ((Wind_Direction > 326.25) && (Wind_Direction < 348.76))
  {
    return direction ="NNW";
  }
}

and call the Wind Direction function in the main loop and display in serial monitor.

Wind_direction= getdirection();

Serial.print("Wind Direction: ");           
Serial.println(Wind_direction);

So if you guys can reduce it or find errors in my code please elt me know. I really appreciate you help.

Thanks alot
Vishal

I did this for a compass...

const char* getHeading(int heading) {
  static const char* directions[] =  {"NORTH", "NE", "EAST", "SE", "SOUTH", "SW", "WEST", "NW", "NORTH"};
    int index = (heading + 23) / 45;
    return directions[index];
}

void setup()
{
  Serial.begin(9600);
  Serial.println(getHeading(90));   // east
  Serial.println(getHeading(3));    //North
  Serial.println(getHeading(358));  //North
  Serial.println(getHeading(180));  // south
}
void loop()
{

}

I'll leave it to you to math it out for the rest of the ordinals...

PaulS: You misspelled char.

Indeed I did. Fixed. I don't know what I was thinking: I have never used C String.

In any case, OP - do you get what my code is doing? I use floating point to convert degrees to a value between 0 and 16, add .5 for rounding, and then cast down to an int, which truncates the fractional part of the number. The % operator takes care of mapping angles greater than 348.75 back to 'N'. You don't need a big chain of if statements: you can just calculate it with numbers. Numbers are good, it's what computers do.

BulldogLowell: I did this for a compass...

I'll leave it to you to math it out for the rest of the ordinals...

Hey BulldogLowell, Thanks so much man.. All those if statements are now gone. I did manage to figure out the math.

I am currently working on building a weather station, and this was part of the code.

My weather station outputs the following readings

Temperature Humidity Atmospheric Pressure Gas Index Air Quality Visible light IR Light UV light Rainfall Wind speed, direction and wind gust.

You guys have being very helpfull and I really appreciate that you have taken your time out to reply and help me on my work.

And thanks again BulldogLowell for your help.

Regards Vishal