Advertisement
safwan092

Untitled

Nov 22nd, 2023
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. #include <Nextion.h>
  2.  
  3. #define trigPin A0
  4. #define echoPin A1
  5.  
  6. String d1_str, d2_str, diff_str;
  7. float d1, d2, diff;
  8. long duration;
  9. int distance;
  10.  
  11. //Declare your Nextion objects , pageid, component id., component name
  12. NexButton b0 = NexButton(0, 2, "b0");
  13. NexButton b1 = NexButton(0, 5, "b1");
  14. NexButton b2 = NexButton(0, 13, "b2");
  15. NexText t3 = NexText(0, 7, "t3");
  16. NexText t4 = NexText(0, 8, "t4");
  17. NexText t5 = NexText(0, 9, "t5");
  18.  
  19. //Register a button object to the touch event list
  20.  
  21. NexTouch *nex_listen_list[] = {
  22. &b0,
  23. &b1,
  24. &b2,
  25. NULL
  26. };
  27.  
  28. void b0PopCallback(void *ptr) {
  29. d1 = read_Ultrasonic_Sensor();
  30. d1_str = String(d1);
  31. const char* d1CharArray = d1_str.c_str();
  32. t3.setText (d1CharArray);
  33. //t3.setText ("0.02");
  34. }
  35. void b1PopCallback(void *ptr) {
  36. d1 = d1;
  37. d2 = read_Ultrasonic_Sensor();
  38. diff = abs(d1 - d2);
  39. d2_str = String(d2);
  40. diff_str = String(diff);
  41. const char* d2CharArray = d2_str.c_str();
  42. const char* diffCharArray = diff_str.c_str();
  43. t4.setText (d2CharArray);
  44. t5.setText (diffCharArray);
  45. }
  46. void b2PopCallback(void *ptr) {
  47. d1 = 0;
  48. d2 = 0;
  49. diff = 0;
  50. t3.setText ("0.00");
  51. t4.setText ("0.00");
  52. t5.setText ("0.00");
  53. }
  54. void setup(void) {
  55. Serial.begin(9600);
  56. init_Sensors();
  57. nexInit();
  58. //Register the pop event callback function of the components
  59. b0.attachPop(b0PopCallback, &b0);
  60. b1.attachPop(b1PopCallback, &b1);
  61. b2.attachPop(b2PopCallback, &b2);
  62.  
  63. }
  64.  
  65. void loop() {
  66. nexLoop(nex_listen_list);
  67. }
  68.  
  69.  
  70.  
  71. void init_Sensors() {
  72. pinMode(trigPin, OUTPUT);
  73. pinMode(echoPin, INPUT);
  74. }
  75.  
  76. float read_Ultrasonic_Sensor() {
  77. digitalWrite(trigPin, LOW);
  78. delayMicroseconds(2);
  79. digitalWrite(trigPin, HIGH);
  80. delayMicroseconds(10);
  81. digitalWrite(trigPin, LOW);
  82. duration = pulseIn(echoPin, HIGH);
  83. distance = duration * 0.034 / 2;
  84. //Serial.print("Distance: ");
  85. //Serial.println(distance);
  86. return distance;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement