Hello, how are you? I'm working on a project and I want to make a class that passes parameters to an ultrasound sensor, but I can't get my class to work. Does anyone have any idea why this is happening?
Thank you very much for answering, my problem is that I take as an example the servo class (attached below). But I can't get it to work for the ultrasound sensor. Perhaps, it is because echo, trigger and max_distance must be defined prior to the declaration of the object with Newping.
#include
class MyClass : public Servo {
public:
MyClass() {}
void servoBackward() {
for (int8_t val = 60; val >= 45; val -= 1) {
write(val);
delay (15);
}
}
Now, my idea is that through the class the programmer only has to say to which pin belongs trigger, echo and max_distance. But I can not pass arguments to the object without first declaring it with the class, so I get an error. Any idea how to solve this?
Why? What is this class you are creating adding to the value of the original ping class? It looks like you are still passing in the same three parameters. I don't understand what you are trying to do. Can you explain what your after a little better?
Sorry, I’m really bad with this classes and objects stuff . But I can’t figure out where the statements that pass arguments to echo, trigger and max_distance go. Excuse me, for not understanding, could you be a little more specific. I did something like this:
#include “NewPing.h”
// Shortened NewPing class
class NewPing {
public:
NewPing(uint8_t trigger_pin, uint8_t echo_pin, int max_cm_distance);
};
Thank you very much Jim for replying, it's basically what you say, pass the same three parameters to it. My goal with the new class is: is that if I want to create another ultrasound sensor I don't have to rewrite:
Are you under the impression that you need to write your own class to use the NewPing.h stuff? Because that is NOT the case.
#include <NewPing.h>
#define TRIGGER_PIN 1
#define ECHO_PIN 2
#define MAX_DISTANCE 1000
NewPing sensor1(TRIGGER_PIN,ECHO_PIN,MAX_DISTANCE); // And there you go. You have sensor1 ready to go.
int distancia=0;
void setup()
{
}
void loop()
{
}
Ohhhhh, thank you so much!!!. Now, the truth is that I do need that class for the sensor. For example, I want besides being able to pass the triger, echo and max_distance, I want to create a function inside the class that will turn on a led for 10 seconds and turn off. Something like this:
#include <NewPing.h>
// Get used to writing classes in two parts.
// PART ONE the class decleration. (Ends up later in life being the .h file)
class Ultrasonido : public NewPing {
public:
Ultrasonido(uint8_t trigger_pin, uint8_t echo_pin, int max_cm_distance=MAX_SENSOR_DISTANCE);
virtual ~Ultrasonido(void);
void encender(void);
};
// PART TWO the actual code. (Ends up later in life being the .cpp file)
Ultrasonido::Ultrasonido (uint8_t trigger_pin, uint8_t echo_pin, int max_cm_distance) { }
: NewPing(trigger_pin,echo_pin,max_cm_distance) { }
Ultrasonido::~Ultrasonido(void) { }
void Ultrasonido::encender(void) {
// Do youre LED or whatever here..
}
Ultrasonido sensor1(1,2,100);
Ultrasonido sensor2(4,5,100);
int distancia=0;
void setup() { }
void loop() {
distancia=sensor1.ping_cm();
if (distancia<10){
sensor1.encender();
}
}
Thank you very much for your reply, I am trying to compile the code but I can't get it to work. I am not even calling the methods. Do you have any idea what could be the error?