This is the variable I want to make global / reachable within the SnowandRain function. This is pulled from weather undground json format and formatted into what I need it.
const char *weather = current["weather"];
This is the while loop
while( Weather(this is the outside function) = WeatherType )
{ do the function below
}void SnowandRain(String WeatherType, String Speed)
{
float fadeRate = .98;
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);
}
}while (showWeather.weather = WeatherType)
{
do code
}
bool showWeather(char *json)
{
uint8_t data[] = { 0xff, 0xff, 0xff, 0xff };
StaticJsonBuffer<3*1024> jsonBuffer;
// Skip characters until first '{' found
// Ignore chunked length, if present
char *jsonstart = strchr(json, '{');
//Serial.print(F("jsonstart ")); Serial.println(jsonstart);
if (jsonstart == NULL)
{
Serial.println(F("JSON data missing"));
return false;
}
json = jsonstart;
// Parse JSON
JsonObject& root = jsonBuffer.parseObject(json);
if (!root.success())
{
Serial.println(F("jsonBuffer.parseObject() failed"));
return false;
}
// Extract weather info from parsed JSON
JsonObject& current = root["current_observation"];
const float temp_f = current["temp_f"];
Serial.print(temp_f, 1); Serial.print(F(" F, "));
const float temp_c = current["temp_c"];
Serial.print(temp_c, 1); Serial.print(F(" C, "));
String temp_c_string = current["temp_c"];
const char *humi = current[F("relative_humidity")];
Serial.print(humi); Serial.println(F(" RH"));
const char *weather = "Rain"; //current["weather"];
//test diff weather patterns with const char *weather = "Suy"; or w/e one you want
Serial.println(weather);
const char *pressure_mb = current["pressure_mb"];
Serial.println(pressure_mb);
const char *observation_time = current["observation_time_rfc822"];
Serial.println(observation_time);
// Extract local timezone fields
const char *local_tz_short = current["local_tz_short"];
Serial.println(local_tz_short);
const char *local_tz_long = current["local_tz_long"];
Serial.println(local_tz_long);
const char *local_tz_offset = current["local_tz_offset"];
Serial.println(local_tz_offset);
int temp_string_length = temp_c_string.length();
//checks to see what type of temperature it is
if (temp_string_length == 3)
{
Serial.println("its single digits positve");
float temp_c_single_p = temp_c;
int temp_c_single_p_add = temp_c_single_p *10;
int temp_c_single_p_2nd_digit = temp_c_single_p_add - (floor(temp_c_single_p) *10 );
int temp_decimal = floor(temp_c_single_p);
//segments left to right
data[2] = B00000000;
data[1] = B00000000;// blank the digit
data[0] = DecimalNumber(temp_decimal); //gets number with dec point in raw form
data[5] = display.encodeDigit(temp_c_single_p_2nd_digit);
data[4] = B00111001;// C letter
data[3] = B01100011; //degree sign
display.setSegments(data);
}
else if (temp_string_length == 4)
{
if (temp_c_string.startsWith("-"))
{
Serial.println("its single digit negative");
float temp_c_single_n = temp_c;
int temp_c_single_n_1st_digit = temp_c;
int temp_c_single_n_add = temp_c_single_n *10;
int temp_c_single_n_2nd_digit = temp_c_single_n_add - (ceil(temp_c_single_n) *10 );
int temp_decimal = ceil(temp_c_single_n_1st_digit*-1);
data[2] = B00000000;
data[1] = B01000000 ; // neg sign
data[0] = DecimalNumber(temp_decimal);//grabs the number in raw form with decimal
data[5] = display.encodeDigit(temp_c_single_n_2nd_digit*-1);
data[4] = B00111001;
data[3] = B01100011;//degree sign
Serial.println(temp_c_single_n);
Serial.println(temp_c_single_n_1st_digit);
Serial.println(temp_c_single_n_2nd_digit);
Serial.println(floor(temp_c_single_n_1st_digit*-1));
Serial.println(DecimalNumber(temp_decimal));
display.setSegments(data);
}
else
{
Serial.println("yay its double digit positive");
float temp_c_double_p = temp_c * 10;
int temp_c_double_p_final = floor(temp_c_double_p/100);
int temp_c_double_p_final_2nd_digit = floor((temp_c_double_p -(temp_c_double_p_final * 100))/10);
int temp_c_double_p_final_3rd_digit = (temp_c_double_p -(floor(temp_c)*10));
int temp_decimal = floor(temp_c_double_p_final_2nd_digit);
data[2] = B00000000;
data[1] = display.encodeDigit(temp_c_double_p_final);
data[0] = DecimalNumber(temp_decimal);
data[5] = display.encodeDigit(temp_c_double_p_final_3rd_digit);
data[4] = B00111001;
data[3] = B01100011;
Serial.println(temp_c_double_p);
Serial.println(temp_c_double_p_final);
Serial.println(temp_c_double_p_final_2nd_digit);
display.setSegments(data);
}
}
else if(temp_string_length == 5)
{
Serial.println("its double digits negative");
float temp_c_double_n = temp_c * 10;
int temp_c_double_n_final = ceil(temp_c_double_n/100);
int temp_c_double_n_final_2nd_digit = ceil((temp_c_double_n -(temp_c_double_n_final * 100))/10);
int temp_c_double_n_final_3rd_digit = (temp_c_double_n -(ceil(temp_c)*10));
int temp_decimal = ceil(temp_c_double_n_final_2nd_digit*-1);
data[2] = B01000000 ; //neg sign
data[1] = display.encodeDigit(temp_c_double_n_final*-1);
data[0] = DecimalNumber(temp_decimal);
data[5] = display.encodeDigit(temp_c_double_n_final_3rd_digit *-1);
data[4] = B00111001;
data[3] = B01100011;
Serial.println(temp_c_double_n);
Serial.println(temp_c_double_n_final);
Serial.println(temp_c_double_n_final_2nd_digit);
display.setSegments(data);
}
//starts weather of leds
handleCondition(weather);
return true;
}
برچسب:
نویسنده: آیلین رضوانی