Pin interrupt and blynk

Hi. No I’m stuck. Trying to control a motor position with an encoder. It is a quadrature encoder, but I only use one wire to detect any interrupts from the encoder. This is because the ESP had a hard time trying to get the states of the 2 wire encoder, it was bouncing and I figured it was just to fast for it to handle.

But now I’m trying to use the number of interrupts as a “counter” to calculate the position/rotations of the motor, but the Blynk.write function cant get the updated values from the counter ("interruptCounter ") in my code.

So how can I pass the fast updated number of interrupts to my blynk.write function so it can turn of the motor when the desired set_point is reached?


#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
SimpleTimer timer;

const int interruptPin_A = 14;
volatile int interruptCounter = 100;  //volatile
int numberOfInterrupts = 0;
int n_first = 0;
int n_last = 0;

const int power_A = 5; //gpio 5 is D1
const int Dir_A = 0;   // direction of motor GPIO0 is D3
int sp_A = 10;         //motor A position
int sp_B = 0;          //motor B position
int conv_A = 10;       //connversion from C/rev to rev
int pwm_A;
int rot_A;
int count_A = 100;

// WIFI and blynk details
char auth[] = "xxx";
char ssid[] = "xxx";
char pass[] = "xxx";
 
void setup() 
{ 
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  Blynk.syncAll();
 
  pinMode(power_A,OUTPUT); 
  pinMode(Dir_A,OUTPUT);
  pinMode(interruptPin_A, INPUT_PULLUP);
  attachInterrupt(interruptPin_A, handleInterrupt_A, RISING);
  Blynk.virtualWrite(V6,sp_A);
  delay(1000);
}

BLYNK_WRITE(V6) //slider for position of motor A
{
  sp_A = param.asInt(); // THIS IS MY DESIRED SET_POINT FOR MY MOTOR

   Serial.print("setpoint = ");
   Serial.println(sp_A*conv_A);
   
  if (sp_A*conv_A < count_A) //check direction and start rotate to sp (set_point)
  {
   rot_A = 0; 
   digitalWrite(power_A,HIGH);
   digitalWrite(Dir_A,LOW); 
   Serial.print("counter = ");
   Serial.println(count_A);
  }
  else if (sp_A*conv_A > count_A) //check direction and start rotate to sp (set_point)
  {
   rot_A = 1; 
   digitalWrite(power_A,HIGH);
   digitalWrite(Dir_A,HIGH);
   Serial.print("counter = ");
   Serial.println(count_A);
  }
  else // do nothing
  {
   digitalWrite(power_A,LOW);
   digitalWrite(Dir_A,LOW); 
  }
}
 
void handleInterrupt_A() 
{
  if (rot_A < 1)
  {
   interruptCounter--;
   Serial.print("UP n_tic = ");
   Serial.println(interruptCounter);
   count_A = interruptCounter;   THIS VALUE IS NOT SEEN BY THE BLYNK.WRITE(V6) FUNCTION
  }
  
  else
  {
   interruptCounter++;
   Serial.print("DOWN n_tic = ");
   Serial.println(interruptCounter);
   count_A = interruptCounter;   **THIS VALUE IS NOT SEEN BY THE BLYNK.WRITE(V6) FUNCTION**
  }
}
 
void loop() 
{
  Blynk.run();
  timer.run();
}

you create your own function that sets the motor when the counter get to your threshold.
Then call that function from the BLYNK_WRITE and from your increment counting code.

BLYNK_WRITE() functions are called from the App upon widget state changes. To send data the other way (to display widgets, feedback to control widgets, etc.) you use Blynk.virtualWrite(vPin).

And to “call” those functions from code you use Blynk.syncVirtual(vPin) commands.

For example you could send a value to a slider…

Blynk.virtualWrite(V6, interruptCounter);

Then “trigger” the slider as if you had moved it to that setting…

Blynk.syncVirtual(V6);

Which then calls the associated function and does the thing.

But depending on how frequently you need to do this, as this can happen rather rapidly and possibly cause some bottleneck issues, as suggested you should do all your threshold logic in the code and only sending the write and sync command to the App when needed.

But the “counter” is not visible outside the "void handleInterrupt_A() ", so I can’t check the counter’s value against my set_point. My problem is that the counter is not seen by the rest of the code as it is changing every 5ms when the motor is running.

declare the counter in the main sketch, not inside a function, and it will be globally accessible.

The counter is declared as a global variable. But for some reason my Blynk.write function can not se the “live” updated counter.