Update value only if new value is bigger or smaller than 2 points

void updateSensor(void)
{

  SensorRaw = analogRead(A0);
  Sensor = map(SensorRaw, 0, 1023, 0, 500);

  Serial1.print("n0.val=");
  Serial1.print(Sensor);
  Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
  Serial1.write(0xff);
  Serial1.write(0xff);
  }

Dear all, looking for the way to only send the "Sensor" value to the Serial1 only if it gets higher or lower than the current value by set amount.

Thank you for any advice.

If (abs(currentValue - previousValue) > someValue)
{
  //do something
}

Dear all, looking for the way to only send the "Sensor" value to the Serial1 only if it gets higher or lower than the current value by set amount.

The current amount will always be exactly equal to the current amount, won't it?

If you want to send data only when the current amount differs from the PREVIOUS amount by more than some amount, you need to keep track of the previous amount, and then:

 if(abs(currentValue - previousValue) > requiredDifference)
  {
     previousValue = currentValue;
     Send(currentValue);
  }

if(abs(currentValue - previousValue) > requiredDifference)
{
previousValue = currentValue;
Send(currentValue);
}

Keeps sending all the time, not sure wheres the bug, also slowed the transfer drastically.

void updateSensor(void)
{
  int previousValue;

  Sensor = analogRead(A0);
  Sensor = map(Sensor, 0, 1023, 0, 500);

  if (abs(Sensor - previousValue) > 5) {
    previousValue = Sensor;
    Serial1.print("n0.val=");
    Serial1.print(Sensor);
    Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
    Serial1.write(0xff);
    Serial1.write(0xff);
    Serial.println(Sensor);
  }
}
  int previousValue;  //create a new variable named previousValue and give it a random value

Note my comment on your code

It would help a lot if you posted your whole program or a smaller but complete program that shows the problem

Note my comment on your code

It would help a lot if you posted your whole program or a smaller but complete program that shows the problem

Sure sorry

#include "Nextion.h"
NexButton b0 = NexButton(0, 1, "button0");
NexButton b1 = NexButton(0, 2, "button1");
NexDSButton bt0 = NexDSButton(0, 3, "bt0");

NexPage page0 = NexPage(0, 0, "page0");  // Page added as a touch event
NexPage page1 = NexPage(1, 0, "page1");  // Page added as a touch event
NexPage page2 = NexPage(2, 0, "page2");  // Page added as a touch event

int DS1 = 0;// dual state starts deselected
int CurrentPage = 0;
int SensorRaw = 0;
int Sensor = 0;



NexTouch *nex_listen_list[] =
{
  &b0,
  &b1,
  &bt0,
  &page0,  // Page added as a touch event
  &page1,  // Page added as a touch event
  &page2,  // Page added as a touch event
  NULL
};


void bt0PopCallback(void *ptr)
{
  uint32_t dual_state;
  NexDSButton *btn = (NexDSButton *)ptr;
  bt0.getValue(&dual_state);
  if (dual_state)
  {
    DS1 = 1;
  }
  else
  {
    DS1 = 0;
  }
  Serial.print("DualState1 = ");
  Serial.println(DS1);
}

// Page change event:
void page0PushCallback(void *ptr)  // If page 0 is loaded on the display, the following is going to execute:
{
  CurrentPage = 0;  // Set variable as 0 so from now on arduino knows page 0 is loaded on the display
}  // End of press event


// Page change event:
void page1PushCallback(void *ptr)  // If page 1 is loaded on the display, the following is going to execute:
{
  CurrentPage = 1;  // Set variable as 1 so from now on arduino knows page 1 is loaded on the display
}  // End of press event


// Page change event:
void page2PushCallback(void *ptr)  // If page 2 is loaded on the display, the following is going to execute:
{
  CurrentPage = 2;  // Set variable as 2 so from now on arduino knows page 2 is loaded on the display
}  // End of press event




////////////////////////// End of touch events


void setup()
{
  Serial.begin(9600);
  pinMode(A0, INPUT);

  nexInit();
  b0.attachPush(b0PushCallback); //button press
  b0.attachPop(b0PopCallback);  // button release
  b1.attachPush(b1PushCallback); //button press
  b1.attachPop(b1PopCallback);  // button release
  bt0.attachPop(bt0PopCallback, &bt0); // dual state
  page0.attachPush(page0PushCallback);  // Page press event
  page1.attachPush(page1PushCallback);  // Page press event
  page2.attachPush(page2PushCallback);  // Page press event

}

void loop() {
  {
    nexLoop(nex_listen_list);
    //Serial.print("Page# = ");
    //Serial.println(CurrentPage);
  }
  if (CurrentPage == 1) {
    updateSensor();
  }
  // Serial.println(SensorVal);
}






void b0PushCallback(void *ptr)
{
  Serial.println("B0 is Pressed");
}

void b0PopCallback(void *ptr)
{
  Serial.println("B0 is Released");
}
void b1PushCallback(void *ptr)
{
  Serial.println("B1 is Pressed");
}

void b1PopCallback(void *ptr)
{
  Serial.println("B1 id Released");
}

void updateSensor(void)
{
  int previousValue;

  Sensor = analogRead(A0);
  Sensor = map(Sensor, 0, 1023, 0, 500);

  if (abs(Sensor - previousValue) > 5) {
    previousValue = Sensor;
    Serial1.print("n0.val=");
    Serial1.print(Sensor);
    Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
    Serial1.write(0xff);
    Serial1.write(0xff);
    //Serial.println(Sensor);
  }
}
int Sensor = 0;

If this is the current value, shouldn't the previous value variable be declared somewhere real close to here?

Should the current value variable and the previous value variable have similar names, so one can see that they ARE related?

Code: [Select]
int Sensor = 0;

If this is the current value, shouldn't the previous value variable be declared somewhere real close to here?

Should the current value variable and the previous value variable have similar names, so one can see that they ARE related?

Yes you are correct.

code corrected

#include "Nextion.h"
NexButton b0 = NexButton(0, 1, "button0");
NexButton b1 = NexButton(0, 2, "button1");
NexDSButton bt0 = NexDSButton(0, 3, "bt0");

NexPage page0 = NexPage(0, 0, "page0");  // Page added as a touch event
NexPage page1 = NexPage(1, 0, "page1");  // Page added as a touch event
NexPage page2 = NexPage(2, 0, "page2");  // Page added as a touch event

int DS1 = 0;// dual state starts deselected
int CurrentPage = 0;
int SensorRaw = 0;
int SensorCurrentValue = 0;  
int SensorPreviousValue;



NexTouch *nex_listen_list[] =
{
  &b0,
  &b1,
  &bt0,
  &page0,  // Page added as a touch event
  &page1,  // Page added as a touch event
  &page2,  // Page added as a touch event
  NULL
};


void bt0PopCallback(void *ptr)
{
  uint32_t dual_state;
  NexDSButton *btn = (NexDSButton *)ptr;
  bt0.getValue(&dual_state);
  if (dual_state)
  {
    DS1 = 1;
  }
  else
  {
    DS1 = 0;
  }
  Serial.print("DualState1 = ");
  Serial.println(DS1);
}

// Page change event:
void page0PushCallback(void *ptr)  // If page 0 is loaded on the display, the following is going to execute:
{
  CurrentPage = 0;  // Set variable as 0 so from now on arduino knows page 0 is loaded on the display
}  // End of press event


// Page change event:
void page1PushCallback(void *ptr)  // If page 1 is loaded on the display, the following is going to execute:
{
  CurrentPage = 1;  // Set variable as 1 so from now on arduino knows page 1 is loaded on the display
}  // End of press event


// Page change event:
void page2PushCallback(void *ptr)  // If page 2 is loaded on the display, the following is going to execute:
{
  CurrentPage = 2;  // Set variable as 2 so from now on arduino knows page 2 is loaded on the display
}  // End of press event




////////////////////////// End of touch events


void setup()
{
  Serial.begin(9600);
  pinMode(A0, INPUT);

  nexInit();
  b0.attachPush(b0PushCallback); //button press
  b0.attachPop(b0PopCallback);  // button release
  b1.attachPush(b1PushCallback); //button press
  b1.attachPop(b1PopCallback);  // button release
  bt0.attachPop(bt0PopCallback, &bt0); // dual state
  page0.attachPush(page0PushCallback);  // Page press event
  page1.attachPush(page1PushCallback);  // Page press event
  page2.attachPush(page2PushCallback);  // Page press event

}

void loop() {
  {
    nexLoop(nex_listen_list);
    //Serial.print("Page# = ");
    //Serial.println(CurrentPage);
  }
  if (CurrentPage == 1) {
    updateSensor();
  }
  // Serial.println(SensorVal);
}






void b0PushCallback(void *ptr)
{
  Serial.println("B0 is Pressed");
}

void b0PopCallback(void *ptr)
{
  Serial.println("B0 is Released");
}
void b1PushCallback(void *ptr)
{
  Serial.println("B1 is Pressed");
}

void b1PopCallback(void *ptr)
{
  Serial.println("B1 id Released");
}

void updateSensor(void)
{


  SensorCurrentValue = analogRead(A0);
  SensorCurrentValue = map(SensorCurrentValue, 0, 1023, 0, 500);

  if (abs(SensorCurrentValue - SensorPreviousValue) > 5) {
    SensorPreviousValue = SensorCurrentValue;
    Serial1.print("n1.val=");
    Serial1.print(SensorCurrentValue);
    Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
    Serial1.write(0xff);
    Serial1.write(0xff);
    //Serial.println(Sensor);
  }
}

So i`m trying to work this out.

This part of the code:

void updateSensor(void)
{


  SensorCurrentValue = analogRead(A0);
  SensorCurrentValue = map(SensorCurrentValue, 0, 1023, 0, 500);

  if (abs(SensorCurrentValue - SensorPreviousValue) > 2) {
    SensorPreviousValue = SensorCurrentValue;
    Serial1.print("n1.val=");
    Serial1.print(SensorPreviousValue);
    Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
    Serial1.write(0xff);
    Serial1.write(0xff);
    Serial.println(SensorPreviousValue);
  }
}

Works as desired, but as you may notice, i`m sending the SensorPreviusValue

Works as desired, but as you may notice, i`m sending the SensorPreviusValue

At the time you send the value, SensorCurrentValue and SensorPreviousValue contain the same value, so it doesn't matter which you send. I'd think that sending the current value would be the obvious thing to do, though.