Relationship between classes with Arduino

I am working on a project, in which I try to use some methods of one class in another. That is, I have the class servomotor and the class Ultrasonido, and I use the void adelante method of the class servomotor in a method of the class Ultrasonido to make it move the servo certain degrees if the distance is less than 10. Unfortunately, I get an error in the compilation related to the fact that Servo0 has not been declared. Does anyone have any ideas on how to solve this beyond the implemented object?, for example not only being able to use that method for Servo0 but also for other declared servos . My code is as follows:

Code Servomotor class (Separately, it works perfectly well):

#include "Servo.h"
 class Servomotor:
        public Servo {     
        public: 
    Servomotor(){}
            void pin (uint8_t servoPin) {
                attach(servoPin);
            }
        void adelante(uint8_t GI, uint8_t GF) {
            for (int val=GI;val<=GF;val+= 1)
            {write(val); delay (30);}
            }
        void atras(uint8_t GI, uint8_t GF) {
            for (int val = GF; val >= GI; val -= 1) {
            write(val);
            delay(30);
      }
    }
};
Servomotor Servo0;
void setup()
{
Servo0.pin (8);
}
void loop()
{
Servo0.adelante(140,180);
 delay(100);
Servo0.atras(180,140); 
}

Code Ultrasonido Class (Separately, it works perfectly well):

#include <NewPing.h>
 class Ultrasonido: public NewPing
{
  public:
    using NewPing::NewPing;

      void encender(void) {
       digitalWrite(LED_BUILTIN, HIGH);
        delay(1000);

    }
    
};

Ultrasonido sensor1(5,4,150);

void setup() { 
  pinMode(LED_BUILTIN, OUTPUT);
   Serial.begin(9600);//iniciailzamos la comunicación

  }
void loop() 
{
 digitalWrite(LED_BUILTIN, LOW);
  delay(2);
 int distancia=sensor1.ping_cm();
 delay(30);
 Serial.print("Distancia: ");
 Serial.print(distancia);      //Enviamos serialmente el valor de la distancia
  if (distancia>0&&distancia<=10){
sensor1.encender();
}
}

Now, my code joining both classes to use the methods of one in the other:

#include <NewPing.h>
#include "Servo.h"

class Ultrasonido: public NewPing
{
  public:
    using NewPing::NewPing;

      void encender(void) {
       digitalWrite(LED_BUILTIN, HIGH);
        delay(1000);
        Servo0.adelante(140,180); // methods servomotor class in the ultrasonido class.
      delay(100);
        Servo0.atras(180,140); 
    }
    
};

 class Servomotor:
        public Servo {
        
        public:  
    Servomotor(){}
            void pin (uint8_t servoPin) {
                attach(servoPin);
            }

        void adelante(uint8_t GI, uint8_t GF) {
            for (int val=GI;val<=GF;val+= 1)
            {write(val); delay (30);}
            }
      
        void atras(uint8_t GI, uint8_t GF) {
            for (int val = GF; val >= GI; val -= 1) {
            write(val);
            delay(30);
      }
    }
  
};
 
 
// Sketch
Ultrasonido sensor1(5,4,150);
Servomotor Servo0;


void setup() { 
  pinMode(LED_BUILTIN, OUTPUT);
   Serial.begin(9600);//iniciailzamos la comunicación
Servo0.pin (8);
  }
void loop() 
{
 digitalWrite(LED_BUILTIN, LOW);
  delay(2);
 int distancia=sensor1.ping_cm();
 delay(30);
 Serial.print("Distancia: ");
 Serial.print(distancia);      //Enviamos serialmente el valor de la distancia
  if (distancia>0&&distancia<=10){
sensor1.encender();
}
}

You just have to put the declaration of Servo0 above the declaration of Ultrasonido. To do that you have to put the declaration of Servomotor above the declaration of Servo0.

1 Like

Thank you very much for your reply. It worked perfectly!!!. Now, if I also needed the encender method in the servo class, it would not let me compile. Is there any way to make the relationships between classes not depend on where the object is declared. I can think of an abstract object, but I really don’t have much experience with OOP.

You can create one object class that inherits from both Servo and NewPing. Then it can do anything that either can do.

#include <NewPing.h>
#include <Servo.h>

class MyClass: public NewPing, public Servo
{
  public:
    MyClass() : NewPing(5, 4, 150) {    };

    void encender(void)
    {
      digitalWrite(LED_BUILTIN, HIGH);
      delay(1000);
      adelante(140, 180); // methods servomotor class in the ultrasonido class.
      delay(100);
      atras(180, 140);
    }

  private:
  
    void adelante(uint8_t GI, uint8_t GF)
    {
      for (int val = GI; val <= GF; val += 1)
      {
        write(val);
        delay (30);
      }
    }

    void atras(uint8_t GI, uint8_t GF)
    {
      for (int val = GF; val >= GI; val -= 1)
      {
        write(val);
        delay(30);
      }
    }
};


// Sketch
MyClass mine;

void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);//iniciailzamos la comunicación
  mine.attach(8);
}

void loop()
{
  digitalWrite(LED_BUILTIN, LOW);
  delay(2);
  int distancia = mine.ping_cm();
  delay(30);
  Serial.print("Distancia: ");
  Serial.print(distancia);      //Enviamos serialmente el valor de la distancia
  if (distancia > 0 && distancia <= 10)
  {
    mine.encender();
  }
}

Another choice is to put the Servo stuff in the Servo object and the Ultrasonic stuff in the Ultrasonic object. It makes no sense to call a function in an Ultrasonic object to move a servo.

#include <NewPing.h>
#include <Servo.h>

class Ultrasonido: public NewPing
{
  public:
    using NewPing::NewPing;
};

class Servomotor : public Servo
{
  public:
    Servomotor() {}

    void encender(void)
    {
      digitalWrite(LED_BUILTIN, HIGH);
      delay(1000);
      adelante(140, 180); // methods servomotor class in the ultrasonido class.
      delay(100);
      atras(180, 140);
    }
    
  private:
    void adelante(uint8_t GI, uint8_t GF)
    {
      for (int val = GI; val <= GF; val += 1)
      {
        write(val);
        delay (30);
      }
    }

    void atras(uint8_t GI, uint8_t GF)
    {
      for (int val = GF; val >= GI; val -= 1)
      {
        write(val);
        delay(30);
      }
    }

};


// Sketch
Ultrasonido sensor1(5, 4, 150);
Servomotor Servo0;

void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);//iniciailzamos la comunicación
  Servo0.attach(8);
}

void loop()
{
  digitalWrite(LED_BUILTIN, LOW);
  delay(2);
  int distancia = sensor1.ping_cm();
  delay(30);
  Serial.print("Distancia: ");
  Serial.print(distancia);      //Enviamos serialmente el valor de la distancia
  if (distancia > 0 && distancia <= 10)
  {
    Servo0.encender();
  }
}

I have a question, if I need to create another ultrasonic sensor that uses other pins, can I do it?

Not with the current design.

Your design ties each ultrasonic sensor to one stepper. It should be possible to change the code to pass ultrasonic pin numbers to the constructor to allow for multiple Ultrasonic/Servo pairs.

Thank you very much for your reply. I will try to modify it to create several ultrasound objects.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.