Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- +5V = 5V
- TX = pin 0 (RX)
- RX = pin 1 (TX)
- GND = GND
- #include <Nextion.h>
- // Include the nextion library (the official one) https://github.com/itead/ITEADLIB_Arduino_Nextion
- // Make sure you edit the NexConfig.h file on the library folder to set the correct serial port for the display.
- // By default it's set to Serial1, which most arduino boards don't have.
- // Change "#define nexSerial Serial1" to "#define nexSerial Serial" if you are using arduino uno, nano, etc.
- int variable1 = 0; // Create a variable to have a counter going up by one on each cycle
- int counter = 0; // Create a variable to have a counter for the + and - buttons
- int CurrentPage = 0; // Create a variable to store which page is currently loaded
- // Declare objects that we are going to read from the display. This includes buttons, sliders, text boxes, etc:
- // Format: <type of object> <object name> = <type of object>(<page id>, <object id>, "<object name>");
- /* ***** Types of objects:
- NexButton - Button
- NexDSButton - Dual-state Button
- NexHotspot - Hotspot, that is like an invisible button
- NexCheckbox - Checkbox
- NexRadio - "Radio" checkbox, that it's exactly like the checkbox but with a rounded shape
- NexSlider - Slider
- NexGauge - Gauge
- NexProgressBar - Progress Bar
- NexText - Text box
- NexScrolltext - Scroll text box
- NexNumber - Number box
- NexVariable - Variable inside the nextion display
- NexPage - Page touch event
- NexGpio - To use the Expansion Board add-on for Enhanced Nextion displays
- NexRtc - To use the real time clock for Enhanced Nextion displays
- * *****
- */
- NexText t0 = NexText(0, 1, "t0"); // Text box added, so we can read it
- NexText t1 = NexText(0, 2, "t1"); // Text box added, so we can read it
- NexNumber n0 = NexNumber(0, 3, "n0");
- NexNumber n1 = NexNumber(0, 4, "n1");
- // Declare pages:
- // Sending data to the display to nonexistent objects on the current page creates an error code sent by the display.
- // Any error sent by the display creates lag on the arduino loop because arduino tries to read it, thinking it's a touch event.
- // So to avoid this, I am only going to send data depending on the page the display is on.
- // That's the reason I want the arduino to know which page is loaded on the display.
- // To let arduino know what page is currently loaded, we are creating a touch event for each page.
- // On the nextion project, each page most send a simulated "Touch Press Event" in the "Preinitialize Event" section so
- // we can register that a new page was loaded.
- NexPage page0 = NexPage(0, 0, "page0"); // Page added as a touch event
- // End of declaring objects
- char buffer[100] = {0}; // This is needed only if you are going to receive a text from the display. You can remove it otherwise.
- // Further on this sketch I do receive text so that's why I created this buffer.
- // Declare touch event objects to the touch event list:
- // You just need to add the names of the objects that send a touch event.
- // Format: &<object name>,
- NexTouch *nex_listen_list[] = {
- &page0, // Page added as a touch event
- &t0,
- &t1,
- &n0,
- &n1,
- NULL // String terminated
- }; // End of touch event list
- void setup()
- {
- nexInit();// Put your setup code here, to run once:
- Serial.begin(9600); // Start serial comunication at baud=9600
- pinMode(13, OUTPUT);
- // I am going to change the Serial baud to a faster rate.
- // The reason is that the slider have a glitch when we try to read it's value.
- // One way to solve it was to increase the speed of the serial port.
- delay(500); // This dalay is just in case the nextion display didn't start yet, to be sure it will receive the following command.
- Serial.print("baud=115200"); // Set new baud rate of nextion to 115200, but it's temporal. Next time nextion is power on,
- // it will retore to default baud of 9600.
- // To take effect, make sure to reboot the arduino (reseting arduino is not enough).
- // If you want to change the default baud, send the command as "bauds=115200", instead of "baud=115200".
- // If you change the default baud, everytime the nextion is power ON is going to have that baud rate, and
- // would not be necessery to set the baud on the setup anymore.
- Serial.write(0xff); // We always have to send this three lines after each command sent to nextion.
- Serial.write(0xff);
- Serial.write(0xff);
- Serial.end(); // End the serial comunication of baud=9600
- pinMode(13, OUTPUT);
- Serial.begin(115200); // Start serial comunication at baud=115200
- // Register the event callback functions of each touch event:
- // You need to register press events and release events seperatly.
- // Format for press events: <object name>.attachPush(<object name>PushCallback);
- // Format for release events: <object name>.attachPop(<object name>PopCallback);
- } // End of setup
- int v = 0;
- int incomingByte = 0;
- const int n11 = 0;
- void loop() // Put your main code here, to run repeatedly:
- {
- nexLoop(nex_listen_list); // Check for any touch event0
- uint32_t number1 = 0; // Create variable to store value of slider
- n1.getValue(&number1); // Read the value of the slider
- if(v == 99) {
- v = 0;
- }
- if(Serial.available() > 0) {
- // read the incoming byte:
- incomingByte = Serial.read();
- }
- if(incomingByte == 0) {
- v++;
- } else {
- v = 98;
- v--;
- }
- Serial.print("n0.val=");
- Serial.print(v);
- // n1.setValue(&number1);
- Serial.write(0xff);
- Serial.write(0xff);
- Serial.write(0xff);
- } // End of loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement