Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.48 KB | None | 0 0
  1. +5V = 5V
  2. TX = pin 0 (RX)
  3. RX = pin 1 (TX)
  4. GND = GND
  5.  
  6. #include <Nextion.h>
  7. // Include the nextion library (the official one) https://github.com/itead/ITEADLIB_Arduino_Nextion
  8. // Make sure you edit the NexConfig.h file on the library folder to set the correct serial port for the display.
  9. // By default it's set to Serial1, which most arduino boards don't have.
  10. // Change "#define nexSerial Serial1" to "#define nexSerial Serial" if you are using arduino uno, nano, etc.
  11.  
  12. int variable1 = 0; // Create a variable to have a counter going up by one on each cycle
  13. int counter = 0; // Create a variable to have a counter for the + and - buttons
  14. int CurrentPage = 0; // Create a variable to store which page is currently loaded
  15.  
  16. // Declare objects that we are going to read from the display. This includes buttons, sliders, text boxes, etc:
  17. // Format: <type of object> <object name> = <type of object>(<page id>, <object id>, "<object name>");
  18. /* ***** Types of objects:
  19. NexButton - Button
  20. NexDSButton - Dual-state Button
  21. NexHotspot - Hotspot, that is like an invisible button
  22. NexCheckbox - Checkbox
  23. NexRadio - "Radio" checkbox, that it's exactly like the checkbox but with a rounded shape
  24. NexSlider - Slider
  25. NexGauge - Gauge
  26. NexProgressBar - Progress Bar
  27. NexText - Text box
  28. NexScrolltext - Scroll text box
  29. NexNumber - Number box
  30. NexVariable - Variable inside the nextion display
  31. NexPage - Page touch event
  32. NexGpio - To use the Expansion Board add-on for Enhanced Nextion displays
  33. NexRtc - To use the real time clock for Enhanced Nextion displays
  34. * *****
  35. */
  36. NexText t0 = NexText(0, 1, "t0"); // Text box added, so we can read it
  37. NexText t1 = NexText(0, 2, "t1"); // Text box added, so we can read it
  38. NexNumber n0 = NexNumber(0, 3, "n0");
  39. NexNumber n1 = NexNumber(0, 4, "n1");
  40. // Declare pages:
  41. // Sending data to the display to nonexistent objects on the current page creates an error code sent by the display.
  42. // Any error sent by the display creates lag on the arduino loop because arduino tries to read it, thinking it's a touch event.
  43. // So to avoid this, I am only going to send data depending on the page the display is on.
  44. // That's the reason I want the arduino to know which page is loaded on the display.
  45. // To let arduino know what page is currently loaded, we are creating a touch event for each page.
  46. // On the nextion project, each page most send a simulated "Touch Press Event" in the "Preinitialize Event" section so
  47. // we can register that a new page was loaded.
  48. NexPage page0 = NexPage(0, 0, "page0"); // Page added as a touch event
  49. // End of declaring objects
  50.  
  51. char buffer[100] = {0}; // This is needed only if you are going to receive a text from the display. You can remove it otherwise.
  52. // Further on this sketch I do receive text so that's why I created this buffer.
  53. // Declare touch event objects to the touch event list:
  54. // You just need to add the names of the objects that send a touch event.
  55. // Format: &<object name>,
  56.  
  57. NexTouch *nex_listen_list[] = {
  58. &page0, // Page added as a touch event
  59. &t0,
  60. &t1,
  61. &n0,
  62. &n1,
  63. NULL // String terminated
  64. }; // End of touch event list
  65.  
  66. void setup()
  67. {
  68. nexInit();// Put your setup code here, to run once:
  69. Serial.begin(9600); // Start serial comunication at baud=9600
  70. pinMode(13, OUTPUT);
  71. // I am going to change the Serial baud to a faster rate.
  72. // The reason is that the slider have a glitch when we try to read it's value.
  73. // One way to solve it was to increase the speed of the serial port.
  74. delay(500); // This dalay is just in case the nextion display didn't start yet, to be sure it will receive the following command.
  75. Serial.print("baud=115200"); // Set new baud rate of nextion to 115200, but it's temporal. Next time nextion is power on,
  76. // it will retore to default baud of 9600.
  77. // To take effect, make sure to reboot the arduino (reseting arduino is not enough).
  78. // If you want to change the default baud, send the command as "bauds=115200", instead of "baud=115200".
  79. // If you change the default baud, everytime the nextion is power ON is going to have that baud rate, and
  80. // would not be necessery to set the baud on the setup anymore.
  81. Serial.write(0xff); // We always have to send this three lines after each command sent to nextion.
  82. Serial.write(0xff);
  83. Serial.write(0xff);
  84. Serial.end(); // End the serial comunication of baud=9600
  85. pinMode(13, OUTPUT);
  86. Serial.begin(115200); // Start serial comunication at baud=115200
  87. // Register the event callback functions of each touch event:
  88. // You need to register press events and release events seperatly.
  89. // Format for press events: <object name>.attachPush(<object name>PushCallback);
  90. // Format for release events: <object name>.attachPop(<object name>PopCallback);
  91.  
  92.  
  93. } // End of setup
  94. int v = 0;
  95. int incomingByte = 0;
  96. const int n11 = 0;
  97. void loop() // Put your main code here, to run repeatedly:
  98. {
  99. nexLoop(nex_listen_list); // Check for any touch event0
  100. uint32_t number1 = 0; // Create variable to store value of slider
  101. n1.getValue(&number1); // Read the value of the slider
  102.  
  103. if(v == 99) {
  104. v = 0;
  105. }
  106.  
  107. if(Serial.available() > 0) {
  108. // read the incoming byte:
  109. incomingByte = Serial.read();
  110. }
  111.  
  112. if(incomingByte == 0) {
  113. v++;
  114. } else {
  115. v = 98;
  116. v--;
  117. }
  118.  
  119. Serial.print("n0.val=");
  120. Serial.print(v);
  121. // n1.setValue(&number1);
  122. Serial.write(0xff);
  123. Serial.write(0xff);
  124. Serial.write(0xff);
  125. } // End of loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement