Help with Header and cpp file for this code.

This is the main function.

#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);
}

This is my current header file.

#ifndef HCSR04_h
#define HCSR04_h

#include “Arduino.h”

#define CM 1
#define INC 0

class HCSR04ProxSensor
{
public:
distanceSensor(int echoPin, int triggerPin);
long Timing();
long Distance(int sys);

private:
int echoPin;
int triggerPin;
long duration,distacne_cm,distance_inc;
float readSensor();
float getLastValue();
};
#endif

and this is my cpp file

#include “Arduino.h”
#include “HCSR04.h”

HCSR04ProxSensor::distanceSensor(int echoPin, int triggerPin)
{
pinMode(triggerPin,OUTPUT);
pinMode(echoPin,INPUT);
//echoPin=EP;
//triggerPin=TP;
}

long HCSR04ProxSensor::Timing()
{
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
duration = pulseIn(echoPin,HIGH);
return duration;
}

long HCSR04ProxSensor::Distance(int sys)
{
Timing();
distacne_cm = duration /29 / 2 ;
distance_inc = duration / 74 / 2;
if (sys)
return distacne_cm;
else
return distance_inc;
}

I keep getting "no matching function to call to HCSR04ProxSensor :: HCSR04ProxSensor(int,int);

Any ideas ?

class HCSR04ProxSensor
{
 public:
   distanceSensor(int echoPin, int triggerPin);

Methods in a class, except the constructor and destructor, need to have return types. Constructors and destructors are distinguished by the fact that they have the same name as the class (the destructor has a ~ up front, too.).

Your distanceSensor() function is not a constructor, so it needs a return type (or to be renamed).

You don't have a constructor, so the compiler supplied you with one that takes no arguments. That is not the constructor you are trying to use, though.

When you get the constructor defined correctly, you'll still have a problem. You should not be diddling with the hardware, calling pinMode(), because your constructor is called before init() gets the hardware ready.

PaulS:

class HCSR04ProxSensor

{
public:
  distanceSensor(int echoPin, int triggerPin);



Methods in a class, except the constructor and destructor, need to have return types. Constructors and destructors are distinguished by the fact that they have the same name as the class (the destructor has a ~ up front, too.).

Your distanceSensor() function is not a constructor, so it needs a return type (or to be renamed).

You don't have a constructor, so the compiler supplied you with one that takes no arguments. That is not the constructor you are trying to use, though.

When you get the constructor defined correctly, you'll still have a problem. You should not be diddling with the hardware, calling pinMode(), because your constructor is called before init() gets the hardware ready.

PaulS:

class HCSR04ProxSensor

{
public:
  distanceSensor(int echoPin, int triggerPin);



Methods in a class, except the constructor and destructor, need to have return types. Constructors and destructors are distinguished by the fact that they have the same name as the class (the destructor has a ~ up front, too.).

Your distanceSensor() function is not a constructor, so it needs a return type (or to be renamed).

You don't have a constructor, so the compiler supplied you with one that takes no arguments. That is not the constructor you are trying to use, though.

When you get the constructor defined correctly, you'll still have a problem. You should not be diddling with the hardware, calling pinMode(), because your constructor is called before init() gets the hardware ready.

Im not sure what to do Paul. I've been stuck on this for a few hours.

I understand what you're saying but I do not know how to apply that.

Any suggestions on how to rewrite it?

Any suggestions on how to rewrite it?

Start with this:

class HCSR04ProxSensor
{
 public:
   HCSR04ProxSensor(int echoPin, int triggerPin);

Then:
HCSR04ProxSensor::HCSR04ProxSensordistanceSensor(int echoPin, int triggerPin)
{
pinMode(triggerPin,OUTPUT);
~~ pinMode(echoPin,INPUT);~~
// this->echoPin=EP echoPin;
// this->triggerPin=TP triggerPin;
}

Read the tutorial:
https://www.arduino.cc/en/Hacking/LibraryTutorial

Bout to KMS over this code guys. I need it finished by 3 pm.
I now my constructor is not correct.
I switched the name in my class and header files and still keep getting errors. Im not sure what else I can do.

Show your new code.
Show your new error messages.

And please use code tags when posting code and errors.

#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(int echoPin,int 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);
}

I have to utilize the above code and create a library for it.
I must develop a "class HCSR04ProxSensor(int echoPin,int triggerPin); //constructor
float readSensor();
float getLastValue();

this is what i have so far and the error i get is

"request for member readSensor in distanceSensor which is of non class type…

MY CPP FILE

#include “Arduino.h”
#include “lol”

HCSR04ProxSensor::distanceSensor(int EP, int TP)
{
pinMode(echoPin, INPUT);
pinMode(triggerPin,OUTPUT);
echoPin=EP;
triggerPin=TP;
}

{
HCSR04ProxSensor::readSensor
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin,LOW);
pulseIn(echoPin,HIGH);
HCSR04ProxSensor::distanceSensor(int readSensor)
readSensor=distacecm/58);
delay(500);
}

MY HEADER FILE

#ifndef lol_h
#define lol_h

#include “Arduino.h”

#define CM 1
#define INC 0

class HCSR04ProxSensor
{
public:
distanceSensor(int echoPin,int triggerPin);
// distanceSensor.readSensor;

private:
float readSensor;
float getLastValue;
int echoPin;
int triggerPin;
int distancecm;
};
#endif

PaulS told you exactly how to fix it and it wasn't that tough.

He said the constructor has to have the same name as the class. What's hard about that? Do you know what the name of the class is? Can't you just change the name of the constructor to match? You know how to use the keyboard on your computer obviously because you posted this. That's about all the skill you need to follow Paul's advice.

Then you need to create another method in the class that can be called from setup that has the pinMode stuff in it.

For Pete's sake he even wrote the constructor function out for you.

I don't see what you're having trouble with. What part is confusing you?

CODE TAGS!!!!! We don't like it when you just ignore posts. That's a good way to see all the help you're after just evaporate. Have you read "How to use this forum" yet? It's at the top of any of the boards on the forum. Why would you start using something without reading the how to use it first?

You also seem to be completely ignoring PaulS's advice to make the name of the constructor match the name of the class. Why should we waste time trying to help you if you aren't even going to pay attention to what anyone says?

… Because it is a homework assignment and I am not allowed to CHANGE the given code which is

  • if i change distanceSensor to HCSR04 I will get a lot of points taken off my grade.

I’m not no flying retard.

#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(int echoPin,int 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);
}

What part of PLEASE USE CODE TAGS don't you understand?

getswifty:
I'm not no flying retard.

gfvalvo:
What part of PLEASE USE CODE TAGS don't you understand?

getswifty:
… Because it is a homework assignment and I am not allowed to CHANGE the given code which is

  • if i change distanceSensor to HCSR04 I will get a lot of points taken off my grade.

You don’t have to change the code in the .ino. You just have to write good code for the class.

Do you have any understanding at all of classes in C++? That is something you should probably research a little. You don’t need a function called distanceSensor.

HCSR04ProxSensor distanceSensor(echoPin,triggerPin);

This is NOT calling a function called distanceSensor. It is creating an object called distanceSensor. It is trying to create that object by calling a two argument constructor but you are getting an error because your class doesn’t have a two argument constructor.

Im not familiar with classes nor libraries for Arduino. But appreciate the help. I turned in the assignment with what i had, i fixed what paul sugguested and was able to almost get what i needed besides the final part with

<<from the .cpp file>>>

I guess my readSensor value from the header file must be out of whack or i declared it wrong but idk. I don’t care anymore at this point lol i can’t spend 4 hours on a piece of code that he refuses to teach but instead makes us go home and watch 10 mins worth of theory on what the sensor is doing…

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”);}

getswifty:
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”);}

And still no code tags.

getswifty:
I'm not no flying retard.

Well at least he has an excuse for the code tags. He said it here himself. Note the double negative.

Maybe if he hadn't repeatedly thumbed his nose at our forum rules then someone might have had a little pity on him.

This is NOT calling a function called distanceSensor. It is creating an object called distanceSensor. It is trying to create that object by calling a two argument constructor but you are getting an error because your class doesn't have a two argument constructor.

Actually, it isn't. Calls to functions, constructors or otherwise, do not have types in the call.

What that code is doing is defining a function, distanceSensor(), that takes two arguments and returns a HCSR04ProxSensor.

PaulS:
Actually, it isn't. Calls to functions, constructors or otherwise, do not have types in the call.

What that code is doing is defining a function, distanceSensor(), that takes two arguments and returns a HCSR04ProxSensor.

Oops. He had it right in the OP. I read it there and copied from the other post.

There... I fixed it.