ReadAndWriteNumberCode.ino 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * ReadAndWriteNumberCode.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 and WeMos D1 mini ESP8266
  18. #include "EasyNextionLibrary.h" // Include EasyNextionLibrary
  19. EasyNex myNex(Serial); // Create an object of EasyNex class with the name < myNex >
  20. // Set as parameter the Hardware Serial you are going to use
  21. uint32_t number = 0;
  22. uint32_t lastnumber = 0;
  23. #define LOOP_TIME 2000
  24. unsigned long timer ;
  25. void setup(){
  26. myNex.begin(9600); // Begin the object with a baud rate of 9600
  27. // If no parameter was given in the begin(), the default baud rate of 9600 will be used
  28. delay(500); // Wait for Nextion to start
  29. timer = millis();
  30. }
  31. void loop(){
  32. if((millis() - timer) > LOOP_TIME){
  33. number = myNex.readNumber("n0.val"); // We read the value of n0 and store it to number variable
  34. if(number != 777777){ // 777777 is the return value if the code fails to read the new value
  35. lastnumber = number; // The chances of getting a wrong value is one in a million.
  36. // Use this if() to ensure it if you believe it is needed.
  37. // You can either call the readNumber funtion again
  38. // or set a safe value in case of failure.
  39. // Ex: number = 2222; or use the last value method
  40. } else if(number == 777777){
  41. number = lastnumber;
  42. }
  43. myNex.writeNum("n1.val", number); // After that, we send the number variable, as value to n1
  44. number = myNex.readNumber("page0.bco"); // Read and store the background color code to number variable
  45. if(number == 33823){
  46. myNex.writeNum("page0.bco", 63488); // Change background color to RED(63488) if it was BLUE(33823)
  47. }else if(number == 63488){
  48. myNex.writeNum("page0.bco", 33823); // Change background color to BLUE(33823) if it was RED(63488)
  49. }
  50. // As these commands are using Serial to read and write,
  51. // it is more preferred not to run them in the loop() without delay();
  52. // or some other method of not running them with the frequency of the loop
  53. // and use them only when it is needed.
  54. // A delay in the loop can be noticed, especially when reading from Serial
  55. // And of course to avoid a Serial buffer overflow
  56. timer = millis();
  57. }
  58. }