Is buffer overflow related to WiFi strength?

Just some observations. These may/may not be the problem, but could maybe help you find it.

In your serial prints that you showed above. In the second scenario, when you are mixing drink_type2, the serial print shows the drink_time2 as being 0. Shouldn’t this have a time as this is the drink being mixed? I would expect to see the same number as drink_time1.

Also, I do not see the need to delete the timers, when using the setTimeout function. It is my understanding that when using the setTimeout function it deletes itself once finished. This may be (or not) causing some conflict when the next timer is created. I would try something like this:

case 1:
    {
      digitalWrite(drink_pump1, HIGH);
      timer.setTimeout(drink_time1, []()
      {
        digitalWrite(drink_pump1, LOW);
      }); 
      break;
    } 

Okay, your problem is a typo here.

As @Toro_Blanco said…

drink_time2 is being calculated on the second line of this code, the re-calculated on the third line. Because of the way that you’ve structured your code drink_type3 is zero, so the result is zero, which is throwing an Invalid Pointer error.

In the same way, when you select type 3, there is no calculation of drink_time3, so it’s zero and throws the same error.

You really should structure this code in a better way, as I said before.

Pete.

Thanks Pete. Sorry for the typo.
Now the program is work in any cases by using mobile application.

However, I found that if I send the value through HTTP API, the nested BLYNK_WRITE function cannot be run.

Program logic:
1.Send value [a,b] to V0
2. send a to V16 , b to V17 by Blynk.virtualWrite (V16 and V17 are for my manual test)
3. run V16 function and V17 function
However, when using HTTP API, the program stopped in Step 1.
BLYNK_WRITE(V16 / V17) has not run.
Shouldn’t BLYNK_WRITE(V16, V17) will be called when V0 value is changed?

I solved it by deleting BLYNK_WRITE(V16 / V17) and merged all codes with BLYNK_WRITE(V0), I would like to know it can’t call the nested BLYNK_WRITE function by the HTTP API?


BLYNK_WRITE(V0) //Receive order information from UI through Restful HTTPAPI
{
  type_no = param[0].asInt();
  cup_no = param[1].asInt();
 
Blynk.virtualWrite(V17, type_no);
Blynk.virtualWrite(V16, cup_no);
  
    Serial.print("type_no is  ");
  Serial.println(type_no);
    Serial.print("cup_no is  ");
  Serial.println(cup_no);
}

BLYNK_WRITE(V16) //find what cup size the client chosen
{
int small = 237; //8oz
int medium = 355; //12oz
int large = 473; //16oz
  cup_no = param.asInt();
  switch(cup_no)
  {
    case 1:
    {
      cup_size = small;
      break;
    }
    case 2:
    {
      cup_size = medium;
      break;
    }
    case 3:
    {
      cup_size = large;
      break;
    }
    case 0:
    { 
      cup_size = 0;
      break;
    }
  }
  Serial.println(cup_size);
}

BLYNK_WRITE(V17) //find what drink has selected and calculate the drink ratio and water ratio to mix the drink
{
  float drink1_ratio = 1.0 / (1.0 + 9.0);
  float drink2_ratio = 1.0 / (1.0 + 8.0);
  float drink3_ratio = 1.0 / (1.0 + 7.0);
  float drink4_ratio = 1.0 / (1.0 + 6.0);
  float drink5_ratio = 1.0 / (1.0 + 5.0);
  float drink6_ratio = 1.0 / (1.0 + 4.0);
  type_no = param.asInt();
  switch(type_no)
  {
    case 1:
    {
      drink_type1 = drink1_ratio; //apple
      drink_type2 = 0;
      drink_type3 = 0;
      drink_type4 = 0;
      drink_type5 = 0;
      drink_type6 = 0;
      break;
    }
    case 2:
    {
      drink_type1 = 0; //orange
      drink_type2 = drink2_ratio;
      drink_type3 = 0;
      drink_type4 = 0;
      drink_type5 = 0;
      drink_type6 = 0;
      break;
    }
    case 3:
    {
      drink_type1 = 0; //orange
      drink_type2 = 0;
      drink_type3 = drink3_ratio;
      drink_type4 = 0;
      drink_type5 = 0;
      drink_type6 = 0;
      break;
    }
    case 4:
    {
      drink_type1 = 0;//apple
      drink_type2 = 0;
      drink_type3 = 0;
      drink_type4 = drink4_ratio;
      drink_type5 = 0;
      drink_type6 = 0;
      break;
    }
    case 5:
    {
      drink_type1 = 0;; //orange
      drink_type2 = 0;
      drink_type3 = 0;
      drink_type4 = 0;
      drink_type5 = drink5_ratio;
      drink_type6 = 0;
      break;
    }
    case 6:
    {
      drink_type1 = 0; //orange
      drink_type2 = 0;
      drink_type3 = 0;
      drink_type4 = 0;
      drink_type5 = 0;
      drink_type6 = drink6_ratio;
      break;
    }
    case 9:
    {
      drink_type1 = 0;//apple
      drink_type2 = 0;
      drink_type3 = 0;
      drink_type4 = drink4_ratio / 2.0;
      drink_type5 = drink5_ratio / 2.0;
      drink_type6 = 0;
      break;
    }
    case 10:
    {
      drink_type1 = 0;//apple
      drink_type2 = 0;
      drink_type3 = 0;
      drink_type4 = drink4_ratio / 2.0;
      drink_type5 = 0;
      drink_type6 = drink6_ratio / 2.0;
      break;
    }
    case 11:
    {
      drink_type1 = 0;//apple
      drink_type2 = 0;
      drink_type3 = 0;
      drink_type4 = 0;
      drink_type5 = drink5_ratio / 2.0;
      drink_type6 = drink6_ratio / 2.0;
      break;
    }
    case 15:
    {
      drink_type1 = 0;//apple
      drink_type2 = 0;
      drink_type3 = 0;
      drink_type4 = drink4_ratio / 3.0;
      drink_type5 = drink5_ratio / 3.0;
      drink_type6 = drink6_ratio / 3.0;
      break;
    }
    case 0:
    { drink_type1 = 0;
      drink_type2 = 0;
      drink_type3 = 0;
      drink_type4 = 0;
      drink_type5 = 0;
      drink_type6 = 0;
      break;
    }
  }
  water_ratio = 1.0 - drink_type1 - drink_type2 - drink_type3 - drink_type4 - drink_type5 - drink_type6;
  Serial.print("drink_type1 =");
  Serial.println(drink_type1);
  Serial.print("drink_type2 =");
  Serial.println(drink_type2);
  Serial.print("drink_type3 =");
  Serial.println(drink_type3);
  Serial.print("drink_type4 =");
  Serial.println(drink_type4);
  Serial.print("drink_type5 =");
  Serial.println(drink_type5);
  Serial.print("drink_type6 =");
  Serial.println(drink_type6);

}

Thank you Toro.
Your suggestion reduces my code length.

I still have absolutely no idea what type of widget is attached to pin V0, or how that widget is set-up within your app.
Does the code work correctly when V0 is used in the app?

Pete.

Also, a Blynk.virtualWrite(V17, type_no); and Blynk.virtualWrite(V16, cup_no); called from within the code wont trigger a BLYNK_WRITE(V17) and BLYNK_WRITE(V16) callback because this could create an endless loop within the code.
The correct method is to wrote the values to V17 and V16 then do a Blynk.syncVirtual(V17) and Blynk.syncVirtual(V16) which will cause the respective callbacks to fire.
But, once again, this isn’t a good way to structure your code or to use the API calls.

Pete.

In the final stage of project, BLYNK_WRITE(V16 & V17) will be deleted.
Only BLYNK_WRITE(V0) will exist, so is it okay for using API calls?