Guidance on tidying / efficiency

Hi All,

I am self taught and really struggle with efficient coding. I found this (code below) online (Becky Stern 2017 - great work btw!) and have adapted it heavily. It is for a weather forecast lamp I am making. The data feed (fed from Adafruit IO) via a weather feed from IFTTT comes in as various keywords into the Arduino, forecasting the weather for the day. The issue is, there are so many random weather keywords that come in (“AM Fog/ PM Sun”) etc that I am struggling to keep up with constantly adding new ones to the sketch as they come in. Sadly I can’t find a published list anywhere (from Weather Underground) so I will have to keep churning away, hopefully exhausting all UK based weather eventualities sooner or later!

Anyhow, How can I adapt the code to allow me just to insert strings as they come in into relevant sections (rain, cloudy, sunny, thunderstorms, etc)? If I get a vague variable “cloudy AM, storms PM”, I would like to slip that into the “clouds” definition without having to manually type out a new line and a new entry for every key wor that comes in.

I am sorry if this doesn’t make much sense, I am really tired, I’ve been messing around with this for hours but have to throw the towel in!

EDIT: it would be most helpful if I could simply stick the commands in to a section as they came in . For example…
Currently the code says: String rain = String(“Rain”);.
In my head, it would make sense if this worked: String rain = String(“Rain”, “Light Rain”, “Heavy Rain”, “Biblical Rain”, “JESUS CHRIST IT’S A MONSOON”, etc); Any string that came in would be inserted in the one line Rain definition. But sadly , I am doing something wrong and this doesn’t work.

// https://www.instructables.com/lesson/Circuit-Displays-Internet-Data/
// this function is called whenever a message
// is received from Adafruit IO.

void handleMessage(AdafruitIO_Data *data) {
  
  clearpixels(); // reset all pixels to off when new info is received
  delay(1500);
  String forecast = data->toString(); // store the incoming weather data in a string
  
  Serial.print("received <- ");
  Serial.println(forecast);
  
  //the following strings store the varous IFTTT weather report words I've discovered so far

  
  String rain = String("Rain");
  String lightrain = String("Light Rain");
  String rainshower = String ("Rain Shower");
  String amshowers = String ("AM Showers");
  String pmshowers = String ("PM Showers");
  String showers = String ("Showers");
  String fewshowers = String ("Few Showers");
  String ice2rain = String ("Ice to Rain");

  String thunderstorms = String("Thunderstorms");
  String thunderstormswind = String("Thunderstorms/Wind");
  String scatteredthunderstorms = String("Scattered Thunderstorms");
  String isolatedthunderstorms = String("Isolated Thunderstorms");
  String amthunderstorms = String("AM Thunderstorms");
  String pmthunderstorms = String("PM Thunderstorms");

  
  String rainandsnow = String("Rain and Snow");
  String rainsnow = String("Rain/Snow");
  String snow = String("Snow");
  String snowshower = String("Snow Shower");
  String snowshowers = String("Snow Showers");
  String amsnowshowers = String("AM Snowshowers");
  String pmsnowshowers = String("PM Snowshowers");
  String fewsnowshowers = String("Few Snowshowers");
  String rainsnowshowers = String("Rain/Snow Snowshowers");
  String lightsnow = String("Light Snow");
  String heavysnow = String("Heavy Snow");
  String wintrymix = String("Wintry Mix");
  String lightwintrymix = String("Light Wintry Mix");
  String showersearly = String("Showers Early");

  String clearsky = String("Clear");
  String fair = String("Fair");
  String sunny = String("Sunny");
  String mostlysunny = String("Mostly Sunny");
  String mostlyclear = String("Mostly Clear");
  String amsunpmclouds = String("AM Sun/PM Clouds");
  
  String cloudy = String("Cloudy");
  String cloudy2 = String("AM Clouds/PM Sun");
  String mostlycloudy = String("Mostly Cloudy");
  String partlycloudy = String("Partly Cloudy");
  String foggy = String("Foggy");
  String cloudywindy = String("Cloudy/Windy");
  String amcloudspmsun = String("AM Clouds/PM Sun");
  String foggy2 = String("AM Fog/PM Clouds");
  

  String test = String("Test");
  String disco = String("Disco");
  String disco2 = String("Disco2");
  String alert = String("Alert");
  String fire = String("Fire");

  // These if statements compare the incoming weather variable to the stored conditions, and control the NeoPixels accordingly.
  
  // if there's rain in the forecast
  if (forecast.equalsIgnoreCase(rain) || forecast.equalsIgnoreCase(lightrain) || forecast.equalsIgnoreCase(rainshower) || forecast.equalsIgnoreCase(showersearly) || forecast.equalsIgnoreCase(showers) || forecast.equalsIgnoreCase(pmshowers) || forecast.equalsIgnoreCase(fewshowers) || forecast.equalsIgnoreCase(ice2rain) ||forecast.equalsIgnoreCase(amshowers)){
    Serial.println("precipitation in the forecast today");
 rain_weather();
  }


    // if there's thunderstorm in the forecast
  if (forecast.equalsIgnoreCase(thunderstorms) || forecast.equalsIgnoreCase(thunderstormswind) || forecast.equalsIgnoreCase(scatteredthunderstorms) || forecast.equalsIgnoreCase(amthunderstorms) ||forecast.equalsIgnoreCase(pmthunderstorms) ||forecast.equalsIgnoreCase(isolatedthunderstorms)){
    Serial.println("precipitation in the forecast today");
    thunderstorm_weather();
  }

  
  // if there's snow in the forecast
  if (forecast.equalsIgnoreCase(snow) || forecast.equalsIgnoreCase(rainandsnow) || forecast.equalsIgnoreCase(rainsnow)|| forecast.equalsIgnoreCase(pmsnowshowers) || forecast.equalsIgnoreCase(lightsnow) || forecast.equalsIgnoreCase(heavysnow) || forecast.equalsIgnoreCase(wintrymix) || forecast.equalsIgnoreCase(lightwintrymix) || forecast.equalsIgnoreCase(amsnowshowers) || forecast.equalsIgnoreCase(fewsnowshowers) ||forecast.equalsIgnoreCase(snowshowers) ||forecast.equalsIgnoreCase(rainsnowshowers) || forecast.equalsIgnoreCase(snowshower)){
    Serial.println("precipitation in the forecast today");
    snow_weather();
  }

  
  // if there's sun in the forecast
  if (forecast.equalsIgnoreCase(clearsky) || forecast.equalsIgnoreCase(fair) || forecast.equalsIgnoreCase(mostlysunny) || forecast.equalsIgnoreCase(mostlyclear) || forecast.equalsIgnoreCase(amsunpmclouds) ||forecast.equalsIgnoreCase(sunny)){
    Serial.println("some kind of sun in the forecast today");
    sun_weather();
  }

    
  // if there're clouds in the forecast
  if (forecast.equalsIgnoreCase(cloudy) || forecast.equalsIgnoreCase(mostlycloudy) || forecast.equalsIgnoreCase(foggy) || forecast.equalsIgnoreCase(foggy2) || forecast.equalsIgnoreCase(cloudy2) || forecast.equalsIgnoreCase(amcloudspmsun) || forecast.equalsIgnoreCase(cloudywindy) ||forecast.equalsIgnoreCase(partlycloudy)){
    Serial.println("cloudy sky in the forecast today");
    //colorWipe(strip.Color(200,   200,   200), 255); // White
     clouds_weather();   
  }

  
  // if there's test in the forecast
  if (forecast.equalsIgnoreCase(test)){
    Serial.println("Test");
    test_strip();
  }


  // if there's disco in the forecast
  if (forecast.equalsIgnoreCase(disco)){
    Serial.println("Disco");
    disco_disco();
  }
  

    // if there's disco2 in the forecast
  if (forecast.equalsIgnoreCase(disco2)){
    Serial.println("Disco2");
    disco2_disco2();
  }

      // if there's fire in the forecast
  if (forecast.equalsIgnoreCase(fire)){
    Serial.println("Fire");
    fuego();
  }

    // if there's alert in the forecast
  if (forecast.equalsIgnoreCase(alert)){
   Serial.println("Alert");
   RedAlert();
  }

}

It looks like your display will not provide a way of displaying a lot of complicated weather patterns. That’s what these strings are doing. But do you really care, I guess not. You would like something abstract and nice looking and ideally easy to understand.

Therefore, I would recommend you simplify the process and search for keywords that are clear and create a led pattern for that.

Use the substring() function to search for snow, rain, sun (no you don’t need that you said UK weather), thunder and a couple of other simple words used, then change the LED colors accordingly.

https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/substring/

coombed2:
I am struggling to keep up with constantly adding new ones to the sketch as they come in.

Just spitballin'. Maybe an SD card, it could hold a ton of data. You'd have to organize it, of course. But then you could automatically add new terms as they come in and in turn search categories to find an exact match.

Klaus_K:
Use the substring() function to search for snow, rain, sun (no you don’t need that you said UK weather),...

:grin: