Advertisement
mikroavr

nextion_text_arduino

Dec 29th, 2021
1,694
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. unsigned int data_pres = 0;
  2. unsigned int avg = 0;
  3.  
  4. #include <movingAvg.h>
  5. movingAvg avgPres(20);                                  
  6. #include <SoftwareSerial.h>
  7. SoftwareSerial nexSerial(3, 2); // RX, TX
  8.  
  9.  
  10. void setup() {
  11.   Serial.begin(9600);
  12.   nexSerial.begin(9600);                                          
  13.   pinMode(A0, INPUT);
  14.   avgPres.begin();
  15.   delay(1000);
  16. }
  17.  
  18. void loop() {
  19.   //data_pres = map(analogRead(A0),0,1024,0,255);  //Read the pot value ann map it to 0.255 (max value of waveform=255)
  20.   tampil_text("t0", "Press(psi)");
  21.   tampil_text("t1", "-3");
  22.   tampil_text("t2", "-2");
  23.   tampil_text("t3", "-1");
  24.   tampil_text("t4", "0");
  25.   tampil_text("t5", "1");
  26.   tampil_text("t6", "");
  27.  
  28.   int dt_adc = analogRead(A0);
  29.   data_pres = dt_adc*4; // scaling pressure graph
  30.   avg = avgPres.reading(dt_adc);
  31.  
  32.   float voltage = (avg*5.0)/1024.0;
  33.  
  34.   float pressure_pascal = (3.0*((float)voltage-0.475))*1000000.0;                       //calibrate here
  35.   float pressure_bar = pressure_pascal/10e5;
  36.   float pressure_psi = pressure_bar*14.5038;
  37.   pressure_psi = pressure_psi - 1;
  38.   Serial.println(pressure_psi);
  39.  
  40.  
  41.   String Tosend = "add ";                                      
  42.     Tosend += 1;                                              
  43.     Tosend += ",";
  44.     Tosend += 0;                                              
  45.     Tosend += ",";
  46.     Tosend += data_pres;                                            
  47.     nexSerial.print(Tosend);
  48.     nexSerial.write(0xff);
  49.     nexSerial.write(0xff);
  50.     nexSerial.write(0xff);
  51.  
  52.   delay(5);
  53. }
  54.  
  55. void tampil_text(String _add_txt, String _txt_display) {
  56.   String buf_txt = _add_txt + ".txt="  + "\"" + _txt_display + "\"";  
  57.   nexSerial.print(buf_txt);
  58.   nexSerial.write(0xff);
  59.   nexSerial.write(0xff);
  60.   nexSerial.write(0xff);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement