hey guys. I am currently creating a weather display that will light up different neopixels based on the weather. for example if its sunny they light up yellow, overcast is all white. it takes data from weather underground json and parses it.
The problem lies in when i have rain and snow. With the rain and snow, they are not static LEDs and need a loop to simulate raindrops or snow falling. I have it working fine. When its sunny it goes to yellow. then it starts raining switches on the rain but its stuck within that loops and i can't wrap my mind around how to get out of it.
below is the rain and snow function. based on weather (ha get it?) or not its snowing or raining light or heavy it blinks the leds in random faster or slower. So the problem lies in the while statement that I have. So without the while statement it lights 1 LED and doesn't do anything else. so i have to have it in. Currently weather type and speed are passed in via a if then in another function or if it snows then its "snow" "fast" etc. the srCheck gets updated by the function that parses the json
If you want I can post the whole code but it is quite a lot and not well written or formatted it all so everyone will probably cringe here.
The only time it is able to escape the rainandsnow function is when it crashes which it does regularly as from what I have read it detects an infinite loop and soft kills itself. any suggestions at all? I know this is probably much harder to troubleshoot than the standard questions but i have spent the past 3 days trying to think of ways to pass data into it that gets updated but im not good enough to think of a solution. I thought of the srCheck last night and that got me a little closer but now im running into the crashing due to infinite loop task and never updating.
if statement
if (forecast.equalsIgnoreCase(rain))
{
Serial.println("precipitation in the forecast today");
const char *fast = "fast";
const char *rain = "rain";
SnowandRain(rain, fast);
snow/rain function
void SnowandRain(const char *WeatherType, const char *Speed)
{
Serial.println("lol");
Serial.println(srCheck);
Serial.println("lol");
float fadeRate = .98;
while (srCheck = WeatherType)
{
if (Speed == "fast")
{
fadeRate = 0.93;
}
if (Speed == "slow")
{
fadeRate = 0.97;
}
//debug what is being passed
Serial.println(WeatherType);
Serial.println(Speed);
while (1 != 2){
if (random(PIXEL_COUNT) == 1) {
uint16_t i = random(PIXEL_COUNT);
if (redStates[i] < 1 && greenStates[i] < 1 && blueStates[i] < 1 && whiteStates[i] < 1)
{
redStates[i] = 0;
greenStates[i] = 0;
//changes to blue if rain, changes to white if snow
if (WeatherType == "snow")
{
blueStates[i] = 0;
whiteStates[i] = random(256);
}
if (WeatherType == "rain")
{
blueStates[i] = random(256);
whiteStates[i] = 0;
}
}
}
for(uint16_t l = 0; l < PIXEL_COUNT; l++)
{
if (redStates[l] > 1 || greenStates[l] > 1 || blueStates[l] > 1 || whiteStates[l] > 1)
{
pixels.setPixelColor(l, redStates[l], greenStates[l], blueStates[l], whiteStates[l]);
if (redStates[l] > 1)
{
redStates[l] = redStates[l] * fadeRate;
}
else
{
redStates[l] = 0;
}
if (greenStates[l] > 1)
{
greenStates[l] = greenStates[l] * fadeRate;
}
else
{
greenStates[l] = 0;
}
if (blueStates[l] > 1)
{
blueStates[l] = blueStates[l] * fadeRate;
}
else
{
blueStates[l] = 0;
}
if (whiteStates[l] > 1)
{
whiteStates[l] = whiteStates[l] * fadeRate;
}
else
{
whiteStates[l] = 0;
}
}
else
{
pixels.setPixelColor(l, 0, 0, 0);
}
}
pixels.show(); // light up the pixels
if (Speed == "fast")
{
delay(6);
}
if (Speed == "slow")
{
delay(15);
}
}
}
}
weather handler from the json
void handleCondition(String weather1)
{
lightPixels(pixels.Color(0, 0, 0, 0)); // reset all pixels to off
String forecast = weather1;
//parses json string data from the weather condition
String rain = String("Rain");
String overcast = String("Overcast");
//String lightrain = String("Light Rain");
String rainshower = String ("Rain Shower");
String snow = String("Snow");
String cloudy = String("Mostly Cloudy");
String mostlycloudy = String("Mostly Cloudy");
String partlycloudy = String("Partly Cloudy");
String clearsky = String("Clear");
String sunny = String("Sunny");
String rainandsnow = String("Rain and Snow");
String snowshower = String("Snow Shower");
// These if statements compare the incoming weather variable to the stored conditions, and control the NeoPixels accordingly.
// if there's rain in the forecast, tell the the first four pixels to be blue and the middle four pixels to be white (but don't draw them yet)
if (forecast.equalsIgnoreCase(rain))
{
Serial.println("precipitation in the forecast today");
const char *fast = "fast";
const char *rain = "rain";
SnowandRain(rain, fast);
//pixels.show();
}
// if there's snow in the forecast, tell the the first four pixels to be whiteish blue and the middle four pixels to be white (but don't draw them yet)
if (forecast.equalsIgnoreCase(snow) || forecast.equalsIgnoreCase(rainandsnow) || forecast.equalsIgnoreCase(snowshower))
{
Serial.println("precipitation in the forecast today");
// SnowandRain("snow");
/* pixels.setPixelColor(0, pixels.Color(0, 30, 175, 100));
pixels.setPixelColor(1, pixels.Color(0, 30, 175, 100));
pixels.show();
}
// if there's sun in the forecast, tell the last four pixels to be yellow (but don't draw them yet)
if (forecast.equalsIgnoreCase(clearsky) || forecast.equalsIgnoreCase(sunny))
{
Serial.println("some kind of sun in the forecast today");
PixelSet(2,255,128,0,0); // section, green,red,blue,white
PixelSet(3,255,128,0,0);
pixels.show();// light up the pixels
}
// if there's sun in the forecast, tell the last four pixels to be yellow (but don't draw them yet)
if (forecast.equalsIgnoreCase(overcast))
{
Serial.println("no sun all overcast");
PixelSet(1,0,0,0,255); // section, green,red,blue,white
PixelSet(2,0,0,0,255);
PixelSet(3,0,0,0,255);
pixels.show();// light up the pixels
}
// if its partly cloudy has 2 yellow and 1 white (but don't draw them yet)
if (forecast.equalsIgnoreCase(partlycloudy))
{
Serial.println("partly cloudy forecast today");
PixelSet(2,255,128,0,0);
PixelSet(3,255,128,0,0);
PixelSet(1,0,0,0,255);
pixels.show(); // light up the pixels
}
// if its mostly cloudy lights up 1 sun and 2 white (but don't draw them yet)
if (forecast.equalsIgnoreCase(mostlycloudy))
{
Serial.println("mostly cloudy forecast today");
PixelSet(3,255,128,0,0);
PixelSet(2,0,0,0,255);
PixelSet(1,0,0,0,255);
pixels.show(); // light up the pixels
}
}