Passing object as param

Need to create a RelayDevice that has a ButtonDebounce inside it as variable
HEADER

class RelayDevice {
public:
    RelayDevice(int p, ButtonDebounce *toggle_switch);
    ButtonDebounce* getSwitch();

private:
    int _p;
    ButtonDebounce *_toggle_switch;
};

CPP file

RelayDevice::RelayDevice(int p, ButtonDebounce *toggle_switch) {
    _p = p;
    pinMode(p, INPUT);
    digitalWrite(p, LOW);
    _toggle_switch = toggle_switch;
}
ButtonDebounce* RelayDevice::getSwitch() {
    return _toggle_switch;
}

HEADER

class ButtonDebounce {
public:

    ButtonDebounce(char p, bool inv=true);
    ButtonDebounce() {};

    /// inspect pin
    void scan();

    /// return current pin state
    bool getCurrentState() const;

    /// return true ONCE for each keydown
    bool hadPress();

private:
    char pin;
    bool state, event, invert;
    unsigned long changedat;

    // 20 ms must elapse since keyup before a new keydown will register
    static const unsigned long TIMEOUT=20000;
};

CPP file

ButtonDebounce::ButtonDebounce(char p, bool inv) : pin(p), invert(inv)
{
    pinMode(pin, INPUT);
    digitalWrite(pin, 1);
    state=false;
    event=false;
    changedat=0;
}

void ButtonDebounce::scan()
{
    bool newstate=digitalRead(pin) ^ invert;

    if(newstate != state){
        unsigned long now=micros();

        // changed after long-enough time
        if(now - changedat > TIMEOUT){
            changedat=now;
            state=newstate;
            if(state)
                event=true;
        }
    }
}

bool ButtonDebounce::getCurrentState() const
{
    Serial.print("\nMY pin is:'");
    Serial.print(pin);
    Serial.print("'");
    return state;
}

bool ButtonDebounce::hadPress()
{
    bool res=event;
    event=false;
    return res;
}

But when I instanciate it like this and try to access getCurrentState

ButtonDebounce button1(2, true);
RelayDevice relay(5, &button1);
relay.getSwitch()->getCurrentState();

prints nothing → what am I doing wrong?

If those are ALL of your header files, and the code is ACTUALLY in header and source files, I'm surprised the code compiles at all. In fact, I'm sure it doesn't.

Create a zip file of your project, with all 5 files (both headers, both source, and the sketch). Attach it to your post, using the Additional Options link below the text entry area.

thanks, didnt know you could attach files here.

Anyways, I’ve done some C++ research and come up with this.

@RelayDevice.cpp

RelayDevice::RelayDevice(int p, RFCode code, int b, bool inv) : pin(p), _toggle_switch(b, inv) {

Just wanted to know how to proceed correctly. Need one instance of ButtonDebounce and one of RFCode inside RelayDevice.
@recv_on_dig11_rev7_standalone.ino

/** Dont construct things inside constructors */
RelayDevice relays[RELAY_COUNT] = 
			{RelayDevice(5, RFCode('A', 'a', 'B', 'b'), 2, true), 
			 RelayDevice(6, RFCode('C', 'c', 'D', 'd'), 3, true)/*,
			 RelayDevice(7, ButtonDebounce(12, true), RFCode('E', 'e', 'F', 'f')),
			 RelayDevice(8, ButtonDebounce(13, true), RFCode('G', 'g', 'F', 'f'))*/};

Constructor.7z (2.52 KB)

I'm not going to guess what an RFCode is.

Do you Serial.begin?

Sorry. Here

RFCode.h (411 Bytes)

RFCode.cpp (447 Bytes)