Building objects/classes?

I've made up two classes - AirPump and TempSensor and testet each of them and they work as supposed to, but ...

IRL an airpump has an integrated tempsensor !

How do I 'integrate' the TempSensor-class into the AirPump-class ?

A link to an example would be really nice :grinning:

It sounds like you need a single class to deal with the AirPump and that it should have a function or functions to deal with the temperature sensor but without more details, who knows ?

Composition. Add a temperature sensor object into your air pump class.

Where are they? We cannot see the code from here.

Which model of air pump from which manufacturer?

Other than to tell you to add an object as an attribute, there's not much else to say without proper information.

Does the AirPump class need to measure the temperature to work? If so, it should have a TempSensor object as a member variable.

If the AirPump class doesn't need to know the temperature but you want an object to control the AirPump at higher level based on values including temperature, create a third class (AirPumpController?) that contains or inherits behavior from AirPump and TemperatureSensor.

It all depends on how the TemperatureSensor effects the control of the AirPump.

IRL the pump and the tempsensor are two seperate things - the tempsensor is in fact a bus with sensors each accessable by an index.

I found out the hard way, that the pump gets too warm (about 46°C) on a hot day and then it can't pump as it should - therefore I will use the tempsensor to shut the pump off when its temperatur is about 40°C and then start another pump while the first cools off ... and visa versa :crazy_face:

I can separate the tempsensor by index and I've written on each of them which index the have.

I think my problem is the overall 'syntax' for making a TempSensor a member/attribute/property/variable of the Pump-class ... I can't find any sample showing how it's done.

#include <TempSensor.h>

class AirPump
{
    TempSensor ts;
};

Sounds like it should be a higher-level object that controls which pumps are pumping based on their temperature. If every pump acts as a TempSensor so the higher level control can check their temperature, the AirPump might better inherit the behavior of a TempSensor:

#include <TempSensor.h>

class AirPump : public TempSensor
{
};

This way the higher-level code doesn't need to know which TempSensor goes with each AirPump. It can just ask the AirPump for the temperature as if it were a TempSensor

johnwasser
THX a lot ... you just nailed the problem - now it's up to my skills to get it to work :thinking:

Please somebody ... a link to a simple sample code on this issue - my skills ain't enough :blush:

I can't even explain what I can't get ... I think again it has to do with the syntax - where to put the TempSensor construntor in which file ... the '.h' and/or the '.ccp' and how to 'link' back to the inherited class :worried:

Again, if you want better help you need to post your code.and tell more about the pump you're using.

There is NOTHING more irrelevat then what pump I'm using for solving my syntax problems - right ?

There is nothing more relevant to solving problems than information, but if you want to work in darkness and have people guess at what help you need that's cool too.

Post the smallest possible COMPLETE MRE that demonstrates your problem.

Post the actual and COMPLETE compile-time error messages, not your paraphrased version of what you think they say.

@ksor
1. As I have understood from your description, this is the (Fig-1) probable hardware setup of your two-pump/two-sensor project; where, the pumps will be running alternately due to over temperature trip and subsequent cooling.

pumpControl
Figure-1:

2. You want to operate your pumps using object oriented codes where methods are called upon to carry out intended actions on the objects. Have you written few lines of codes? If yes! Please, post them; the forum members will be glad to help you to adjust/correct your codes.

3. If you have not written any codes at all, then follow the hints given by @johnwasser (in his various posts of this thread) to create a sketch and then post it.

You must declare the constructor in the class declaration.
You would typically declare the class in a .h file so it can be used in more than one place. The declaration tells everyone the constructor's argument list.

You can define it (give it a body to execute when called) either in the declaration or in a separate .cpp file. Usually you would put it the declaration only if it is quite small and in a separate .cpp file if it is larger, like more than half a page.

Your schematic is as close as it could be except for some pin numbers !

Here is my files ALL included in the project:

TempSensor.h

#ifndef TempSensor_h
#define TempSensor_h
#include <Arduino.h>

class TempSensor {

  private:
    byte        _pin = 0;     // pin number for the Tempsensor BUS
    byte        _index = 0;   // reference to the specific Tempsensorer for the Pump it's attached to
    float       _tempLimit;   // max temperature for starting the pump

  public:
    TempSensor(byte pn, byte idx, float tl);

    // Methods
    byte      getPin();
    byte      getIndex();
    float     getTempValue();
    float     getTempLimit();
    void      setTempLimit(float tl);
    
};
#endif

TempSensor.cpp

#include "TempSensor.h"

#include <OneWire.h>
#include <DallasTemperature.h>

TempSensor::TempSensor(byte pn, byte idx, float tl) {
  _pin = pn;
  _index = idx;
  _tempLimit = tl;
}

// Methods

float readTemperature(byte pn, byte idx) {
  // Setup a oneWire instance to communicate with any OneWire device
  OneWire oneWire(pn);  
  // Pass oneWire reference to DallasTemperature library
  DallasTemperature sensors(&oneWire);
  sensors.begin();  // Start up the Temperatur Sensor library
  sensors.requestTemperatures();
  return sensors.getTempCByIndex(idx);
}

byte TempSensor::getPin() {
  return _pin;
}

byte TempSensor::getIndex() {
  return _index;
}

float TempSensor::getTempValue() {
  return readTemperature(getPin(), getIndex());
}

float     TempSensor::getTempLimit() {
  return _tempLimit;
}

void      TempSensor::setTempLimit(float tl) {
  _tempLimit = tl;
}

AirPump.h

#ifndef AirPump_h
#define AirPump_h

#include "TempSensor.h"
#include <Arduino.h>

enum enumState {stopped, tooWarm, pumping};

class AirPump : public TempSensor {
  
  private:
    byte          _pin = 0;           // the pin controlling the relay for the pump
    enumState     _state = stopped;
    unsigned long _lastStarted = 0;   // millis() for the last start - 0 if state = stopped/tooWarm

  public:
    AirPump(byte pin, enumState state, unsigned long lastSt);
    
    // Methods
    byte          getPin();
    enumState     getEnmState();
    void          setEnmState(enumState st);
    unsigned long getLastStarted();
    void          setLastStarted(unsigned long ls);
    bool          startPump();        // return true if OK or false if too warm to start
    bool          stopPump();
};
#endif

AirPump.cpp

#include "AirPump.h"

AirPump::AirPump(byte pin, enumState state, unsigned long lastSt) {
  _pin = pin;
  _state = state;
  _lastStarted = lastSt;
  pinMode(_pin, OUTPUT);
}

byte AirPump::getPin() {
  return _pin;  
}

enumState AirPump::getEnmState() {
  return _state;
}

void AirPump::setEnmState(enumState st) {
  _state = st;
}

unsigned long AirPump::getLastStarted() {
  return _lastStarted;
}

void AirPump::setLastStarted(unsigned long ls) {
  _lastStarted = ls;
}

bool AirPump::startPump() {           // return true if OK or false if too warm to start
  if (getEnmState() == stopped) {
    digitalWrite(getPin(), LOW);        
    setLastStarted(millis());
    setEnmState(pumping);
    return true; } 
   else if (getEnmState() == tooWarm) {
      setLastStarted(0);
      return false; }
   else { 
    return true;  // already running !
   }
}
  
bool AirPump::stopPump() {
    digitalWrite(getPin(), HIGH);        
    setLastStarted(0);
    setEnmState(stopped);
    return true;  
}

MAIN project - just for checking classes and so:

#include <Arduino.h>
#include "AirPump.h"
//#include "TempSensor.h"

#define tempSensorBUSPin2 2
#define relayPin3 3
#define relayPin4 4

byte activePump = 0;    // the pump currently running - 0 if no pump is running
float maxTemp = 29;     // max temp for starting any pump        

AirPump Pumpe3(relayPin3,stopped, 0);                            // pin-number for the relay controlling the pump, actuel state, mS last started
//AirPump TempSensor::TempSensor(tempSensorBUSPin2, 0, maxTemp);   // pin-number for connected TempSensor BUS, BUS-index for TempSensor to this pump, max temperature

AirPump Pumpe4(relayPin4,stopped, 0); 
//AirPump TempSensor::TempSensor(tempSensorBUSPin2, 1, maxTemp);

void turnOffBuildInLED() {
  pinMode(LED_BUILTIN,OUTPUT);
  digitalWrite(LED_BUILTIN,LOW);  
}

int pickRandom(unsigned long minIcl, unsigned long maxXcl ) {
  // returnerer et tilfældigt tal i 
  // interval fra og MED minIcl TIL (ikke 'med') maxXcl 
  randomSeed(analogRead(A0));     // Sikrer en tilfældigt startværdi
  return random(minIcl, maxXcl);
}

int tagEnTilfeldig() {
  int rn = pickRandom(3,5);    // Vælger tilfældigt 3 eller 4 (ikke 5 !!!!)
  return rn;
}  

bool startDenValgte(int st) {
  if (st == 3) {
    return (Pumpe3.startPump() && Pumpe4.stopPump());
  } else {
    return (Pumpe4.startPump() && Pumpe3.stopPump());
  }
}

void setup() {
  Serial.begin(9600);
  turnOffBuildInLED();
  Pumpe3.stopPump(); Pumpe4.stopPump();
  byte p = tagEnTilfeldig();
  if (startDenValgte(p)) { activePump = p; }
  // Serial.print("Pump started: "); Serial.println(p);
}

void loop() {

// Serial.print("Målt temperatur: "); Serial.println(Temp4.getTempValue());
  
}

Now DON'T kick me I'm new in this business and in this forum :face_with_thermometer:

Don't do this, an air pump is not a temperature sensor, so it shouldn't inherit from it.
A pump might contain a temperature sensor, but that doesn't mean that it is a temperature sensor.

????????
I simply DON'T get you !

Why the strong reaction? I just pointed out the Composition over Inheritance principle which leads to better code.

Is there any good reason to choose DPin-0 to control the pump relay when DPin-0 (Fig-1 of Post-14) indicates that the said pin remains engaged with Serial Monitor/IDE?