Question
I need to create a cpp file and a header file that work with the given code. Need super help. Coding is in C++ -
I need to create a cpp file and a header file that work with the given code. Need super help. Coding is in C++ - We are trying to program an arduino to read out the proximity sensor HCSR04.
#include "HCSR04.h"//include your proximity sensor library
#define echoPin 8 // This is the echo pin #define triggerPin 9 // This is the trigger pin
HCSR04ProxSensor distanceSensor(echoPin,triggerPin);//here we call the constructor to instantiate a sensor named "distanceSensor"
/***************************setup function****************************************************/
void setup() { Serial.begin(9600);//start serial communication }
/***************************main loop*********************************************************/
void loop() { Serial.print("The distance is : "); float distance = distanceSensor.readSensor();//here we call the 'readSensor' method to determine the distance Serial.print(distance);// send the measurement to the serial monitor Serial.println(" cm"); if (distanceSensor.getLastValue() - distance > 1) {Serial.println("object is approaching");}//here we call the 'getLastValue' method to determine the direction of motion if (distanceSensor.getLastValue() - distance < -1) {Serial.println("object is retracting");}
delay(500); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started