Virtual pins need a timer for Particle

Blynk works great to control pins and read pins using Particle Photon, but whenever I add a virtual pin it shows down to a crawl and goes on and off line a lot.

Reading the Blynk documents it says to add a timer like “SimpleTimer.h”, but this is an Arduino timer and I cannot load it into the Particle IDE. There is a timer from Spark called SparkIntervalTimer.h available, but I have been unable to implement it. (I don’t understand the example they give)

Does anyone have a easy way to add Virtual pins in Particle ? Or a simple example of a timer that works with Particle…
The code below reads the mills (uptime from Particle) and it works as a virtual pin, but it’s super slow and resets a lot.

thanks
Randy

```#include <blynk/blynk.h>

char auth[] = "771825b75e0c41778745953a*******";
#define PIN_UPTIME V5
void setup()
{
  
  Serial.begin(9600);
  delay(5000);
  Blynk.begin(auth);
 
}

BLYNK_READ(PIN_UPTIME)
{
  // This command writes Arduino's uptime in seconds to Virtual Pin (5)
  Blynk.virtualWrite(PIN_UPTIME, millis() / 1000);
  }

void loop()
{
  Blynk.run();
    }```

Try the threads that come up if you put SparkIntervalTimer in the search tool.

Thank you Costas that was just the extra push I needed. The trick is to not use the SparkIntervalTimer, but rather the SparkCorePolledTimer.h. which includes a simple example. I revised my code below and it works great.

#include "SparkCorePolledTimer/SparkCorePolledTimer.h"

#include <blynk/blynk.h>
SparkCorePolledTimer updateTimer(1000);
void OnTimer(void);   

char auth[] = "771825b75e0c41778745953a581*****";
#define PIN_UPTIME V5
void setup()
{
  
  Serial.begin(9600);
  updateTimer.SetCallback(OnTimer);
  delay(5000);
  Blynk.begin(auth);
 
}

BLYNK_READ(PIN_UPTIME)
{
  // This command writes Arduino's uptime in seconds to Virtual Pin (5)
  Blynk.virtualWrite(PIN_UPTIME, millis() / 1000);
  }

void loop()
{
  Blynk.run();
  updateTimer.Update();
    }
    
void OnTimer(void) {  //Handler for the timer, will be called automatically
  
}```
2 Likes