I need to create a cpp file and a header file that work with the given code. Nee
ID: 2249012 • Letter: I
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++ - 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);
}
Explanation / Answer
Code For HCSR04.h
--------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------Code for CPP file
#ifndef Ultrasonic_h #define Ultrasonic_h #if ARDUINO >= 100 #include "Arduino.h" #else #include "WProgram.h" #endif #define CM 1 #define INC 0 class Ultrasonic { public: Ultrasonic(int TP, int EP); Ultrasonic(int TP, int EP, long TO); long Timing(); long Ranging(int sys); private: int Trig_pin; int Echo_pin; long Time_out; long duration,distance_cm,distance_inc; }; #endifRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.