Skip to Content
Hands-On Robotics Programming with C++
book

Hands-On Robotics Programming with C++

by Dinesh Tavasalkar
March 2019
Intermediate to advanced
312 pages
7h 37m
English
Packt Publishing
Content preview from Hands-On Robotics Programming with C++

The HC-SR04 sensor program

After wiring up the HC-SR04 sensor to the Raspberry Pi, let's write a program for measuring the distance between an object from an ultrasonic sensor. The distance measurement program is called DistanceMeasurement.cpp and you can download it from the Chapter04 folder of the GitHub repository.

The code for measuring the distance is as follows:

#include <stdio.h>#include <iostream>#include <wiringPi.h>using namespace std;#define trigger 12#define echo 13long startTime;long stopTime;int main(){ wiringPiSetup(); pinMode(trigger,OUTPUT); pinMode(echo, INPUT); for(;;){ digitalWrite(trigger,LOW); delay(500); digitalWrite(trigger,HIGH); delayMicroseconds(10); digitalWrite(trigger,LOW);  while(digitalRead(echo) == LOW); ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learn Robotics Programming

Learn Robotics Programming

Danny Staple
C++ Reactive Programming

C++ Reactive Programming

Praseed Pai, Peter Abraham

Publisher Resources

ISBN: 9781789139006Supplemental Content