PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

74 lines
1.6KB

  1. /*
  2. Ping.cpp - Library for using Ping))) Sensors with Arduino - Version 2
  3. Copyright (c) 2009 Caleb Zulawski. All right reserved.
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. */
  16. #include "Arduino.h"
  17. #include "Ping.h"
  18. Ping::Ping(int pin)
  19. {
  20. pinMode(pin, OUTPUT);
  21. _pin = pin;
  22. _in = 0;
  23. _cm = 0;
  24. _duration = -1;
  25. }
  26. Ping::Ping(int pin, double in, double cm)
  27. {
  28. pinMode(pin, OUTPUT);
  29. _pin = pin;
  30. _in = in;
  31. _cm = cm;
  32. _duration = -1;
  33. }
  34. void Ping::fire()
  35. {
  36. pinMode(_pin, OUTPUT);
  37. digitalWrite(_pin, LOW);
  38. delayMicroseconds(2);
  39. digitalWrite(_pin, HIGH);
  40. delayMicroseconds(5);
  41. digitalWrite(_pin, LOW);
  42. pinMode(_pin, INPUT);
  43. _duration = pulseIn(_pin, HIGH);
  44. }
  45. int Ping::microseconds()
  46. {
  47. return _duration;
  48. }
  49. double Ping::inches()
  50. {
  51. if(_duration != -1){
  52. return _duration / (74+_in) / 2;
  53. }else{
  54. return -1;
  55. }
  56. }
  57. double Ping::centimeters()
  58. {
  59. if(_duration != -1){
  60. return _duration / (29+_cm) / 2;
  61. }else{
  62. return -1;
  63. }
  64. }