TriggerCode.ino 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * TriggerCode.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. // 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);
  40. }
  41. void loop(){
  42. myNex.NextionListen(); // WARNING: 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. // To call this void send from Nextion's component's Event: printh 23 02 54 00
  47. // In this exmaple, we send this command from the Release Event of b0 button (see the HMI of this example)
  48. // You can send the same `printh` command, to call the same function, from more than one component, depending on your needs
  49. digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // If LED_BUILTIN is ON, turn it OFF, or the opposite
  50. if(digitalRead(LED_BUILTIN) == HIGH){
  51. myNex.writeNum("b0.bco", 2016); // Set button b0 background color to GREEN (color code: 2016)
  52. myNex.writeStr("b0.txt", "ON"); // Set button b0 text to "ON"
  53. myNex.writeNum("p0.pic", 1); // Set picture 1 as background picture for p0
  54. }else if(digitalRead(LED_BUILTIN) == LOW){
  55. myNex.writeNum("b0.bco", 63488); // Set button b0 background color to RED (color code: 63488)
  56. myNex.writeStr("b0.txt", "OFF"); // Set button b0 text to "OFF"
  57. myNex.writeNum("p0.pic", 0); // Set picture 0 as background picture for p0 picture component
  58. }
  59. }
  60. void trigger1(){
  61. // To call this void send from Nextion's component's Event: printh 23 02 54 01
  62. // In this exmaple, we send this command from the Release Event of b1 button (see the HMI of this example)
  63. // You can send the same `printh` command, to call the same function, from more than one component, depending on your needs
  64. if(digitalRead(LED_BUILTIN) == HIGH){
  65. digitalWrite(LED_BUILTIN, LOW); // Start the function with the LED off
  66. myNex.writeNum("b0.bco", 63488); // Set button b0 background color to RED (color code: 63488)
  67. myNex.writeStr("b0.txt", "OFF"); // Set button b0 text to "OFF"
  68. myNex.writeNum("p0.pic", 0); // Set picture 0 as background picture for p0 picture component
  69. }
  70. myNex.writeStr("t0.txt", "LED STROBE\\rON");
  71. for(int i = 0; i < 10; i++){
  72. for(int x = 0; x < 10; x++){
  73. digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // If LED_BUILTIN is ON, turn it OFF, or the opposite
  74. delay(50);
  75. }
  76. digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // If LED_BUILTIN is ON, turn it OFF, or the opposite
  77. delay(500);
  78. digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // If LED_BUILTIN is ON, turn it OFF, or the opposite
  79. }
  80. myNex.writeStr("t0.txt", "LED STROBE\\rOFF"); // Setting the text of t0 textbox to "LED STROBE OFF"
  81. // \\r is the newline character for Nextion.
  82. // The text will look like this:
  83. // 1st line: LED STROBE
  84. // 2nd line: OFF
  85. }