Distanz-/Tiefenmesser mit JSN-SR40T
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TreePositionSwitch.cpp 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * TreePositionSwitch.cpp
  3. *
  4. * Created on: 21.12.2025
  5. * Author: FSmilari
  6. */
  7. #include "TreePositionSwitch.h"
  8. /*****************
  9. ** Constructors.
  10. ****************/
  11. TreePositionSwitch::TreePositionSwitch(int Pos1Pin, int Pos2Pin, int Pos3Pin) : Pos1_Pin(Pos1Pin), Pos2_Pin(Pos2Pin), Pos3_Pin(Pos3Pin), actualPosition(-1) {
  12. lastDebounceTime = 0;
  13. pinMode(Pos1_Pin, INPUT_PULLUP);
  14. pinMode(Pos2_Pin, INPUT_PULLUP);
  15. pinMode(Pos3_Pin, INPUT_PULLUP);
  16. }
  17. void TreePositionSwitch::loop(void) {
  18. int currentReadPosition = -1;
  19. if (digitalRead(Pos1_Pin) == LOW) {
  20. currentReadPosition = 1;
  21. }
  22. else if (digitalRead(Pos2_Pin) == LOW) {
  23. currentReadPosition = 2;
  24. }
  25. else if (digitalRead(Pos3_Pin) == LOW) {
  26. currentReadPosition = 3;
  27. }
  28. // Änderung erkannt?
  29. if (currentReadPosition != lastReadPosition) {
  30. lastDebounceTime = millis();
  31. lastReadPosition = currentReadPosition;
  32. }
  33. // Zustand stabil genug?
  34. if ((millis() - lastDebounceTime) >= debounceTime) {
  35. actualPosition = lastReadPosition;
  36. }
  37. }
  38. int TreePositionSwitch::getPosition(void) {
  39. return actualPosition;
  40. }
  41. void TreePositionSwitch::setDebounceTime(unsigned long time) {
  42. lastDebounceTime = time;
  43. }