Hello, I need some help assembling a library with a .h file and .cpp that can measure the distance in cm and tell if an object is getting closer with a proximity sensor hooked up to my Arduino. For my class, we are given a sketch to make a .h and .cpp with and we have to make it work. Pasted below is the .ino code I have to make a library for:
#include "HCSR04.h"//include your proximity sensor library
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#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);
}
Help is greatly appreciated as I'm a total noob with Arduino and C++ coding. Thanks!