Solution for the nextion Gauge

Hello
I'm trying to create a speedometer using nextion recently. However, nextion's gauge comes out weird. The previous gauges don't disappear and the problem continues to appear on the screen.
Put the counting value in the val of zo(gauge).
Isn't changing the value of zo only changing the angle of the gauge?
I don't know how to solve it

I'd like to thank those who answered

1 Like

Are you telling the gauge to disappear BEFORE giving the new value? I am not familiar with what you are using, so please give a link to the documentation for it.

1 Like

It's a problem that one line has to go up and down like an instrument panel, but it's a problem that there are multiple lines.

#include <Arduino.h>

#include <TimerOne.h>

#include <math.h>

#include <SoftwareSerial.h>

​

SoftwareSerial hmi(2,3);

​

const int IRSensorPin = 7;

const float Diameter = 0.25;

 

int inputState;                          // the current state from the input pin

int lastInputState = LOW;        // the previous InputState from the input pin

long lastDebounceTime = 0;   // the last time the output pin was toggled

long debounceDelay = 5;        // the debounce time; increase if the output flickers

long time;

long endTime;

long startTime;

int lin_vel;

int RPM = 0;

int counting;

float temp;

​

float lnTime = 0;

int lin_vel_16;

byte tail[]= {0xFF, 0xFF, 0xFF};

​

void setup() {

  pinMode(IRSensorPin, INPUT);

 

  hmi.begin(9600);

 

  endTime = 0;

  Timer1.initialize(1000000);  // Set the timer to 60 rpm, 1,000,000 microseconds (1 second)

  Timer1.attachInterrupt(timerIsr);  // Attach the service routine here

 

 

}

 

void loop() {

 

 

  time = millis();

  int currentSwitchState = digitalRead(IRSensorPin);

 

  if (currentSwitchState != lastInputState) {

    lastDebounceTime = millis();

  }

 

  if ((millis() - lastDebounceTime) > debounceDelay) {

    if (currentSwitchState != inputState) {

      inputState = currentSwitchState;

      if (inputState == LOW) {

 

        calculateRPM();

 

      }

      else {

 

      }

    }

  }

  lastInputState = currentSwitchState;

 

 }

 

// ---------------------------------------------------------------

void calculateRPM() {

  startTime = lastDebounceTime;

  lnTime = startTime - endTime;

  RPM = 60000 / (startTime - endTime);

  endTime = startTime;

}

 

 

void timerIsr()

{

  // Print RPM every second

  // RPM based on timer

  Serial.println("---------------");

  time = millis() / 1000;

  lin_vel = (Diameter) * RPM * ((2 * PI) / 60) * 3.6;

  lin_vel_16 = lin_vel;

  Serial.print(lin_vel_16);

  Serial.println(" km");

  if(lin_vel_16>15 && lin_vel_16<120)

  {

    counting = map(lin_vel_16, 15, 120, 0, 210);

  } else if (lin_vel_16<=15 && lin_vel_16>=0)

  {

    counting = map(lin_vel_16, 0, 15, 330, 360);

  }

  else

  {

    counting = 0;

  }

  int temperature = temp;

  String data1 = "n0.val=" + String(lin_vel_16);

  String data2 = "z0.val=" + String(counting);

  String data3 = "n1.val=" + String(temperature);

  hmi.write(data1.c_str());

  hmi.write(tail,3);

  hmi.write(data2.c_str());

  hmi.write(tail,3);  

  delay(10);

  RPM = 0;

}

1 Like

You need to erase the last line before you draw the new one. So remember the coordinates of the previous line then draw the new one and update the coordinates

1 Like

Are you telling me to paint the old value in the background color and update the new value?

2 Likes

you need your screen to refresh, you should clear the screen before you print on it again,
put some code that clears the display in the upper part of your timerIsr(), that way it clears the display before it prints again.

Correct

How can I clear the screen? Is there such a command?

just like he said, you can color the old "images" or frames black, AKA erase them.

Thank you, and I'm glad I was able to resolve the issue.
Thank you so much for your advice.

2 Likes

Glad you got it solved. You will use that very same procedure even when using the small LCD, 2 line display. Even your digital TV uses that same procedure on your TV screen.