0
votes

I have an Arduino MEGA with one of these Nextion displays. I created a nice display and my aim is to show analog inputs. However I have noticed that this display cannot show decimals (using the number "widget"). So I am using "text" widget to do that, unfortunately it doesn't work, anybody knows why?

#include "Nextion.h"
#define RES 1023
#define AREF 5
int voltage_pin=A0;
int raw_voltage;
double voltage;
String voltage2;

void setup() {
  Serial.begin(9600);
}

void loop() {
  raw_voltage=analogRead(voltage_pin);
  voltage=raw_voltage*AREF/RES;
  Serial.print("n3.val=");
  Serial.print(raw_voltage);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  String voltage2=String(voltage);
  Serial.print("t10.txt=");
  Serial.print(voltage2);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  delay(100);
}

Thanks in advance! :)

3
You probably should explain what you mean by "it doesn't work."intrepidhero

3 Answers

0
votes

this could help:

value1 = analogRead(A1);

  vout1 = (value1 * AREF) / 1023.0; 

  vin1 = vout1 / (R2 / (R1 + R2));

  memset(buffer, 0, sizeof(buffer));

  dtostrf(vin1,5, 2, buffer);

  t4.setText(buffer);

  vin1 = map(vin1, 8, 15, 0, 100);

  send_ProgressBar("j2", vin1);
0
votes

If you are using a text object the command should be Serial.print("n3.txt=\"yourtext\"");

0
votes

You can use the Xfloat widget and send the value in the same way as number, but you need to change the parameters "vvs0" and "vvs1" on nextion editor, so the widget adjust the value. For example if you wanna show the value 2.53, multiply it by 100 and then set the parameter "vvs0" to 1 and "vvs1" to 2.