Weight values not updating when timer is on

So I’m making a cat feeder with a servo controlled auger.
I’m using the timer widget to start a feed at a specific time. I want it to stop when I reach a certain weight.
However when the timer is on it’s not updating the weight values of the bowl continuously, what would I need to add into my code to get it to read it?

I can already read the weight on blynk it’s just when the timer is ON it stops updating the weight until its off.

I’m using LoLin NodeMCU V3 ESP8266, Hx711, load cell, and a 360°servo.

float weightInput;
BLYNK_WRITE(V2) //weight input, the value we want the weight to reach when the servo is turned on with timer widget
{
   weightInput = param.asInt();
   delay(10000);
    
}

float weight=0;
void weightInBowl()
{
 weight=scale.get_units(50);        
 Blynk.virtualWrite(V4, weight);  
  
}

 
 BLYNK_WRITE(V3)//timer widget, turns on servo
{
 

    while (weight < weightInput)  //want it to keep going until desired weight (weightInput) is reached.
    {
      if (param.asInt())
      {   
         
         servo.write(180); //  servo is moving       
      }

       else 
      {
        
        servo.write(90); // 360°servo is stop at 90
      }
    } 
}

I assume that there’s some sort of timer or loop that’s calling void weightInBowl() on a regular basis, but you’ve not shown that.

This while loop is a blocking function, which is probably why you aren’t taking weight readings when its executing.

as a 10 second delay is often enough to cause a Blynk disconnection and it’s not really clear why you want to pause (or should that be paws :smile_cat:) the code execution when you input a new weight target value.

Pete.

1 Like

Thanks for the reply!

I do indeed have a timer loop checking the weightInBowl function and as for the 10 second delay that was simply a typo :sweat_smile: .

I solved the problem if you’re interested you can check the code below :slightly_smiling_face:


float weight=0;
int checktime=0; 
void setup()
{
    Serial.begin(115200);// Debug console
    Blynk.begin(auth, ssid, pass);
   
    scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
    scale.set_scale(3391.0f);    
    Serial.println(scale.read());

    timer.setInterval(1000L, weightInBowl);
    servo.attach(12);
    servo.write(90);   
    

    

}




BLYNK_WRITE(V1)//manually turn on servo
{
  
   servo.write(param.asInt());
   delay(100);
}






 BLYNK_WRITE(V3)//timer widget
{
 
  checktime=param.asInt();
} 


float weightInput;
BLYNK_WRITE(V2) //desired weight from user
{
   weightInput = param.asInt();
   
}
  




void weightInBowl()
{
 weight=scale.get_units(50);        
 Blynk.virtualWrite(V4, weight);

 if (checktime ==1)
 
 {
  if (weight < weightInput)
  
  {
    servo.write(180);
  }
  if (weight > weightInput)
  {
    servo.write(90);
  }
 }
}   

 
 void loop()
{  

  Blynk.run();
  timer.run();
}

That means that you’re potentially topping-up the bowl once every second, and you have a timer widget that’s not being used.

Personally, I’d have gone for a system that checks the weight of the bowl quite infrequently on a timer(maybe every 3 hours), then checks the weight once every second when its being filled, by calling the check weight routine from within a bowl filling routine.

Have you seen this project:

Pete.

The timer widget is being used. I store the on/off value into checktime and then I plug the value into the weightInBowl function.
This way the servo turns on at the right time and the weight values are updated continuously. The servo then turns off when it reaches the value the user entered into V3. The system works but I do see your point, maybe I’ll streamline my project a bit using your suggestions.

I haven’t seen the project you posted, will definitely take a look!

Of course, I missed that!

Pete.