Guest User

Arduino-Nextion

a guest
Feb 16th, 2020
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 26.95 KB | None | 0 0
  1. /*
  2. This sketch shows examples on how to send data from the Nextion display to arduino, and vice versa.
  3.  
  4. I didn't find a reliable way to receive data from the nextion display without using the library
  5. so I am going to use the official library to receive data, but I am not going to use it to send data to the display
  6. because it can create problems with touch events when we send data in the loop.
  7. I think it's easier to send data to the display without the library, anyway.
  8.  
  9. Connection with Arduino Uno/Nano:
  10. * +5V = 5V
  11. * TX  = pin 0 (RX)
  12. * RX  = pin 1 (TX)
  13. * GND = GND
  14.  
  15. If you are going to use an Arduino Mega, you have to edit everything on this sketch that says "Serial1"
  16. and replace it with "Serial11" (or whatever number you are using). Also define the Serial1 port on NexConfig.h
  17. inside the nextion library.
  18.  
  19.  
  20. Nextion library: https://github.com/itead/ITEADLIB_Arduino_Nextion
  21.  
  22.  
  23. This sketch was made for my 2nd video tutorial shown here: https://www.youtube.com/watch?v=mdkUBB60HoI&t=26s
  24.  
  25. Made by InterlinkKnight
  26. Last update: 02/22/2018
  27. */
  28.  
  29.  
  30. #include <Nextion.h>  // Include the nextion library (the official one) https://github.com/itead/ITEADLIB_Arduino_Nextion
  31.                       // Make sure you edit the NexConfig.h file on the library folder to set the correct Serial1 port for the display.
  32.                       // By default it's set to Serial11, which most arduino boards don't have.
  33.                       // Change "#define nexSerial1 Serial11" to "#define nexSerial1 Serial1" if you are using arduino uno, nano, etc.
  34.  
  35.  
  36.  
  37. int variable1 = 0;  // Create a variable to have a counter going up by one on each cycle
  38. int counter = 0;  // Create a variable to have a counter for the + and - buttons
  39. int CurrentPage = 0;  // Create a variable to store which page is currently loaded
  40.  
  41.  
  42.  
  43.  
  44.  
  45. // Declare objects that we are going to read from the display. This includes buttons, sliders, text boxes, etc:
  46. // Format: <type of object> <object name> = <type of object>(<page id>, <object id>, "<object name>");
  47. /* ***** Types of objects:
  48.  * NexButton - Button
  49.  * NexDSButton - Dual-state Button
  50.  * NexHotspot - Hotspot, that is like an invisible button
  51.  * NexCheckbox - Checkbox
  52.  * NexRadio - "Radio" checkbox, that it's exactly like the checkbox but with a rounded shape
  53.  * NexSlider - Slider
  54.  * NexGauge - Gauge
  55.  * NexProgressBar - Progress Bar
  56.  * NexText - Text box
  57.  * NexScrolltext - Scroll text box
  58.  * NexNumber - Number box
  59.  * NexVariable - Variable inside the nextion display
  60.  * NexPage - Page touch event
  61.  * NexGpio - To use the Expansion Board add-on for Enhanced Nextion displays
  62.  * NexRtc - To use the real time clock for Enhanced Nextion displays
  63.  * *****
  64.  */
  65. NexButton b1 = NexButton(0, 9, "b1");  // Button added
  66. NexButton b0 = NexButton(0, 1, "b0");  // Button added
  67. NexButton b4 = NexButton(0, 11, "b4");  // Button added
  68. NexDSButton bt0 = NexDSButton(0, 8, "bt0");  // Dual state button added
  69. NexSlider h0 = NexSlider(0, 4, "h0");  // Slider added
  70. NexText t5 = NexText(2, 12, "t5");  // Text box added, so we can read it
  71. NexText t6 = NexText(2, 13, "t6");  // Text box added, so we can read it
  72. NexText t7 = NexText(2, 14, "t7");  // Text box added, so we can read it
  73. NexRadio r0 = NexRadio(2, 4, "r0");  // Radio checkbox added
  74. NexRadio r1 = NexRadio(2, 5, "r1");  // Radio checkbox added
  75. NexRadio r2 = NexRadio(2, 6, "r2");  // Radio checkbox added
  76. NexCheckbox c0 = NexCheckbox(2, 3, "c0");  // Checkbox added
  77. NexButton j0 = NexButton(2, 2, "j0");  // Progress bar as a button added, so if we press the progress bar we can create an action
  78. NexButton b21 = NexButton(2, 10, "b21");  // Button added
  79. NexButton b22 = NexButton(2, 16, "b22");  // Button added
  80. NexButton b23 = NexButton(2, 18, "b23");  // Button added
  81. NexButton b24 = NexButton(2, 19, "b24");  // Button added
  82.  
  83. // Declare pages:
  84. // Sending data to the display to nonexistent objects on the current page creates an error code sent by the display.
  85. // Any error sent by the display creates lag on the arduino loop because arduino tries to read it, thinking it's a touch event.
  86. // So to avoid this, I am only going to send data depending on the page the display is on.
  87. // That's the reason I want the arduino to know which page is loaded on the display.
  88. // To let arduino know what page is currently loaded, we are creating a touch event for each page.
  89. // On the nextion project, each page most send a simulated "Touch Press Event" in the "Preinitialize Event" section so
  90. // we can register that a new page was loaded.
  91. NexPage page0 = NexPage(0, 0, "page0");  // Page added as a touch event
  92. NexPage page1 = NexPage(1, 0, "page1");  // Page added as a touch event
  93. NexPage page2 = NexPage(2, 0, "page2");  // Page added as a touch event
  94.  
  95. // End of declaring objects
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103. char buffer[100] = {0};  // This is needed only if you are going to receive a text from the display. You can remove it otherwise.
  104.                          // Further on this sketch I do receive text so that's why I created this buffer.
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112. // Declare touch event objects to the touch event list:
  113. // You just need to add the names of the objects that send a touch event.
  114. // Format: &<object name>,
  115.  
  116. NexTouch *nex_listen_list[] =
  117. {
  118.   &b1,  // Button added
  119.   &b0,  // Button added
  120.   &b4,  // Button added
  121.   &b21,  // Button added
  122.   &b22,  // Button added
  123.   &b23,  // Button added
  124.   &b24,  // Button added
  125.   &bt0,  // Dual state button added
  126.   &h0,  // Slider added
  127.   &r0,  // Radio checkbox added
  128.   &r1,  // Radio checkbox added
  129.   &r2,  // Radio checkbox added
  130.   &c0,  // Checkbox added
  131.   &j0,  // Progress bar as a button added
  132.   &page0,  // Page added as a touch event
  133.   &page1,  // Page added as a touch event
  134.   &page2,  // Page added as a touch event
  135.   NULL  // String terminated
  136. };  // End of touch event list
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146. ////////////////////////// Touch events:
  147. // Each of the following sections are going to run everytime the touch event happens:
  148. // Is going to run the code inside each section only ones for each touch event.
  149.  
  150. void b1PushCallback(void *ptr)  // Press event for button b1
  151. {
  152.   digitalWrite(LED_BUILTIN, HIGH);  // Turn ON internal LED
  153. }  // End of press event
  154.  
  155.  
  156.  
  157.  
  158.  
  159. void b1PopCallback(void *ptr)  // Release event for button b1
  160. {
  161.   digitalWrite(LED_BUILTIN, LOW);  // Turn OFF internal LED
  162. }  // End of release event
  163.  
  164.  
  165.  
  166.  
  167.  
  168. void b0PushCallback(void *ptr)  // Press event for button b0
  169. {
  170.   counter = counter - 1;  // Subtract 1 to the current value of the counter
  171.  
  172.   Serial1.print("n3.val=");  // This is sent to the nextion display to set what object name (before the dot) and what atribute (after the dot) are you going to change.
  173.   Serial1.print(counter);  // This is the value you want to send to that object and atribute mentioned before.
  174.   Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
  175.   Serial1.write(0xff);
  176.   Serial1.write(0xff);
  177. }  // End of press event
  178.  
  179.  
  180.  
  181.  
  182.  
  183. void b4PushCallback(void *ptr)  // Press event for button b4
  184. {
  185.   counter = counter + 1;  // Add 1 to the current value of the counter
  186.  
  187.   Serial1.print("n3.val=");  // This is sent to the nextion display to set what object name (before the dot) and what atribute (after the dot) are you going to change.
  188.   Serial1.print(counter);  // This is the value you want to send to that object and atribute mentioned before.
  189.   Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
  190.   Serial1.write(0xff);
  191.   Serial1.write(0xff);
  192. }  // End of press event
  193.  
  194.  
  195.  
  196.  
  197.  
  198. void bt0PopCallback(void *ptr)  // Release event for dual state button bt0
  199. {
  200.   uint32_t number5 = 0;  // Create variable to store value we are going to get
  201.   bt0.getValue(&number5);  // Read value of dual state button to know the state (0 or 1)
  202.  
  203.   if(number5 == 1){  // If dual state button is equal to 1 (meaning is ON)...
  204.     digitalWrite(LED_BUILTIN, HIGH);  // Turn ON internal LED
  205.   }else{  // Since the dual state button is OFF...
  206.     digitalWrite(LED_BUILTIN, LOW);  // Turn OFF internal LED
  207.   }
  208. }  // End of release event
  209.  
  210.  
  211.  
  212.  
  213.  
  214. void h0PopCallback(void *ptr)  // Release event for slider
  215. {
  216.   uint32_t number2 = 0;  // Create variable to store value of slider
  217.   h0.getValue(&number2);  // Read the value of the slider
  218.  
  219.   // I got a problem where sometimes I received a 0 instead of the correct slider value.
  220.   // To fix this I will put a condition that if I get a 0, I am going to read again the slider to make sure I get the real value.
  221.   // I am going to do this a few times because sometimes it takes a few tries to get the correct value.
  222.   // The problem looks like it have something to do with touch events. Everytime the display sends a touch event,
  223.   // we need to wait that data to finish transmiting before we can get another data from the display (in this case
  224.   // we want the slider position). For this reason it's important to use a high Serial1 baud (ideally 115200)
  225.   // so it doesn't have to wait too long for the touch event to finish sending the data.
  226.  
  227.   // The "Are you sure is 0?" begins:
  228.   if(number2==0){  // If I got a 0, then recheck:
  229.     h0.getValue(&number2);  // Read the value of the slider
  230.   }
  231.   if(number2==0){  // If I got a 0, then recheck:
  232.     h0.getValue(&number2);  // Read the value of the slider
  233.   }
  234.   if(number2==0){  // If I got a 0, then recheck:
  235.     h0.getValue(&number2);  // Read the value of the slider
  236.   }
  237.   if(number2==0){  // If I got a 0, then recheck:
  238.     h0.getValue(&number2);  // Read the value of the slider
  239.   }
  240.   if(number2==0){  // If I got a 0, then recheck:
  241.     h0.getValue(&number2);  // Read the value of the slider
  242.   }
  243.   if(number2==0){  // If I got a 0, then recheck:
  244.     h0.getValue(&number2);  // Read the value of the slider
  245.   }
  246.   if(number2==0){  // If I got a 0, then recheck:
  247.     h0.getValue(&number2);  // Read the value of the slider
  248.   }
  249.   if(number2==0){  // If I got a 0, then recheck:
  250.     h0.getValue(&number2);  // Read the value of the slider
  251.   }
  252.   if(number2==0){  // If I got a 0, then recheck:
  253.     h0.getValue(&number2);  // Read the value of the slider
  254.   }
  255.   if(number2==0){  // If I got a 0, then recheck:
  256.     h0.getValue(&number2);  // Read the value of the slider
  257.   }
  258.   if(number2==0){  // If I got a 0, then recheck:
  259.     h0.getValue(&number2);  // Read the value of the slider
  260.   }
  261.   if(number2==0){  // If I got a 0, then recheck:
  262.     h0.getValue(&number2);  // Read the value of the slider
  263.   }
  264.   if(number2==0){  // If I got a 0, then recheck:
  265.     h0.getValue(&number2);  // Read the value of the slider
  266.   }
  267.   if(number2==0){  // If I got a 0, then recheck:
  268.     h0.getValue(&number2);  // Read the value of the slider
  269.   }
  270.   if(number2==0){  // If I got a 0, then recheck:
  271.     h0.getValue(&number2);  // Read the value of the slider
  272.   }
  273.   if(number2==0){  // If I got a 0, then recheck:
  274.     h0.getValue(&number2);  // Read the value of the slider
  275.   }
  276.   if(number2==0){  // If I got a 0, then recheck:
  277.     h0.getValue(&number2);  // Read the value of the slider
  278.   }
  279.   if(number2==0){  // If I got a 0, then recheck:
  280.     h0.getValue(&number2);  // Read the value of the slider
  281.   }
  282.   if(number2==0){  // If I got a 0, then recheck:
  283.     h0.getValue(&number2);  // Read the value of the slider
  284.   }
  285.   if(number2==0){  // If I got a 0, then recheck:
  286.     h0.getValue(&number2);  // Read the value of the slider
  287.   }
  288.   if(number2==0){  // If I got a 0, then recheck:
  289.     h0.getValue(&number2);  // Read the value of the slider
  290.   }
  291.   // The "Are you sure is 0?" ended. At this point, if the slider keep showing as 0, then it really is at 0.
  292.  
  293.   // Now is going to send the value it received by the slider:
  294.   Serial1.print("n4.val=");  // This is sent to the nextion display to set what object name (before the dot) and what atribute (after the dot) are you going to change.
  295.   Serial1.print(number2);  // This is the value you want to send to that object and atribute mentioned before.
  296.   Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
  297.   Serial1.write(0xff);
  298.   Serial1.write(0xff);
  299. }  // End of release event
  300.  
  301.  
  302.  
  303.  
  304.  
  305. void r0PushCallback(void *ptr)  // Press event for radio checkbox
  306. {
  307.   Serial1.print("n9.val=");  // This is sent to the nextion display to set what object name (before the dot) and what atribute (after the dot) are you going to change.
  308.   Serial1.print(1);  // This is the value you want to send to that object and atribute mentioned before.
  309.   Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
  310.   Serial1.write(0xff);
  311.   Serial1.write(0xff);
  312. }  // End of press event
  313.  
  314.  
  315.  
  316.  
  317.  
  318. void r1PushCallback(void *ptr)  // Press event for radio checkbox
  319. {
  320.   Serial1.print("n9.val=");  // This is sent to the nextion display to set what object name (before the dot) and what atribute (after the dot) are you going to change.
  321.   Serial1.print(2);  // This is the value you want to send to that object and atribute mentioned before.
  322.   Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
  323.   Serial1.write(0xff);
  324.   Serial1.write(0xff);
  325. }  // End of press event
  326.  
  327.  
  328.  
  329.  
  330.  
  331. void r2PushCallback(void *ptr)  // Press event for radio checkbox
  332. {
  333.   Serial1.print("n9.val=");  // This is sent to the nextion display to set what object name (before the dot) and what atribute (after the dot) are you going to change.
  334.   Serial1.print(3);  // This is the value you want to send to that object and atribute mentioned before.
  335.   Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
  336.   Serial1.write(0xff);
  337.   Serial1.write(0xff);
  338. }  // End of press event
  339.  
  340.  
  341.  
  342.  
  343.  
  344. void c0PushCallback(void *ptr)  // Press event for checkbox
  345. {
  346.   uint32_t number3 = 0;  // Create variable to store the value of the state of the checkbox
  347.   c0.getValue(&number3);  // Read the state of the checkbox
  348.   Serial1.print("n1.val=");  // This is sent to the nextion display to set what object name (before the dot) and what atribute (after the dot) are you going to change.
  349.   Serial1.print(number3);  // This is the value you want to send to that object and atribute mentioned before.
  350.   Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
  351.   Serial1.write(0xff);
  352.   Serial1.write(0xff);
  353. }  // End of press event
  354.  
  355.  
  356.  
  357.  
  358.  
  359. void j0PushCallback(void *ptr)  // Press event for progress bar
  360. {
  361.   Serial1.print("j0.pco=");  // This is sent to the nextion display to set what object name (before the dot) and what atribute (after the dot) are you going to change.
  362.   Serial1.print(63488);  // Code for the color red
  363.   Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
  364.   Serial1.write(0xff);
  365.   Serial1.write(0xff);
  366. }  // End of press event
  367.  
  368.  
  369.  
  370.  
  371.  
  372. void b21PushCallback(void *ptr)  // Press event for "Home" button on page 2
  373. {
  374.   Serial1.print("page 0");  // Sending this it's going to tell the nextion display to go to page 0.
  375.   Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
  376.   Serial1.write(0xff);
  377.   Serial1.write(0xff);
  378. }  // End of press event
  379.  
  380.  
  381.  
  382.  
  383.  
  384. void b22PushCallback(void *ptr)  // Press event for "Send" button on page 2
  385. {
  386.   memset(buffer, 0, sizeof(buffer));  // Clear the buffer, so we can start using it
  387.   t5.getText(buffer, sizeof(buffer));  // Read the text on the object t5 and store it on the buffer
  388.  
  389.   // Now is going to send the text we received to object t23:
  390.   Serial1.print("t23.txt=");  // This is sent to the nextion display to set what object name (before the dot) and what atribute (after the dot) are you going to change.
  391.   Serial1.print("\"");  // Since we are sending text, and not a number, we need to send double quote before and after the actual text.
  392.   Serial1.print(buffer);  // This is the text you want to send to that object and atribute mentioned before.
  393.   Serial1.print("\"");  // Since we are sending text, and not a number, we need to send double quote before and after the actual text.
  394.   Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
  395.   Serial1.write(0xff);
  396.   Serial1.write(0xff);
  397. }  // End of press event
  398.  
  399.  
  400.  
  401.  
  402.  
  403. void b23PushCallback(void *ptr)  // Press event for "Send" button on page 2
  404. {
  405.   memset(buffer, 0, sizeof(buffer));  // Clear the buffer, so we can start using it
  406.   t6.getText(buffer, sizeof(buffer));  // Read the text on the object t6 and store it on the buffer
  407.  
  408.   // Now is going to send the text we received to object t23:
  409.   Serial1.print("t23.txt=");  // This is sent to the nextion display to set what object name (before the dot) and what atribute (after the dot) are you going to change.
  410.   Serial1.print("\"");  // Since we are sending text, and not a number, we need to send double quote before and after the actual text.
  411.   Serial1.print(buffer);  // This is the text you want to send to that object and atribute mentioned before.
  412.   Serial1.print("\"");  // Since we are sending text, and not a number, we need to send double quote before and after the actual text.
  413.   Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
  414.   Serial1.write(0xff);
  415.   Serial1.write(0xff);
  416. }  // End of press event
  417.  
  418.  
  419.  
  420.  
  421.  
  422. void b24PushCallback(void *ptr)  // Press event for "Send" button on page 2
  423. {
  424.   memset(buffer, 0, sizeof(buffer));  // Clear the buffer, so we can start using it
  425.   t7.getText(buffer, sizeof(buffer));  // Read the text on the object t7 and store it on the buffer
  426.  
  427.   // Now is going to send the text we received to object t23:
  428.   Serial1.print("t23.txt=");  // This is sent to the nextion display to set what object name (before the dot) and what atribute (after the dot) are you going to change.
  429.   Serial1.print("\"");  // Since we are sending text, and not a number, we need to send double quote before and after the actual text.
  430.   Serial1.print(buffer);  // This is the text you want to send to that object and atribute mentioned before.
  431.   Serial1.print("\"");  // Since we are sending text, and not a number, we need to send double quote before and after the actual text.
  432.   Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
  433.   Serial1.write(0xff);
  434.   Serial1.write(0xff);
  435. }  // End of press event
  436.  
  437.  
  438.  
  439.  
  440.  
  441. // Page change event:
  442. void page0PushCallback(void *ptr)  // If page 0 is loaded on the display, the following is going to execute:
  443. {
  444.   CurrentPage = 0;  // Set variable as 0 so from now on arduino knows page 0 is loaded on the display
  445. }  // End of press event
  446.  
  447.  
  448. // Page change event:
  449. void page1PushCallback(void *ptr)  // If page 1 is loaded on the display, the following is going to execute:
  450. {
  451.   CurrentPage = 1;  // Set variable as 1 so from now on arduino knows page 1 is loaded on the display
  452. }  // End of press event
  453.  
  454.  
  455. // Page change event:
  456. void page2PushCallback(void *ptr)  // If page 2 is loaded on the display, the following is going to execute:
  457. {
  458.   CurrentPage = 2;  // Set variable as 2 so from now on arduino knows page 2 is loaded on the display
  459. }  // End of press event
  460.  
  461.  
  462.  
  463.  
  464. ////////////////////////// End of touch events
  465.  
  466.  
  467.  
  468.  
  469.  
  470.  
  471.  
  472.  
  473.  
  474.  
  475.  
  476. void setup() {  // Put your setup code here, to run once:
  477.  
  478.   Serial1.begin(9600);  // Start Serial1 comunication at baud=9600
  479.  
  480.  
  481.  
  482.  
  483.  
  484.  
  485.   // Register the event callback functions of each touch event:
  486.   // You need to register press events and release events seperatly.
  487.   // Format for press events: <object name>.attachPush(<object name>PushCallback);
  488.   // Format for release events: <object name>.attachPop(<object name>PopCallback);
  489.   b1.attachPush(b1PushCallback);  // Button press
  490.   b1.attachPop(b1PopCallback);  // Button release
  491.   b0.attachPush(b0PushCallback);  // Button press
  492.   b4.attachPush(b4PushCallback);  // Button press
  493.   b21.attachPush(b21PushCallback);  // Button press
  494.   b22.attachPush(b22PushCallback);  // Button press
  495.   b23.attachPush(b23PushCallback);  // Button press
  496.   b24.attachPush(b24PushCallback);  // Button press
  497.   bt0.attachPop(bt0PopCallback);  // Dual state button bt0 release
  498.   h0.attachPop(h0PopCallback);  // Slider release
  499.   r0.attachPush(r0PushCallback);  // Radio checkbox press
  500.   r1.attachPush(r1PushCallback);  // Radio checkbox press
  501.   r2.attachPush(r2PushCallback);  // Radio checkbox press
  502.   c0.attachPush(c0PushCallback);  // Radio checkbox press
  503.   j0.attachPush(j0PushCallback);  // Progress bar as a button press
  504.   page0.attachPush(page0PushCallback);  // Page press event
  505.   page1.attachPush(page1PushCallback);  // Page press event
  506.   page2.attachPush(page2PushCallback);  // Page press event
  507.  
  508.   // End of registering the event callback functions
  509.  
  510.   pinMode(LED_BUILTIN, OUTPUT);
  511.  
  512. }  // End of setup
  513.  
  514.  
  515.  
  516.  
  517. void loop() {  // Put your main code here, to run repeatedly:
  518.  
  519.  
  520.   delay(30);  // This is the only delay on this loop.
  521.               // I put this delay because without it, the timer on the display would stop running.
  522.               // The timer I am talking about is the one called tm0 on page 0 (of my example nextion project).
  523.               // Aparently we shouldn't send data to the display too often.
  524.  
  525.  
  526.  
  527.   // I created the following variable to have a dynamic number to send to the display:
  528.   variable1++;  // Add 1 to the variable1
  529.   if(variable1 > 240){  // If the variable is above 240
  530.     variable1 = 0;  // Set variable to 0 to start over
  531.   }
  532.  
  533.  
  534.  
  535.  
  536.  
  537.  
  538.  
  539.  
  540.  
  541.   // Send variable1 value to the object called n0:
  542.   // This object (n0) exist on every page so at this point we don't need to check which page is loaded on the display.
  543.   Serial1.print("n0.val=");  // This is sent to the nextion display to set what object name (before the dot) and what atribute (after the dot) are you going to change.
  544.   Serial1.print(variable1);  // This is the value you want to send to that object and atribute mentioned before.
  545.   Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
  546.   Serial1.write(0xff);
  547.   Serial1.write(0xff);
  548.  
  549.  
  550.  
  551.  
  552.  
  553.  
  554.   // Send page number to the object called np:
  555.   // This object (np) exist on every page so at this point we don't need to check which page is loaded on the display.
  556.   Serial1.print("np.val=");  // This is sent to the nextion display to set what object name (before the dot) and what atribute (after the dot) are you going to change.
  557.   Serial1.print(CurrentPage);  // This is the value you want to send to that object and atribute mentioned before.
  558.   Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
  559.   Serial1.write(0xff);
  560.   Serial1.write(0xff);
  561.  
  562.  
  563.  
  564.  
  565.  
  566.  
  567.  
  568.  
  569.  
  570.  
  571.  
  572.  
  573.   // Sending data to the display to nonexistent objects on the current page creates an error code sent by the display.
  574.   // Any error sent by the display creates lag on the arduino loop because arduino tries to read it, thinking it's a touch event.
  575.   // So to avoid this, I am only going to send data depending on the page the display is on.
  576.   // That's the reason I want the arduino to know which page is loaded on the display.
  577.  
  578.   if(CurrentPage == 0){  // If the display is on page 0, do the following:
  579.  
  580.     // Send a text to the object called t1:
  581.     Serial1.print("t1.txt=");  // This is sent to the nextion display to set what object name (before the dot) and what atribute (after the dot) are you going to change.
  582.     Serial1.print("\"");  // Since we are sending text, and not a number, we need to send double quote before and after the actual text.
  583.     Serial1.print("Hello!");  // This is the text you want to send to that object and atribute mentioned before.
  584.     Serial1.print("\"");  // Since we are sending text, and not a number, we need to send double quote before and after the actual text.
  585.     Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
  586.     Serial1.write(0xff);
  587.     Serial1.write(0xff);
  588.   }
  589.  
  590.  
  591.  
  592.   if(CurrentPage == 1){  // If the display is on page 1, do the following:
  593.  
  594.     // Send a new value to the waveform. In this case we are sending the variable1 value that goes from 0 to 240:
  595.     Serial1.print("add 2,0,");  // This sends data to the waveform. There are 3 numbers you have to put with a comma
  596.                                // between them: [objectID],[Channel],[Value]
  597.                                // In this case 2 is the ID of the waveform; 0 is the channel number; and last value is
  598.                                // going to be send in the next line:
  599.     Serial1.print(variable1);  // This is the value we are going to send for the waveform and channel mentioned previously.
  600.                               // Range is from 0 to 255. Going over 255 will show the graphic line going to 0 and continue from there.
  601.                               // Each number it's a pixel. This means you can't do waveforms over 255 pixels high. Over that is unusable.
  602.     Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
  603.     Serial1.write(0xff);
  604.     Serial1.write(0xff);
  605.   }
  606.  
  607.  
  608.  
  609.  
  610.  
  611.   if(CurrentPage == 2){  // If the display is on page 2, do the following:
  612.  
  613.     // Send variable1 value to the progress bar called j0:
  614.     // If we send a value greater than 100, we are going to receive an error and this cause lag on the arduino loop, so we are
  615.     // going to constrain the value to avoid going over 100:
  616.     int variable2 = constrain(variable1, 0, 100);  // limits value between 0 and 100 to prevent going over this limits
  617.  
  618.     Serial1.print("j0.val=");  // This is sent to the nextion display to set what object name (before the dot) and what atribute (after the dot) are you going to change.
  619.     Serial1.print(variable2);  // This is the value you want to send to that object and atribute mentioned before.
  620.     Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
  621.     Serial1.write(0xff);
  622.     Serial1.write(0xff);
  623.  
  624.  
  625.     // Send variable1 value to the gauge called z0:
  626.     // Range for the gauge is from 0 to 360. Don't send a greater value or the display is going to send an error code and
  627.     // that creates lag on the arduino loop.
  628.     Serial1.print("z0.val=");  // This is sent to the nextion display to set what object name (before the dot) and what atribute (after the dot) are you going to change.
  629.     Serial1.print(variable1);  // This is the value you want to send to that object and atribute mentioned before.
  630.     Serial1.write(0xff);  // We always have to send this three lines after each command sent to the nextion display.
  631.     Serial1.write(0xff);
  632.     Serial1.write(0xff);
  633.  
  634.   }
  635.  
  636.  
  637.  
  638.  
  639.  
  640.  
  641.  
  642.   // We are going to check the list of touch events we enter previously to
  643.   // know if any touch event just happened, and excecute the corresponding instructions:
  644.  
  645.   nexLoop(nex_listen_list);  // Check for any touch event
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.  
  653.  
  654.  
  655.  
  656. }  // End of loop
Advertisement
Add Comment
Please, Sign In to add comment