Blynk.run() interrupting reading values from digitalRead

Hi everyone,
I’m doing a project where I need to know the rpm of a spinning object.
Im using a ESP8266 with and IR sensor.
When I use blynk, the blynk.run() is doing some kind of interrupts to be able to keep the connection to the server alive. This is a problem for me, because I do rpm calculations with time and digitalRead, and blynk interferes with that. This results in that I get my correct value, but then for some reason the rpm value drops significantly. So for example, the spinning object is doing 100 rpm and i get the 100 rpm correctly, but every like 2 seconds, the rpm reading drops down to like 30 rpm, and then back up again, while the real rpm is still 100. One thing to note is that, this only seem to happen with rpm´s over like 50 ish.
I have attached a pictue of the rpm plot graph.

I have tried doing the rpm calculation with attatchInterrupts also, but with no succsess. I think this is partally becuase the ESP82699 can’t prioritise interrupts.

The attached code is doing rpm calculation without interrupts.


void loop()
{  
  Blynk.virtualWrite(V2, rpmCalc());  
  Serial.println(rpmCalc());
  Blynk.run(); 
  
}

 int rpmCalc(){
  
  currentstate = digitalRead(rpmInputPin); // Read IR sensor state

  if( prevstate != currentstate) // If there is change in input
   {
     if( currentstate == LOW ) // If input only changes from LOW to HIGH
       {
        long tid = micros();
         duration = ( tid - prevmillis ); // Time difference between revolution in microsecond
         rpm = (60000000/duration); // rpm = (1/ time millis)*1000*1000*60;
         prevmillis = tid; // store time for next revolution calculation
       }
   }
  prevstate = currentstate; // store this scan (prev scan) data for next scan
  if(micros() - prevmillis > timeOut){
      rpm = 0;
    }
    
  return rpm;

    
    }

I really would help if you added the information that was requested when you created this topic:

  1. Add details :
    • Hardware model + communication type. For example: Arduino UNO with Ethernet Shield
    • Smartphone OS (iOS or Android) + version
    • Blynk server or local server
    • Blynk Library version

especially the bit about whether you are using a local or Blynk cloud server.

Your issue seems to be that you are breaking the golden rule of Blynk, which is never put Blynk.virtualWrite() in your void loop

http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/keep-your-void-loop-clean

The behaviour you are seeing is most likely because you are flooding the server.

Pete.

Pete!

Thank you very much for your reply, this was my issue. It’s now woking fine! :slight_smile:
Embarrassing that i have missed that rule, since i have read that article many times.