Distanz-/Tiefenmesser mit JSN-SR40T
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

EchoLot.ino 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include "Arduino.h"
  2. #include "ExEzButton.h"
  3. #include "Status.h"
  4. #include "RotaryControler.h"
  5. #include "EchoLotSetup.h"
  6. #include "ThreePositionSwitch.h"
  7. static const int trigPin = PB11;
  8. static const int echoPin = PB10;
  9. static const int Pos3Sw_Pin1 = PB13;
  10. static const int Pos3Sw_Pin2 = PB14;
  11. static const int Pos3Sw_Pin3 = PB15;
  12. static const int Measure_Pin = PB6;
  13. static const int RotEnc_Clk_Pin = PB7;
  14. static const int RotEnc_Dta_Pin = PB8;
  15. static const int RotEnc_Switch_Pin = PB9;
  16. Status actualStatus;
  17. Status originStatus;
  18. String oldStatus = "";
  19. String lastDistance = "";
  20. RotaryControler RotaryControler(RotEnc_Dta_Pin, RotEnc_Clk_Pin, RotEnc_Switch_Pin);
  21. ExEzButton MeasureBtn(Measure_Pin, false, 2000);
  22. ThreePositionSwitch ThreePositionSwitch(Pos3Sw_Pin1, Pos3Sw_Pin2, Pos3Sw_Pin3);
  23. Display Display;
  24. EchoLotSetup EchoLotSetup(Display);
  25. //*****************************************************************************************************
  26. //*** Initialization routine. Reads the eeprom first and sets the (potentially new) configured values.
  27. //*****************************************************************************************************
  28. void Initialize() {
  29. Display.showInitialization();
  30. EchoLotSetup.initialize();
  31. delay(50);
  32. SetActualStatus(IDLE);
  33. }
  34. void setup() {
  35. Serial.begin(115200);
  36. delay(500);
  37. pinMode(RotEnc_Switch_Pin, INPUT);
  38. pinMode(RotEnc_Dta_Pin, INPUT);
  39. pinMode(RotEnc_Clk_Pin, INPUT);
  40. pinMode(trigPin, OUTPUT);
  41. pinMode(echoPin, INPUT);
  42. pinMode(Measure_Pin, INPUT);
  43. MeasureBtn.setDebounceTime(50);
  44. RotaryControler.setDebounceTime(50);
  45. ThreePositionSwitch.setDebounceTime(50);
  46. Display.init();
  47. SetActualStatus(INITIALIZATION);
  48. Serial.println("Setup end");
  49. delay(50);
  50. }
  51. void loop() {
  52. MeasureBtn.loop();
  53. RotaryControler.loop();
  54. switch (actualStatus) {
  55. case INITIALIZATION:
  56. Initialize();
  57. SetActualStatus(IDLE);
  58. break;
  59. case CONFIGURATION:
  60. break;
  61. case IDLE: {
  62. if (RotaryControler.isSwitchLongPressed()) {
  63. Serial.println("RotaryEnc Long SwitchPressed");
  64. RotaryControler.resetPosition();
  65. }
  66. if (RotaryControler.isSwitchPressed()) {
  67. Serial.println("RotaryEnc SwitchPressed");
  68. } else if (MeasureBtn.isPressing()) {
  69. SetActualStatus(MEASURING);
  70. }
  71. ThreePositionSwitch.loop();
  72. int pin = ThreePositionSwitch.getPosition();
  73. // Serial.print("3-Pos-Schalter Position: ");
  74. // Serial.println(pin);
  75. delay(200);
  76. break;
  77. }
  78. case MEASURING:
  79. if (MeasureBtn.isPressing()) {
  80. digitalWrite(trigPin, LOW);
  81. delayMicroseconds(5);
  82. digitalWrite(trigPin, HIGH);
  83. delayMicroseconds(20);
  84. digitalWrite(trigPin, LOW);
  85. long duration = pulseIn(echoPin, HIGH);
  86. long distance = duration * 0.03432 / 2;
  87. Serial.print("Distance:");
  88. Serial.println(distance);
  89. // char buf[50];
  90. // sprintf(buf, "%lu cm", distance);
  91. //
  92. // if (!lastDistance.equals(buf)) {
  93. // tft.fillRect(0, 20, 130, 50, ST7735_BLACK);
  94. // tft.setCursor(0, 50); // Set position (x,y)
  95. // tft.setFont(&titillium_web_regular20pt7b);
  96. // tft.setTextSize(0); // Set text size. Goes from 0 (the smallest) to 20 (very big)
  97. // tft.setTextColor(ST7735_CYAN);
  98. // tft.println(buf);
  99. // lastDistance = buf;
  100. // }
  101. delay(200);
  102. } else {
  103. // tft.fillScreen(ST7735_BLACK);
  104. SetActualStatus(IDLE);
  105. }
  106. break;
  107. default:
  108. break;
  109. }
  110. }
  111. void SetActualStatus(Status newStatus) {
  112. if (actualStatus != newStatus) {
  113. actualStatus = newStatus;
  114. // Display.setRefreshScreen();
  115. }
  116. }