خرید بک لینک
Hey guys me again
I am working on a separate project now. A weather visualization program with neopixels on a ESP8266. I have somewhat of a problem I don't know how to fix. So i have a function I call that has to go in a loop else the led's dont fade in and out like they should. so in the code below I put a while loop in there infinitely. The obviously problem is it never exits the loops and my ESP crashes as it detects an infinite loop. However my problem is I need to compare it to a variable outside of the function and I don't know how. i had tried with extern and #define but it just hates on anything i do and doesn't compile any suggestions? below is the function that runs the loop. What I need to replace is the while 1 != 2 loop with something like

any suggestions?
Code:

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
}

function
Code:

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

An update to this. I still haven't got it working but I have been doing more research since I am not that good at programming. From what I understand any variable declared outside a function is a global variable by default. My problem is the variable that I need is within a function below is the code for the weather function i need. I tried to call it like showWeather.weather but it complains about "request for member 'weather' in 'showWeather', which is of non-class type 'bool(char*)'"
Code:

while (showWeather.weather = WeatherType)
{
do code
}

code for the function of ShowWeather
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;
}

برچسب: نویسنده: آیلین رضوانی تاريخ: سه شنبه 10 بهمن 1396 ساعت: 16:45

صفحه بندی