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?
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
@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.
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.