FourStepExampleCode.ino 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * FourStepExample.ino - Simple example code
  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. // Compatible for Arduino
  18. /* This is the most important method of the library.
  19. * And this is because, it gives you the ability to use the predefined functions and run your code from there.
  20. * These predefined functions are named trigger0(), trigger1(), trigger2()... up to trigger50().
  21. * You can use them as a simple void out of the loop, in which you will have written a block of code to run every time it is called.
  22. * You can call those trigger() functions and run the code they contain anytime by simply writing in a Nextion Event the command:
  23. * `printh 23 02 54 XX` , where `XX` the id for the triggerXX() in HEX.
  24. * Example: printh 23 02 54 00 to call trigger0() ... printh 23 02 54 0A to call trigger10() and so on...
  25. */
  26. /*
  27. Declare the void by simply writing:
  28. void trigger0(){
  29. [ put your code here !!!!]
  30. }
  31. */
  32. #include "EasyNextionLibrary.h" // Include EasyNextionLibrary
  33. EasyNex myNex(Serial); // Create an object of EasyNex class with the name < myNex >
  34. // Set as parameter the Hardware Serial you are going to use
  35. void setup(){
  36. myNex.begin(9600); // Begin the object with a baud rate of 9600
  37. // If no parameter was given in the begin(), the default baud rate of 9600 will be used
  38. pinMode(LED_BUILTIN, OUTPUT); // The built-in LED is initialized as an output
  39. digitalWrite(LED_BUILTIN, LOW); // Setting the built-in LED to LOW = off
  40. }
  41. void loop(){
  42. myNex.NextionListen(); // This function must be called repeatedly to response touch events
  43. // from Nextion touch panel. Actually, you should place it in your loop function.
  44. }
  45. void trigger0(){
  46. /* Create a button on Nextion
  47. * Write in the Touch Release Event of the button
  48. * this command: printh 23 02 54 00
  49. * Every time the button is pressed, the trigger0() function will run
  50. * and the code inside will be executed once
  51. */
  52. digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // If LED_BUILTIN is ON, turn it OFF, or the opposite
  53. if(digitalRead(LED_BUILTIN) == HIGH){
  54. myNex.writeNum("b0.bco", 2016); // Set button b0 background color to GREEN (color code: 2016)
  55. myNex.writeStr("b0.txt", "ON"); // Set button b0 text to "ON"
  56. }else if(digitalRead(LED_BUILTIN) == LOW){
  57. myNex.writeNum("b0.bco", 63488); // Set button b0 background color to RED (color code: 63488)
  58. myNex.writeStr("b0.txt", "OFF"); // Set button b0 text to "ON"
  59. }
  60. }