Distanz-/Tiefenmesser mit JSN-SR40T
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

EchoLot.ino 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "C:/Users/FSmilari/Documents/Arduino/libraries/Adafruit_GFX_Library/Adafruit_GFX.h" // Include core graphics library
  2. #include "D:/Programme/sloeber/4.4/arduinoPlugin/libraries/Adafruit_ST7735_and_ST7789_Library/1.11.0/Adafruit_ST7735.h" // Include Adafruit_ST7735 library to drive the display
  3. #include "D:/Programme/sloeber/4.4/arduinoPlugin/packages/arduino/hardware/avr/1.8.4/cores/arduino/Arduino.h"
  4. #include "D:/Programme/sloeber/4.4/arduinoPlugin/packages/arduino/hardware/avr/1.8.4/cores/arduino/HardwareSerial.h"
  5. #include "D:/Programme/sloeber/4.4/arduinoPlugin/packages/arduino/hardware/avr/1.8.4/cores/arduino/WString.h"
  6. #include "D:/Programme/sloeber/4.4/arduinoPlugin/packages/arduino/hardware/avr/1.8.4/variants/standard/pins_arduino.h"
  7. #include "ExEzButton.h"
  8. #include "Status.h"
  9. #include "fonts/titillium_web_regular20pt7b.h" // Add a custom font
  10. #include "images/SFToolsLogo.h"
  11. #include "images/thermometer_32.h"
  12. #include "RotaryControler.h"
  13. static const int RotEnc_Switch_Pin = 3;
  14. static const int RotEnc_Clk_Pin = 4;
  15. static const int RotEnc_Dta_Pin = 5;
  16. static const int Measure_Pin = 6;
  17. static const int echoPin = A5;
  18. static const int trigPin = A4;
  19. #define LED -1 // to +5v
  20. #define SCK 13//A1 with SCK on 13 and SDA on 11 it is 10x faster!!!
  21. #define SDA 11//A2
  22. #define MISO -1
  23. #define MOSI SDA
  24. #define CS 10
  25. #define DC 9
  26. #define RST 8
  27. Status actualStatus;
  28. Status originStatus;
  29. String oldStatus = "";
  30. String lastDistance = "";
  31. // Create display:
  32. Adafruit_ST7735 tft = Adafruit_ST7735(CS, DC, RST);
  33. RotaryControler RotaryControler(RotEnc_Dta_Pin, RotEnc_Clk_Pin, RotEnc_Switch_Pin);
  34. ExEzButton MeasureBtn(Measure_Pin, false, 2000);
  35. void setup() {
  36. SetActualStatus(INITIALIZATION);
  37. Serial.begin(115200);
  38. pinMode(trigPin, OUTPUT);
  39. pinMode(echoPin, INPUT);
  40. pinMode(Measure_Pin, INPUT);
  41. pinMode(RotEnc_Switch_Pin, INPUT);
  42. MeasureBtn.setDebounceTime(50);
  43. RotaryControler.setDebounceTime(50);
  44. // Display setup:
  45. // Use this initializer if you're using a 1.8" TFT
  46. tft.initR(INITR_BLACKTAB); // Initialize a ST7735S chip, black tab
  47. tft.fillScreen(ST7735_BLACK); // Fill screen with black
  48. tft.setRotation(2); // Set orientation of the display. Values are from 0 to 3. If not declared, orientation would be 0,
  49. // which is portrait mode.
  50. tft.setTextWrap(false); // By default, long lines of text are set to automatically “wrap� back to the leftmost column.
  51. // To override this behavior (so text will run off the right side of the display - useful for
  52. // scrolling marquee effects), use setTextWrap(false). The normal wrapping behavior is restored
  53. // with setTextWrap(true).
  54. tft.setCursor(0, 20); // Set position (x,y)
  55. tft.setFont(&titillium_web_regular20pt7b);
  56. tft.setTextSize(0); // Set text size. Goes from 0 (the smallest) to 20 (very big)
  57. tft.setTextColor(ST7735_WHITE);
  58. tft.println("Hello");
  59. tft.drawBitmap(0, 80, SFTools_Logo, 128, 34, ST7735_GREEN);
  60. tft.drawBitmap(1, 160 - 32, thermometer_icon_32, 32, 32, 0xE71C);
  61. tft.drawBitmap(33, 160 - 32, thermometer_icon_32, 32, 32, ST7735_YELLOW);
  62. tft.drawRect(48, 137, 2, 6, ST7735_YELLOW);
  63. tft.drawBitmap(65, 160 - 32, thermometer_icon_32, 32, 32, ST7735_RED);
  64. tft.drawRect(80, 133, 2, 10, ST7735_RED);
  65. delay(3000);
  66. tft.fillScreen(ST7735_BLACK); // Fill screen with black
  67. }
  68. void loop() {
  69. MeasureBtn.loop();
  70. RotaryControler.loop();
  71. switch (actualStatus) {
  72. case INITIALIZATION:
  73. SetActualStatus(IDLE);
  74. break;
  75. case CONFIGURATION:
  76. break;
  77. case IDLE:
  78. if (RotaryControler.isSwitchPressed()) {
  79. Serial.println("RotaryEnc SwitchPressed");
  80. }
  81. if (MeasureBtn.isPressing()) {
  82. digitalWrite(trigPin, LOW);
  83. delayMicroseconds(5);
  84. digitalWrite(trigPin, HIGH);
  85. delayMicroseconds(20);
  86. digitalWrite(trigPin, LOW);
  87. long duration = pulseIn(echoPin, HIGH);
  88. long distance = duration * 0.03432 / 2;
  89. Serial.print("Distance:");
  90. Serial.println(distance);
  91. char buf[50];
  92. sprintf(buf, "%lu cm", distance);
  93. if (!lastDistance.equals(buf)) {
  94. tft.fillRect(0, 20, 130, 50, ST7735_BLACK);
  95. tft.setCursor(0, 50); // Set position (x,y)
  96. tft.setFont(&titillium_web_regular20pt7b);
  97. tft.setTextSize(0); // Set text size. Goes from 0 (the smallest) to 20 (very big)
  98. tft.setTextColor(ST7735_CYAN);
  99. tft.println(buf);
  100. lastDistance = buf;
  101. }
  102. delay(200);
  103. // } else {
  104. // mylcd.Fill_Screen(BLACK);
  105. }
  106. break;
  107. case MEASURING:
  108. break;
  109. default:
  110. break;
  111. }
  112. }
  113. void SetActualStatus(Status newStatus) {
  114. if (actualStatus != newStatus) {
  115. actualStatus = newStatus;
  116. // Display.setRefreshScreen();
  117. }
  118. }