How to take data from structure about another structure nametag/handler

Hi,
i'm trying to implement structure in my code. I'm using few GPIO expanders and i want use struct to "translate" what relay i want to active using only ordinary number (like it's done using standard digitalWrite).

This is version of code that works in 100%, but I still have one information hand-written and not taken from structure i created.

#include "PCF8574.h"
PCF8574 pcf8574_0(0x20);

struct relays {
  PCF8574 expander;
  int pin;

}  relay[] = {  {pcf8574_0, P0}  }; // in reality it have over 30 members, that's why i want to use an array


int cleanWaterIn = 0; // I'm saving ordinary number of relay, so later i can use 'human name' instead of remembering all numbers

void relayWrite( int ordinaryNumber, bool newState){
pcf8574_0.digitalWrite(relay[ordinaryNumber].pin, newState); // here i'm using hand-written info about expander instead off taking it from struct
}

void setup() {
  Wire.begin();
  Serial.begin(9600);

  pcf8574_0.pinMode(P0, OUTPUT);
  if (pcf8574_0.begin()) Serial.println("OK");
  else Serial.println("FAILED!");
}

void loop() {
 relayWrite( cleanWaterIn, HIGH );
 delay(300);
 relayWrite( cleanWaterIn, LOW );
 delay(300);
}

and when I try to change relayWrite() to take information from
relay[].expander it stops working. I don't have any warnings and code compiles, but pin state on PCF8574 module never changes.

void relayWrite( int ordinaryNumber, bool newState){
relay[ordinaryNumber].expander.digitalWrite(relay[ordinaryNumber].pin, newState);
}

I'm pretty sure i'm missing some symbol correlated to pointers, I tried so many configurations but i didn't had any luck. Any ideas?

Thanks in advance.

look this over

#define MyHW
#ifdef MyHW
# include "sim.hh"

#else
#include "PCF8574.h"
#endif

PCF8574 pcf8574_0 (0x20);

struct Relay {
    PCF8574 expander;
    int     pin;
}

// in reality it have over 30 members, that's why i want to use an array
relay [] = {
    { pcf8574_0, LED_BUILTIN}
};
const int Nrelay = sizeof(relay) / sizeof(Relay);

// -----------------------------------------------------------------------------
void
relayWrite (
    int  idx,
    bool state )
{
    digitalWrite (relay [idx].pin, state);
}

// -----------------------------------------------------------------------------
void loop () {
    relayWrite (0, HIGH );
    delay      (300);
    relayWrite (0, LOW );
    delay      (300);
}

void
setup () {
    Wire.begin ();
    Serial.begin (9600);

    for (int n = 0; n < Nrelay; n++) {
        pinMode (relay [n].pin, OUTPUT);
        relay [n].expander.begin ();
    }

    if (pcf8574_0.begin ())
        Serial.println ("OK");
    else
        Serial.println ("FAILED!");
}

I can't run it yet.

What Arduino board are you aiming this at?

Whose PCF8574 library are you using? Can you post a link to it?

THX

a7

1 Like

Welcome

struct Relay {
    PCF8574 expander;

This should be a reference to an existing object, instead of creating a new object

Like this:

struct Relay {
    PCF8574 & expander;
1 Like

Thank you, it was it.

I wasted whole evening trying to do it, im pretty sure i also tried it in some point.. Again, thank u very much!

1 Like

Or alternatively, keep the old struct as it was, and initialize the object directly in the array

Like this:

struct relays {
  PCF8574 expander;
  int pin;

}  relay[] = {  { { 0x20 }, P0 } };
1 Like

Problem was solved by another user, thanks anyway


U didn't change anything in struct part, pcf8574 wasn't able to begin.

digitalWrite (relay [idx].pin, state);

Here u just removed problem, that's why led was blinking correctly and why u was thinked problem was solved. I was able to get int variable (pin) whole time, problem was laying in taking tag/handler (in my example pcf8574_0). Again, thanks anyway for your time

Ok, thanks

Problem was already solved, but i'll answer anyway because maybe someone may need this in future.


I made mistake during copying and changing code on website to look cleaner, sorry. This part:

void relayWrite( int ordinaryNumber, bool newState){
pcf8574_0.expander.digitalWrite(relay[ordinaryNumber].pin, newState); 
}

should look like this:


pcf8574_0.digitalWrite(relay[ordinaryNumber].pin, newState); 

@alto777 thanks for pointing it out. I'm gonna change oryginal code to not confuse someone who will read in future.

I'm was UNO clone during these tests, but it also works on ESP32. I'm using the most popular Adafruit lib, it can be found in IDE and idk how to make link to it.

Didn't. Thank @guix .

I still can't compile it. If you meant you edited the code in the first post. Which you should maybe not have done, as it makes nonsense of the thread.

In any case, could you post the last code in a new post, and say again which library. A link would be unambiguous. I tried an Adafruit library I turned up and it coulkdn't

#include "PCF8574.h"

it may well be my brain has had too much sun just now. :expressionless:

a7

why does expander have a digitalWrite methtod

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