Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sensor READ fails on even number of reads #220

Open
sgreensides opened this issue Apr 12, 2024 · 2 comments
Open

Sensor READ fails on even number of reads #220

sgreensides opened this issue Apr 12, 2024 · 2 comments
Labels
triage identified and logged, but it hasn't yet been fully reviewed or assigned a priority

Comments

@sgreensides
Copy link

sgreensides commented Apr 12, 2024

CM4 with latest OS and WiringPi - clean install of both
Same circuit with python works every-time = so assumed to be correct
Current work-around is to check the size of return and call again if too low

C++ Code

  class HCSR04 {
  public:
    HCSR04 (const uint8_t pEchoPin, const uint8_t pTriggerPin) {
      this->_EchoPin = pEchoPin;
      this->_TriggerPin = pTriggerPin;
      pinMode(this->_TriggerPin, OUTPUT);
      pinMode(this->_EchoPin, INPUT);
      digitalWrite(this->_TriggerPin, LOW);
    }                                                           // end constructor

   /*
    * get the distance reading - RAW
    * and return it
    */
   uint64_t getDistance (uint64_t pTimeOut = 50000) {
     //
     // blip the trigger pin
     // first we set it LOW - so that we are starting from a known state
     // blip = set HIGH
     // set LOW to end
     //
     digitalWrite(this->_TriggerPin, LOW);
     delayMicroseconds(100);                                        // time for GPIO pin to settle ? needed ?
     // known state now

     // so blip
     digitalWrite(this->_TriggerPin, HIGH);
     delayMicroseconds(10);                                        // check whether we can reduce this!!
     digitalWrite(this->_TriggerPin, LOW);

     // wait for echo to return
     // pTimeOut is unsigned so should never be allowed to approach zero
     while ((digitalRead(this->_EchoPin) == LOW) && (pTimeOut-- > 2)) {
       // or fail on timeout
       ;
     };                                                             // end waiting for echo;

     // record this time
     this->_startTimeUsec = micros();

     // whilst we have an echo
     while ( digitalRead(this->_EchoPin) == HIGH ) {
       // wait for the ECHO to finish
       ;
     };                                                         // end of whilst echo echo exists

     // difference between now and when we first started
     return (micros() - this->_startTimeUsec);
   }

  /*****/
  private:
    uint8_t _EchoPin;
    uint8_t _TriggerPin;
    uint64_t _startTimeUsec;
};                                                              // end of class HCSR04
@sgreensides
Copy link
Author

Apologies if not pasted code correctly = not done this before :)

@mwallner mwallner added the triage identified and logged, but it hasn't yet been fully reviewed or assigned a priority label Apr 19, 2024
@mstroh76
Copy link
Member

mstroh76 commented May 8, 2024

This is your reference implementation (and WiringPi test case) :
https://github.com/GrazerComputerClub/Sensors-WiringPi/tree/master/HC-SR04
please check if its working and compare to your implementation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage identified and logged, but it hasn't yet been fully reviewed or assigned a priority
Projects
None yet
Development

No branches or pull requests

3 participants