AnalogReadToWaveformCode.ino 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * AnalogReadToWaveformCode.ino - Simple example code for EasyNextionLibrary
  3. * Copyright (c) 2020 Athanasios Seitanis < seithagta@gmail.com >.
  4. * https://www.seithan.com
  5. * All rights reserved. EasyNextionLibrary is licensed under the MIT License
  6. * https://opensource.org/licenses/MIT
  7. */
  8. /* I have invested time and resources providing open source codes, like this one.
  9. * Please do not hesitate to support my work!
  10. * If you found this work useful and has saved you time and effort,
  11. * Just simply paypal me at: seithagta@gmail.com
  12. */
  13. //********************************************************************************
  14. // You can find more examples, tutorials and projects with Nextion on my website
  15. // https://www.seithan.com
  16. //********************************************************************************
  17. /* This example aims to show how you can use and manage single waveform
  18. * on Nextion displays. As an example we are going to display the value of an analog input.
  19. * With the same way you can manage more than one channels or waveforms
  20. */
  21. /* Every waveform can have up to 4 channels. They are numbered from 0 to 3
  22. * Don't mistake the number of channels with the ID of the channel
  23. * ID is 0 for the first channel, 1 for the second, 2 for the third, 3 for the fourth
  24. */
  25. //********** Waveform's channel data range is min 0 max 255 **********
  26. // **** A re-map of value range must de done ****
  27. /* The command to Add single value to Waveform Channel is:
  28. * add <waveform>,<channel>,<value>
  29. * <waveform> is the .id of the waveform component
  30. * <channel> is the channel the data will be added to
  31. * <value> is ASCII text of data value, or numeric value
  32. */
  33. /* TIP: Waveform 1 pixel column used for every data value added on x - axis
  34. * on y - axis as the Data range is from 0 to 255 the height of waveform must be 255 pixel
  35. * in order to have a represent of zero to the lower point and 255 to the higher point of the waveform
  36. * If the height of the waveform is not 255 pixel use the * dis * attribute for scaling the data from 10 to 1000%
  37. * if waveform has a height of 127 pixel set data scaling * dis * to 50. Set dis to 200 for 510 pixel waveform height
  38. * You can use this math type < dis = 100*(waveform height in pixels)/255 >
  39. */
  40. /* There is no need to send from Arduino the command add <waveform>,<channel>,<value>
  41. * as most times the waveform is not the only component on a page that must be updated
  42. * usually the display of the value on a box also needed
  43. * On some cases a change of color on a box also needed when value reach a point.
  44. * As the value to waveform must be re-mapped to 0-255 and in order to not send the same value multiple times
  45. * with different formats and limit the data from Serial, use the advantages of Nextion graphics to
  46. * create a variable and save there the value you want to appear in the waveform.
  47. * Use the features provided by Nextion via the user code on a timer to update the waveform
  48. * and at the same time display the value or whatever else you need
  49. */
  50. #include "EasyNextionLibrary.h" // Include EasyNextionLibrary
  51. EasyNex myNex(Serial); // Create an object of EasyNex class with the name < myNex >
  52. // Set as parameter the Hardware Serial you are going to use
  53. uint16_t voltageGraph; // a variable to store the reading
  54. // for simplicity reasons, we do not use float and we are going to take the measure in millivolts
  55. const int REFRESH_TIME = 100; // time to refresh the Nextion page every 100 ms
  56. unsigned long refresh_timer = millis(); // timer for refreshing Nextion's page
  57. void setup() {
  58. pinMode(A0, INPUT); // set A0 pin as INPUT
  59. myNex.begin(9600); // Begin the object with a baud rate of 9600
  60. // If no parameter was given in the begin(), the default baud rate of 9600 will be used
  61. }
  62. void loop() {
  63. if((millis()-refresh_timer) > REFRESH_TIME){ //IMPORTANT do not have serial print commands in the loop without a delay
  64. // or an if statement with a timer condition like this one.
  65. int tempData = analogRead(A0); // Read the analog pin
  66. voltageGraph = map(tempData, 0, 1024, 0, 5000); // same like: voltageGraph = analogRead(A0)*5000/1024
  67. /* We Re-map the value of tempData from 0-1024 (steps) to 0-5000 millivolt
  68. * connect the pins of a Potentiometer on A0 pin, 5v (5000 millivolt) and GND. Outer pins to 5v and GND, middle pin to A0
  69. * https://www.arduino.cc/en/tutorial/potentiometer
  70. * Turn it over and read values from 0 to 5000 millivolts
  71. */
  72. myNex.writeNum("NvoltageGraph.val", voltageGraph); // NvoltageGraph.val is a variable that we have create on Nextion.
  73. // we send the value of the voltageGraph variable on Arduino
  74. // you can use the same name for variables on Nextion for easy recognition with a capital N infront
  75. // Avoid the use of big variable names as every character is one byte to serial.
  76. // In here we use big names for the sake of the example.
  77. refresh_timer = millis(); // Set the timer equal to millis, create a time stamp to start over the "delay"
  78. }
  79. }
  80. /* The rest work is on Nextion with the code on a timers user event
  81. sys0=NvoltageGraph.val*255/5000 // use sys0 to make the calculations
  82. add 2,0,sys0 // add the value to the waveform with id=2 at first channel (0)
  83. n0.val=NvoltageGraph.val // write NvoltageGraph.val to n0.val
  84. //
  85. // Waveform can take values from 0-250
  86. // we map the value from arduino 0-5000 :
  87. // the math type for map the range is:
  88. // return = (value - low1) * (high2 - low2) / (high1 - low1) + low2
  89. // as both ranges start from zero low1 and low2 = 0
  90. // the type becomes
  91. // return = value*hight2/hight1
  92. // return=value*255/5000
  93. //
  94. // And some graphic effects
  95. if(n0.val>3300)
  96. {
  97. n0.bco=RED
  98. }else
  99. {
  100. n0.bco=YELLOW
  101. }
  102. */