Go Down

Topic: Arduino serial to Nextion text box (Read 9701 times) previous topic - next topic

andyturner

Hi

Does anyone know how I can get the float number from my accelerometer into a taxt box on the Nextion HMI Screen. I know its a long shot but thought I would try.

Thanks
Andy

IndianaTux

This is probably the how-to you want to read:

https://randomnerdtutorials.com/nextion-display-with-arduino-getting-started/

Shows how to use that display with an Arduino.

andyturner

This is probably the how-to you want to read:

https://randomnerdtutorials.com/nextion-display-with-arduino-getting-started/

Shows how to use that display with an Arduino.
Thank you for the lead. However I have some knowledge and have almost completed my project but can't seem to get my head around this problem, just thought someone may have had a similar experience.

Thanks
Andy

IndianaTux

Do you have the Nextion HMI Screen connected to the Arduino and working ?

andyturner

Do you have the Nextion HMI Screen connected to the Arduino and working ?
[Hi
Yes all working good, and most of my project is completed just need to get code for this and then im done
Thanks
Andy]

IndianaTux

To what I read it would be as simple as:

Code: [Select]

static char buffer[6];
dtostrf(accelerometer_value_to_display, 6, 2, buffer);
your_text_box_id.setText(buffer);

andyturner

To what I read it would be as simple as:

Code: [Select]

static char buffer[6];
dtostrf(accelerometer_value_to_display, 6, 2, buffer);
your_text_box_id.setText(buffer);

[Hi
As the float is in the loop i am not sure how to code this, in your example it asks for the accelerometer value which is in fact a float, so would I place the float in that part of the code?
Thanks
Andy]

andyturner

To what I read it would be as simple as:

Code: [Select]

static char buffer[6];
dtostrf(accelerometer_value_to_display, 6, 2, buffer);
your_text_box_id.setText(buffer);

[Hi
As the float is in the loop i am not sure how to code this, in your example it asks for the accelerometer value which is in fact a float, so would I place the float in that part of the code?
Thanks
Andy]
[

static char buffer[6];
dtostrf(((float)y - 329.5) / 68.5 * 9.8, 0), 6, 2, buffer);
t0.setText(buffer);

gives an error of
c:\program files (x86)\arduino\hardware\tools\avr\avr\include\stdlib.h:666:14: note: declared here

 extern char *dtostrf(double __val, signed char __width,

              ^

exit status 1
too few arguments to function 'char* dtostrf(double, signed char, unsigned char, char*)'

Thanks]

IndianaTux

How about:

Code: [Select]
#include <ADXL335.h>
#include <Nextion.h>
#include <Arduino.h>

#define SerialNxtn.Serial2;  // <== Change Serial1 to what you are using

NexText t0 = NexText(0, 1, "t0");

char buffer[100] = {0};

int val;
int xpin = A0;                  // x-axis of the accelerometer
int ypin = A1;                  // y-axis
int zpin = A2;                  // z-axis (only on 3-axis models)

void setup()
{
  Serial.begin(9600);
  dbSerialPrintln("setup done");
}

void loop()
{
  int x = analogRead(xpin);  //read from xpin
  int y = analogRead(ypin);  //read from ypin
  int z = analogRead(zpin);  //read from zpin
  static char buffer[6];

  float zero_G = 512.0; //ADC is 0~1023  the zero g output equal to Vs/2
  float scale = 102.3;  //ADXL335330 Sensitivity is 330mv/g
  float accel_y = ((float)y - 329.5) / 68.5 * 9.8;

  Serial.print(accel_y);
  Serial.println("%");
 
  dtostrf(accel_y, 6, 2, buffer);
  t0.setText(buffer);

  delay(2000);
}


Should compile just fine.

IndianaTux


andyturner

Hi

Sorry for the late reply, had a little issue with the serial connections on the Nextion. All sorted now and YES the code works a treat, does exactly what I wanted. Thank you very much for your help.

Andy

andyturner

Hi IndianaTux

While you are on a roll, I don't suppose you would know how to control a single servo using the outputted result from the text box populated with the accelerometer reading? I currently have the following but not working.

#include <Servo.h>
#include <Nextion.h>

NexButton b0 = NexButton(0, 1, "b0");


NexText t0 = NexText(0, 1, "t0");

char buffer[100] = {0};


int xpin = A0;                  // x-axis of the accelerometer
int ypin = A1;                  // y-axis
int zpin = A2;                  // z-axis (only on 3-axis models)

NexTouch *nex_listen_list[] =
{
  &b0,
   NULL  // String terminated
};

Servo myservo;  // create servo object to control a servo

int pos = 0;    // variable to store the servo position

void b0PushCallback(void *ptr)  // Press event for button b1
{
  pos = 90;
  myservo.write(pos);
  delay(100);
}  // End of press event




void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  dbSerialPrintln("setup done");
  nexInit();
  Serial.begin(9600);// Start serial comunication at baud=9600
  Serial.write(0xff);  // We always have to send this three lines after each command sent to nextion.
  Serial.write(0xff);
  Serial.write(0xff);

  nexInit();
 
  b0.attachPush(b0PushCallback);
 
}

void loop() {
  nexLoop(nex_listen_list);
 
  int x = analogRead(xpin);  //read from xpin
  int y = analogRead(ypin);  //read from ypin
  int z = analogRead(zpin);  //read from zpin
 

  float zero_G = 512.0; //ADC is 0~1023  the zero g output equal to Vs/2
  float scale = 102.4;  //ADXL335330 Sensitivity is 330mv/g
  float accel_y = ((y - 329.5)/68.5*9.8);

  Serial.print(accel_y);
  Serial.println("%");
 
  dtostrf(accel_y, 2, 1, buffer);
  t0.setText(buffer);
 
  delay(2000);
 
 
  pos = t0.setText(buffer);
 

Thanks

Andy

Go Up