Building for speed

Hi all

Just wondering if there are any specific ideas or requirements when it comes to having things run as fast as possible? I’m new to this coding business so am unsure if there is a better way to do what I have done. My first project was to create a simple counter eg. a sensor triggers a relay which in turn adds one to a count on the ESP8266. This count variable is displayed in Blynk along with a count reset button.

If this was a relatively high speed scenario, would it be beneficial to only update the value on the Blynk app at a certain interval as opposed to continuously? Initially I had a problem with too much delay due to the way I debounced my input. I’ve since used a library to solve that I think.

Can someone take a quick look at this random code and tell me if it can be optimised? Ignore the timeOverride, countON, timerON code as that’s removed for now

Cheers!

[EDIT] I asked too soon. Off to look up pin interrupts so I do not need to poll the input pin constantly.

#include <ButtonDebounce.h>

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG

#define USE_WEMOS_D1_MINI

#include "BlynkEdgent.h"

BlynkTimer timer; 

//____________________________________
//----------VARIABLES ETC-------------
//====================================

ButtonDebounce button(14, 10);  // Button pin @ D1 on ESP8266 Dev board
              
bool countReset = 0;                    // Used from Blynk to reset the count
bool countON = 0;                       // Used to enable counting - see V3 (START COUNTING)
bool timerON = 0;                       // Used to override the count within specified time function - see RTC
bool led = 0;                           // LED for Blynk dashboard to show when a can passes - see V4
bool timeOverride = 0;                  // Used to enable counting no matter what time it is - see V2

int count = 0;                          // Times button has been pressed

//====================================

// This function will run every time Blynk connection is established
BLYNK_CONNECTED()
{
  // Add virtual pins to sync here. A corresponding 'BLYNK_WRITE' handler is called (see below)
  // Can update multiple by separating with commas
  Blynk.syncVirtual(V0,V2,V3);
  Blynk.sendInternal("rtc", "sync");    //request current local time for device
  //update_terminal();
}

// Write value of virtual pin V0 to variable 'count.'
BLYNK_WRITE(V0)
{
  count = param.asInt();                // Count = parameter V0 from server set as int
}

// Write value of virtual pin V1 'COUNTRESET' from server to variable 'countReset.' This is a button from app
BLYNK_WRITE(V1)
{
  countReset = param.asInt();           // Resets count value
  Blynk.virtualWrite(V0, count);        // Writes 0 value after reset so app updates
}

// Write value of virtual pin V2 'TIME OVERRIDE' from server to variable 'timeOverride'
BLYNK_WRITE(V2)
{
  timeOverride = param.asInt();         // Used to enable counting no matter what time it is, regardless of set Automation in app
}

// Write value of virtual pin V3 'START COUNTING' from server to variable 'countON.'
BLYNK_WRITE(V3)
{
  countON = param.asInt();              // Enables counting within time period set by Automations
}

// Write value of 'led' to virtual pin V4
BLYNK_WRITE(V4)
{
  led = param.asInt();                  // LED for Blynk dashboard to show when a can passes. Just testing
}

//____________________________________
//--------------SETUP-----------------
//====================================

void setup()
{
  Serial.begin(115200);
  delay(100);

//  pinMode(button, INPUT_PULLUP); // Could be anythig triggering the button pin

  BlynkEdgent.begin();

}

//____________________________________
//--------------COUNTER---------------
//====================================

// Counts activations of virtual pin 0
// Will count within a time period set by user using Automation within app
// Or will always count if the override is active 

void counter()
{
  button.update();

    if(button.state() == LOW)
    {
      count = count + 1;                // Add 1 to count
      Blynk.virtualWrite(V0, count);
      Blynk.virtualWrite(V4, 1);        // Turn on LED within blynk app
    }

  if (countReset == 1)                  // If count reset is pressed in the app, reset count
  {
    count = 0;
  }
}

//____________________________________
//---------------LOOP-----------------
//====================================

void loop()
{
  timer.run(); // call the BlynkTimer object
  BlynkEdgent.run();
  counter();
}

OK so I’ve now got something that can count fast and reliably I believe. For reference here is what I ended up with. Couldn’t work out how to use BlynkTimer to debounce my interrupt tho… Will have to learn a bit more about that


// Fill-in information from your Blynk Template here
#define BLYNK_TEMPLATE_ID "xx"
#define BLYNK_DEVICE_NAME "D1"
#define BLYNK_AUTH_TOKEN "xx";
#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG

#define USE_WEMOS_D1_MINI

#include "BlynkEdgent.h"

BlynkTimer timer; 

WidgetTerminal terminal(V5); // Attach widget terminal in app to virtual pin V5

//____________________________________
//----------VARIABLES ETC-------------
//====================================

const int button = 14;                  // Button pin @ D1 on ESP8266 Dev board

bool countReset = 0;                    // Used from Blynk to reset the count
bool countON = 0;                       // Used to enable counting - see V3 (START COUNTING)
bool timerON = 0;                       // Used to override the count within specified time function - see RTC
bool led = 0;                           // LED for Blynk dashboard to show when a can passes - see V4
bool timeOverride = 0;                  // Used to enable counting no matter what time it is - see V2

int count = 0;                          // Times button has been pressed

//____________________________________
//----------COUNT INTERRUPT-----------
//====================================

// Uses interrupt on pin 14 (D5) to increment count

ICACHE_RAM_ATTR void countUP()
{
  static unsigned long last_interrupt_time = 0;
  unsigned long interrupt_time = millis();

  // If interrupts come faster than 100ms, assume it's a bounce and ignore
  if ((interrupt_time - last_interrupt_time > 100) && ((countON == 1) || (timeOverride == 1)))
  {
    last_interrupt_time = interrupt_time;
    count = count + 1;                         
  }
}

void update_count()
{
  Blynk.virtualWrite(V0, count);
}

//=====================================

// This function will run every time Blynk connection is established
BLYNK_CONNECTED()
{
  // Add virtual pins to sync here. A corresponding 'BLYNK_WRITE' handler is called (see below)
  // Can update multiple by separating with commas
  Blynk.syncVirtual(V0, V2, V3);
  Blynk.sendInternal("rtc", "sync");    //request current local time for device
}

// Write value of virtual pin V0 to variable 'count.'
BLYNK_WRITE(V0)
{
  count = param.asInt();                // Count = parameter V0 from server set as int
}

// Write value of virtual pin V1 'COUNTRESET' from server to variable 'countReset.' This is a button from app
BLYNK_WRITE(V1)
{
  countReset = param.asInt();           // Resets count value
  Blynk.virtualWrite(V0, count);        // Writes 0 value after reset so app updates
}

// Write value of virtual pin V2 'TIME OVERRIDE' from server to variable 'timeOverride'
BLYNK_WRITE(V2)
{
  timeOverride = param.asInt();         // Used to enable counting no matter what time it is, regardless of set Automation in app
}

// Write value of virtual pin V3 'START COUNTING' from server to variable 'countON.'
BLYNK_WRITE(V3)
{
  countON = param.asInt();              // Enables counting within time period set by Automations
}

// Write value of 'led' to virtual pin V4
BLYNK_WRITE(V4)
{
  led = param.asInt();                  // LED for Blynk dashboard to show when a can passes. Don't really need this shit as you can see the numbers change...
}

//____________________________________
//--------------SETUP-----------------
//====================================

void setup()
{
  Serial.begin(115200);
  delay(100);

  pinMode(button, INPUT_PULLUP); // Could be anythig triggering the button pin
  attachInterrupt(digitalPinToInterrupt(button), countUP, FALLING); // Interrupt to count

  BlynkEdgent.begin();

  timer.setInterval(5000L, update_count);  // Updates count to Blynk app every 5 sec. No need for it to be faster.
}


//____________________________________
//---------------LOOP-----------------
//====================================

void loop()
{
  timer.run(); // call the BlynkTimer object
  BlynkEdgent.run();

  if (countReset == 1)                  // If count reset is pressed in the app, reset count
  {
    count = 0;
  }
}