Distanz-/Tiefenmesser mit JSN-SR40T
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include "Adafruit_GFX.h" // Include core graphics library
  2. #include "Adafruit_ST7735.h" // Include Adafruit_ST7735 library to drive the display
  3. #include "Arduino.h"
  4. #include "HardwareSerial.h"
  5. #include "ExEzButton.h"
  6. #include "Status.h"
  7. #include "RotaryControler.h"
  8. #include "EchoLotSetup.h"
  9. #include "ThreePositionSwitch.h"
  10. #include "fonts/titillium_web_regular16pt7b.h" // Add a custom font
  11. #include "fonts/titillium_web_regular12pt7b.h" // Add a custom font
  12. #include "fonts/titillium_web_regular8pt7b.h" // Add a custom font
  13. #include "images/SFToolsLogo.h"
  14. #include "images/thermometer_32.h"
  15. #include "images/gear.h"
  16. #include "images/zzz.h"
  17. #include "images/ruler.h"
  18. #include "images/air.h"
  19. #include "images/water.h"
  20. #include "images/water_s.h"
  21. static const int trigPin = A0;
  22. static const int echoPin = A1;
  23. static const int RotEnc_Switch_Pin = A2;
  24. static const int RotEnc_Clk_Pin = A3;
  25. static const int RotEnc_Dta_Pin = A4;
  26. static const int Pos3Sw_Pin1 = 3;
  27. static const int Pos3Sw_Pin2 = 4;
  28. static const int Pos3Sw_Pin3 = 5;
  29. static const int Measure_Pin = 7;
  30. #define LED -1 // to +5v
  31. #define SCK 13//A1 with SCK on 13 and SDA on 11 it is 10x faster!!!
  32. #define SDA 11//A2
  33. #define MISO -1
  34. #define MOSI SDA
  35. #define CS 10
  36. #define DC 9
  37. #define RST 8
  38. Status actualStatus;
  39. Status originStatus;
  40. String oldStatus = "";
  41. String lastDistance = "";
  42. // Create display:
  43. Adafruit_ST7735 tft = Adafruit_ST7735(CS, DC, RST);
  44. RotaryControler RotaryControler(RotEnc_Dta_Pin, RotEnc_Clk_Pin, RotEnc_Switch_Pin);
  45. ExEzButton MeasureBtn(Measure_Pin, false, 2000);
  46. ThreePositionSwitch ThreePositionSwitch(Pos3Sw_Pin1, Pos3Sw_Pin2, Pos3Sw_Pin3);
  47. //Display Display;
  48. void setup() {
  49. SetActualStatus(INITIALIZATION);
  50. // Display.init();
  51. Serial.begin(115200);
  52. pinMode(trigPin, OUTPUT);
  53. pinMode(echoPin, INPUT);
  54. pinMode(RotEnc_Switch_Pin, INPUT);
  55. pinMode(RotEnc_Dta_Pin, INPUT);
  56. pinMode(RotEnc_Clk_Pin, INPUT);
  57. pinMode(Measure_Pin, INPUT);
  58. MeasureBtn.setDebounceTime(50);
  59. RotaryControler.setDebounceTime(50);
  60. ThreePositionSwitch.setDebounceTime(50);
  61. // Display setup:
  62. // Use this initializer if you're using a 1.8" TFT
  63. tft.initR(INITR_BLACKTAB); // Initialize a ST7735S chip, black tab
  64. tft.fillScreen(ST7735_BLACK); // Fill screen with black
  65. tft.setRotation(1); // Set orientation of the display. Values are from 0 to 3. If not declared, orientation would be 0,
  66. // // which is portrait mode.
  67. //
  68. // tft.setTextWrap(false); // By default, long lines of text are set to automatically “wrap� back to the leftmost column.
  69. // // To override this behavior (so text will run off the right side of the display - useful for
  70. // // scrolling marquee effects), use setTextWrap(false). The normal wrapping behavior is restored
  71. // // with setTextWrap(true).
  72. //
  73. tft.setCursor(0, 20); // Set position (x,y)
  74. tft.setFont(&titillium_web_regular16pt7b);
  75. tft.setTextSize(1); // Set text size. Goes from 0 (the smallest) to 20 (very big)
  76. tft.setTextColor(ST7735_WHITE);
  77. tft.println("AaBbCc012");
  78. tft.setCursor(0, 60); // Set position (x,y)
  79. tft.setFont(&titillium_web_regular12pt7b);
  80. tft.setTextSize(1); // Set text size. Goes from 0 (the smallest) to 20 (very big)
  81. tft.setTextColor(ST7735_WHITE);
  82. tft.println("AaBbCc0129");
  83. tft.setCursor(0, 80); // Set position (x,y)
  84. tft.setFont(&titillium_web_regular8pt7b);
  85. tft.setTextSize(1); // Set text size. Goes from 0 (the smallest) to 20 (very big)
  86. tft.setTextColor(ST7735_WHITE);
  87. tft.println("AaBbCc0129");
  88. tft.drawBitmap(5, 100, epd_bitmap_gear, 24, 24, ST7735_YELLOW);
  89. tft.drawBitmap(40, 100, epd_bitmap_Zzz_24, 24, 24, ST7735_CYAN);
  90. tft.drawBitmap(75, 100, epd_bitmap_Measure_25, 25, 25, ST7735_RED);
  91. tft.fillTriangle(75, 98, 80, 102, 75, 107, ST7735_RED);
  92. tft.fillTriangle(82, 98, 87, 102, 82, 107, ST7735_RED);
  93. tft.fillTriangle(89, 98, 94, 102, 89, 107, ST7735_RED);
  94. tft.fillTriangle(96, 98, 101, 102, 96, 107, ST7735_RED);
  95. // tft.drawBitmap(110, 100, epd_bitmap_air, 24, 24, ST7735_CYAN);
  96. tft.drawBitmap(110, 100, epd_bitmap_water_s, 24, 24, ST7735_BLUE);
  97. //
  98. // tft.drawBitmap(0, 80, SFTools_Logo, 128, 34, ST7735_GREEN);
  99. //
  100. // tft.drawBitmap(1, 160 - 32, thermometer_icon_32, 32, 32, 0xE71C);
  101. //
  102. // tft.drawBitmap(33, 160 - 32, thermometer_icon_32, 32, 32, ST7735_YELLOW);
  103. // tft.drawRect(48, 137, 2, 6, ST7735_YELLOW);
  104. //
  105. // tft.drawBitmap(110, 96, thermometer_icon_32, 32, 32, ST7735_RED);
  106. // tft.drawRect(125, 101, 2, 10, ST7735_RED);
  107. //
  108. // delay(3000);
  109. //
  110. // tft.fillScreen(ST7735_BLACK); // Fill screen with black
  111. // Display.display();
  112. }
  113. void loop() {
  114. MeasureBtn.loop();
  115. RotaryControler.loop();
  116. switch (actualStatus) {
  117. case INITIALIZATION:
  118. SetActualStatus(IDLE);
  119. break;
  120. case CONFIGURATION:
  121. break;
  122. case IDLE:
  123. if (RotaryControler.isSwitchLongPressed()) {
  124. Serial.println("RotaryEnc Long SwitchPressed");
  125. RotaryControler.resetPosition();
  126. }
  127. if (RotaryControler.isSwitchPressed()) {
  128. Serial.println("RotaryEnc SwitchPressed");
  129. } else if (MeasureBtn.isPressing()) {
  130. SetActualStatus(MEASURING);
  131. }
  132. ThreePositionSwitch.loop();
  133. int pin = ThreePositionSwitch.getPosition();
  134. Serial.print("3-Pos-Schalter Position: ");
  135. Serial.println(pin);
  136. delay(200);
  137. break;
  138. case MEASURING:
  139. if (MeasureBtn.isPressing()) {
  140. digitalWrite(trigPin, LOW);
  141. delayMicroseconds(5);
  142. digitalWrite(trigPin, HIGH);
  143. delayMicroseconds(20);
  144. digitalWrite(trigPin, LOW);
  145. long duration = pulseIn(echoPin, HIGH);
  146. long distance = duration * 0.03432 / 2;
  147. Serial.print("Distance:");
  148. Serial.println(distance);
  149. char buf[50];
  150. sprintf(buf, "%lu cm", distance);
  151. if (!lastDistance.equals(buf)) {
  152. // tft.fillRect(0, 20, 130, 50, ST7735_BLACK);
  153. // tft.setCursor(0, 50); // Set position (x,y)
  154. // tft.setFont(&titillium_web_regular20pt7b);
  155. // tft.setTextSize(0); // Set text size. Goes from 0 (the smallest) to 20 (very big)
  156. // tft.setTextColor(ST7735_CYAN);
  157. // tft.println(buf);
  158. lastDistance = buf;
  159. }
  160. delay(200);
  161. } else {
  162. // tft.fillScreen(ST7735_BLACK);
  163. SetActualStatus(IDLE);
  164. }
  165. break;
  166. default:
  167. break;
  168. }
  169. }
  170. void SetActualStatus(Status newStatus) {
  171. if (actualStatus != newStatus) {
  172. actualStatus = newStatus;
  173. // Display.setRefreshScreen();
  174. }
  175. }