Skip to content

Adapting read-write example to distance sensor #33

@AnasGhrab

Description

@AnasGhrab

I'm trying to adapt the read-write example to a distance sensor (HC-SR04), sending a Trigger and getting the Echo result. I'd be glad if I get help with this not working code :

#include "ofMain.h"
#include "ofAppNoWindow.h"
#include "ofxGPIO.h"

class noWin : public ofBaseApp{
        public:
		GPIO* gpio17;
                 GPIO* gpio39;
                 GPIO* gpio40;

		string state_button, echo;

   void setup(){
      gpio17  = new GPIO("17");
      gpio39  = new GPIO("39"); // Trigger (out)
      gpio40  = new GPIO("40"); // Echo (in)

      gpio17->export_gpio();
      gpio39->export_gpio();
      gpio40->export_gpio();

      gpio17->setdir_gpio("in"); // State
      gpio39->setdir_gpio("out"); // Trigger
      gpio40->setdir_gpio("in"); // Echo

		}

void update(){
      if(state_button == "1"){
          gpio39->setval_gpio("1");
          usleep(10);
          gpio40->getval_gpio(echo);
          ofLog()<< echo;
          }
      if(state_button == "0"){
          gpio39->setval_gpio("0");
          usleep(5000);
          }
       }

   void exit(){
      gpio17->unexport_gpio();
      gpio39->unexport_gpio();
      gpio40->unexport_gpio();
     }
};

int main( ){
  ofAppNoWindow window;
	ofSetupOpenGL(&window, 0,0, OF_WINDOW);
	ofRunApp( new noWin() );
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions