AnalogReadToProgressbarCode.ino 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * AnalogReadToProgressbarCode.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 progress bar
  18. * on Nextion displays and change it's value.
  19. * As example we are going to display the value of an analog input.
  20. */
  21. //********** Progress bar's value data range is min 0 max 100 **********
  22. // **** A re-map of value range must de done ****
  23. /* To update the value of a progress bar you can use the writeNum() function
  24. * writeNum("j0.val", 55); set the value of j0(object Name) to 55
  25. * writeNum("j0.val", volt); set the value of j0(object Name) equal with the value of volt variable
  26. */
  27. /* To make a vertical progress bar set the **dez** attribute to vertical
  28. * And change the values of width and height of the progress bar.
  29. */
  30. /* To reverse the direction of progress bar where the default in horizontal is: left to right
  31. * and down to up in vertical align, subtract the value from 100 and reverse the colors
  32. * of foreground and background between them
  33. */
  34. /* There is no need to send the value from Arduino straight to the progress bar with writeNum();
  35. * as most times the progress bar is not the only component on a page that must be updated
  36. * usually the display of the value on a box also needed
  37. * On some cases a change of color on a box or on progress bar also needed when value reach a point.
  38. * As the value of the progress bar must be re-mapped to 0-100 and in order to not send the same value multiple times
  39. * with different formats and limit the data from Serial, use the advantages of Nextion graphics to
  40. * create a variable and save there the value you want to appear in the progress bar.
  41. * Use the features provided by Nextion via the user code on a timer to update the progress bar
  42. * and at the same time display the value or whatever else you need.
  43. */
  44. #include "EasyNextionLibrary.h" // Include EasyNextionLibrary
  45. EasyNex myNex(Serial); // Create an object of EasyNex class with the name < myNex >
  46. // Set as parameter the Hardware Serial you are going to use
  47. uint16_t voltage; // a variable to store the reading
  48. // for simplicity Reasons we do not use float and we are going to take the measure in millivolts
  49. const int REFRESH_TIME = 100; // time to refresh the Nextion page every 100 ms
  50. unsigned long refresh_timer = millis(); // timer for refreshing Nextion's page
  51. void setup() {
  52. pinMode(A0, INPUT); // set A0 pin as INPUT
  53. myNex.begin(9600); // Begin the object with a baud rate of 9600
  54. // If no parameter was given in the begin(), the default baud rate of 9600 will be used
  55. }
  56. void loop() {
  57. if((millis()-refresh_timer) > REFRESH_TIME){ //IMPORTANT do not have serial print commands in the loop without a delay
  58. // or an if statement with a timer condition like this one.
  59. int tempData = analogRead(A0); // Read the analog pin
  60. voltage = map(tempData, 0, 1024, 0, 5000); // same like: voltage = analogRead(A0)*5000/1024
  61. /* We Re-map the value of tempData from 0-1024 (steps) to 0-5000 millivolt
  62. * connect the pins of a Potentiometer on A0 pin, 5v (5000 millivolt) and GND. Outer pins to 5v and GND, middle pin to A0
  63. * https://www.arduino.cc/en/tutorial/potentiometer
  64. * Turn it over and read values from 0 to 5000 millivolts
  65. */
  66. myNex.writeNum("Nvoltage.val", voltage); // Nvoltage.val is a variable that we have create on Nextion.
  67. // we send the value of the voltage variable on Arduino
  68. // you can use the same name for variables on Nextion for easy recognition with a capital N in front
  69. // Avoid the use of big variable names as every character is one byte to serial.
  70. // In here we use big names for the sake of example.
  71. refresh_timer = millis(); // Set the timer equal to millis, create a time stamp to start over the "delay"
  72. }
  73. }
  74. /* The rest work is on Nextion with the code on a timers user event
  75. n0.val=Nvoltage.val // write Nvoltage.val to n0.val
  76. sys0=Nvoltage.val*100/5000 // use sys0 to make the calculations
  77. j0.val=sys0 // add the value to the Progress bar
  78. //
  79. //Reverse direction progress bar
  80. j1.val=100-sys0
  81. //
  82. // Vertical allign
  83. j2.val=sys0
  84. //
  85. // Progress bar can take values from 0-100
  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*100/5000
  93. //
  94. // And some graphic effects
  95. if(n0.val>3300)
  96. {
  97. n0.bco=RED
  98. j0.pco=RED
  99. j1.bco=RED //bco for the reversed
  100. j2.pco=RED
  101. }else
  102. {
  103. n0.bco=YELLOW
  104. j0.pco=1024
  105. j1.bco=1024
  106. j2.pco=1024
  107. }
  108. */