Blynk.syncVirtual adding value on reboot

Hello,

i have a rotary encoder to set a value for a setpoint(calibration).
Everything works fine as it should ! But when the device reboots it adds one increment to the stored value Ex: if the value was 7.0 before reboot, after reboot the value will be 7.1 and subsequent reboots will keep on adding 0.1 to the previous value !!

this is the code

n = digitalRead(clkPinA);
  if ((clkPinALast == LOW) && (n == HIGH)) {
    if (digitalRead(sckPinB) == LOW) {
      encoder0Pos--;
      if(setpoint - 5.5 >= 0)setpoint -= 0.1;
      Blynk.virtualWrite(V6, setpoint);
   } else {
      encoder0Pos++;
      if(setpoint + 0.1 <= 15) setpoint += 0.1;
      Blynk.virtualWrite(V6, setpoint);
     }
  }
  clkPinALast = n;

is this a problem of the code ?? Or do i need to update the esp core(2.4.1) ??

Please post your full code, plus all the other details that are requested when you create a new topic.

Pete.

@PeteKnight

Here is the code

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <TimeLib.h>


SimpleTimer timer;

float setpoint = 0;

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";
char ssid[] = "xxxxxxxxxxx";
char pass[] = "xxxxxxxxxx";
char server[] = "192.168.x.xxx";

int encoder0PinA = D5;
int encoder0PinB = D6;
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;

BLYNK_WRITE(V6) {

  setpoint = param.asFloat();
  Blynk.virtualWrite(V6, setpoint);
}

void setup()
{
  Serial.begin(115200);
  Serial.println();
  Blynk.begin(auth, ssid, pass, server, 8080);
pinMode (encoder0PinA, INPUT);
pinMode (encoder0PinB, INPUT);

}

void loop()
{
  Blynk.run();
  timer.run();
  n = digitalRead(encoder0PinA);
  if ((encoder0PinALast == LOW) && (n == HIGH)) {
    if (digitalRead(encoder0PinB) == LOW) {
      encoder0Pos--;
      if(setpoint - 5.5 >= 0)setpoint -= 0.1;
      Blynk.virtualWrite(V6, setpoint);
   } else {
      encoder0Pos++;
      if(setpoint + 0.1 <= 15) setpoint += 0.1;
      Blynk.virtualWrite(V6, setpoint);
     }
  }
  encoder0PinALast = n;
}

In the labeled value display widget (V6) the setpoint increments by .1 on reboots.

Here are my observations (in no particular order)…

  1. The title of the topic is “Blynk.syncVirtual adding value on reboot”, but your code does not contain any BLYNK_CONNECTED() callback or and Blynk.syncVirtual command.

  2. Your void loop is a mess!

  3. What is the purpose of this code:

BLYNK_WRITE(V6) {

  setpoint = param.asFloat();
  Blynk.virtualWrite(V6, setpoint);
}

why are you reading the value from the V6 widget then writing it back to the widget?

Pete.

i have missed this while rewriting the code here !!

BLYNK_CONNECTED() {
  if (isFirstConnect) {
    Blynk.syncVirtual(V6);
    isFirstConnect = false;
  }
  Serial.println("Connected");
  ReCnctCount = 0;
}

Have this running fine

Yes ! Working on it to keep it empty !

:roll_eyes: O yes !! I am a bit confused now ! Can you please me guide me through ?

So what does your fill working code look like, because simply adding this additional function in to your original code won’t work, as there are missing variable declarations.

No kidding!
Comment-out this line:
Blynk.virtualWrite(V6, setpoint);
from your BLYNK_WRITE(V6) callback.

Pete.

Yes Tried that earlier !! But still increment on reboot !!

int ReCnctFlag;  
int ReCnctCount = 0; 

are you talking about this ?

Yes. Please post the full code that you are currently using, and forum members will take a look at it. Posting irrelevant snippets and incomplete code simply wastes our time.

Pete.

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <TimeLib.h>


SimpleTimer timer;

float setpoint = 0;

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";
char ssid[] = "xxxxxxxxxxx";
char pass[] = "xxxxxxxxxx";
char server[] = "192.168.x.xxx";

int encoder0PinA = D5;
int encoder0PinB = D6;
int encoder0Pos = 0;
int encoder0PinALast = LOW;
int n = LOW;

int ReCnctFlag;  
int ReCnctCount = 0; 
bool isFirstConnect = true;

BLYNK_WRITE(V6) {

  setpoint = param.asFloat();
 // Blynk.virtualWrite(V6, setpoint); Commented out, but still increments 
}

void setup()
{
  Serial.begin(115200);
  Serial.println();
  Blynk.begin(auth, ssid, pass, server, 8080);
pinMode (encoder0PinA, INPUT);
pinMode (encoder0PinB, INPUT);

}

BLYNK_CONNECTED() {
  if (isFirstConnect) {
    Blynk.syncVirtual(V6);
    isFirstConnect = false;
  }
  Serial.println("Connected");
  ReCnctCount = 0;
}

void loop()
{
  Blynk.run();
  timer.run();
  n = digitalRead(encoder0PinA);
  if ((encoder0PinALast == LOW) && (n == HIGH)) {
    if (digitalRead(encoder0PinB) == LOW) {
      encoder0Pos--;
      if(setpoint - 5.5 >= 0)setpoint -= 0.1;
      Blynk.virtualWrite(V6, setpoint);
   } else {
      encoder0Pos++;
      if(setpoint + 0.1 <= 15) setpoint += 0.1;
      Blynk.virtualWrite(V6, setpoint);
     }
  }
  encoder0PinALast = n;
}

This is the complete code !! Compiles and works here !! I have configured a LABELED VALUE widget and as i turn the rotary encoder the values changes !!

But once the device reboots the value increments by 0.1

Without having some details of your rotary encoder it’s difficult to know for sure, but I think the code in your void loop may be incrementing the value of setpoint the first time it runs.
You’ll be able to check this by commenting-out everything in your void loop after timer.run(); and rebooting your device.

Some well placed and informative Serial.print statements would also allow you to track the value of the various variables involved in the nested if statements to work-out exactly what’s happening.

Pete.

1 Like

Well i took it in a different way !
The rotary encoder has a inbuilt push button.
I am reading the push button, and when its high i will set the flag == 0 and call the encoder function encoder() under if condition
at the same time rotate the encoder to change the the value. Once the button is released the flag is set to 1 under else condiion and nothing can happen further !!

Now reboot does not effect the set value !!

This is crude\bad method, but works well !! (Really bad at coding) :sob:

If anyone can guide me with a right way that will be really appreciated…

Does it increment only once at boot or continually?

This has got to be what’s doing it but like @PeteKnight says add a serial print to test Serial.println ("Adding 0.1"); it then figure out why it is running…

What does the app look like? Especially settings on V6

Hi,

There is some flaw in your code, but that depends on the type of rotary encoder you use. Not all rotary encoders have a stop where both inputs are not connected (open).

I suggest you read my blog [https://phpjj.wordpress.com/about-rotary-encodera/](About Rotary Encoders)

Now when you understand rotary encoder better, there is some code with sufficient comment for you to adapt it for your needs. The code is written to allow Rotary Encoder Type to be defined later as a configuration option. Original code workts on MicroChip PIC processor but had proven to work for arduino as well.
If you know the type you can just pick the code for your type. Now put both inputs in a "byte now.

So you need to assign:

byte now=0;
bute prev=0;

In your code

now = digitalRead(encoder0PinA) * 2 ; // Read pin A and put it in bit 1
now += digitalRead(encoder0PinB); // Add bit B to now bit 0
…

Hope this helps.
Peter

3 Likes