PCF8575 issue, what am i doing wrong?

have been struggling to get the inputs working on a PCF8575.
it now sort of works but reading the input pins can be erratic giving false positives.
the input pins get pulled up to almost 5v and drop to 0v when i push the button, can't find any shorts
if i run it without interupt but just in the loop all the leds light up dim/flicker fast until i add a delay of about 50ms.
outputs seem to work great, had one of them constantly running a blink without delay and that showed no issues.

almost a total beginner and i did alot of searching and troubleshooting but finally gave up and decided to ask for help and i do hate asking for it :neutral_face:
hope i posted this in the right category.

using this library

got this board i have put solder blobs on address pads to gnd configuring it to 0x20 and soldered he vcc-vdd jumper

#include <PCF8575.h>
#define NOT_SEQUENTIAL_PINOUT
#define INTERRUPTED_PIN 3

void keyPressedOnPCF8575();
PCF8575 pcf8575(0x20, INTERRUPTED_PIN, keyPressedOnPCF8575);

//powerhandeling
const int powerSwitch = 2;

// defining output pins for LEDs

const int kLinks(pcf8575, P8);
const int sLinks(pcf8575, P9);
const int sRechts(pcf8575, P10);
const int kRechts(pcf8575, P11);
const int remL(pcf8575, P12);
const int mistL(pcf8575, P13);
const int aUit(pcf8575, P14);


// defining button pins

const int kLinksSwitch(pcf8575, P0);
const int sLinksSwitch(pcf8575, P1);
const int sRechtsSwitch(pcf8575, P2);
const int kRechtsSwitch(pcf8575, P3);
const int remLSwitch(pcf8575, P4);
const int mistLSwitch(pcf8575, P5);
const int aUitSwitch(pcf8575, P6);

// defining button state

int kLinksSwitchState;
int sLinksSwitchState;
int sRechtsSwitchState;
int kRechtsSwitchState;
int remLSwitchState;
int mistLSwitchState;
int aUitSwitchState;

//defining previous button state

int oldkLinksSwitchState = LOW;
int oldsLinksSwitchState = LOW;
int oldsRechtsSwitchState = LOW;
int oldkRechtsSwitchState = LOW;
int oldremLSwitchState = LOW;
int oldmistLSwitchState = LOW;
int oldaUitSwitchState = LOW;


// definging outputstates states

int kLinksState = HIGH;
int sLinksState = HIGH;
int sRechtstate = HIGH;
int kRechtstate = HIGH;
int remLState = HIGH;
int mistLState = HIGH;
int aUitState = HIGH;


//defining housekeeping states
int powerState = HIGH;
int ledState = LOW;

unsigned long ledMillis = 0;
unsigned long powerMillis = 0;

const long ledinterval = 1000;
const long powerinterval = 20000;

unsigned long debounceDelay;


void setup() {

  Serial.begin(115200);

  pinMode(powerSwitch, OUTPUT);
  digitalWrite(powerSwitch, HIGH);  // Keeping power latching circuit ON

  //IO expander pinmodes
  pcf8575.pinMode(P0, INPUT);
  pcf8575.pinMode(P1, INPUT);
  pcf8575.pinMode(P2, INPUT);
  pcf8575.pinMode(P3, INPUT);
  pcf8575.pinMode(P4, INPUT);
  pcf8575.pinMode(P5, INPUT);
  pcf8575.pinMode(P6, INPUT);

  pcf8575.pinMode(P8, OUTPUT);  //P10
  pcf8575.pinMode(P9, OUTPUT);
  pcf8575.pinMode(P10, OUTPUT);
  pcf8575.pinMode(P11, OUTPUT);
  pcf8575.pinMode(P12, OUTPUT);
  pcf8575.pinMode(P13, OUTPUT);
  pcf8575.pinMode(P14, OUTPUT);

  pcf8575.begin();
}

bool keyPressed = false;

void loop() {

  // bluetooth code goes here.

  // rtc + powerhandeling goes here

  // Mosfet handeling code goes here




  if (keyPressed) {  //interupt routine
    Serial.println("Interupting");
    // pcf8575.readBuffer();
    if (millis() - debounceDelay > 2000) {
      int kLinksSwitchState = pcf8575.digitalRead(kLinksSwitch);
      int sLinksSwitchState = pcf8575.digitalRead(sLinksSwitch);
      int sRechtsSwitchState = pcf8575.digitalRead(sRechtsSwitch);
      int kRechtsSwitchState = pcf8575.digitalRead(kRechtsSwitch);
      int remLSwitchState = pcf8575.digitalRead(remLSwitch);
      int mistLSwitchState = pcf8575.digitalRead(mistLSwitch);
      int aUitSwitchState = pcf8575.digitalRead(aUitSwitch);


      if (kLinksSwitchState == LOW && oldkLinksSwitchState == HIGH) {
        kLinksState = !kLinksState;
        pcf8575.digitalWrite(kLinks, kLinksState);
      }
      oldkLinksSwitchState = kLinksSwitchState;


      if (sLinksSwitchState == LOW && oldsLinksSwitchState == HIGH) {
        sLinksState = !sLinksState;
        pcf8575.digitalWrite(sLinks, sLinksState);
      }
      oldsLinksSwitchState = sLinksSwitchState;


      if (sRechtsSwitchState == LOW && oldsRechtsSwitchState == HIGH) {
        sRechtstate = !sRechtstate;
        pcf8575.digitalWrite(sRechts, sRechtstate);
      }
      oldsRechtsSwitchState = sRechtsSwitchState;


      if (kRechtsSwitchState == LOW && oldkRechtsSwitchState == HIGH) {
        kRechtstate = !kRechtstate;
        pcf8575.digitalWrite(kRechts, kRechtstate);
      }
      oldkRechtsSwitchState = kRechtsSwitchState;


      if (remLSwitchState == LOW && oldremLSwitchState == HIGH) {
        remLState = !remLState;
        pcf8575.digitalWrite(remL, remLState);
      }
      oldremLSwitchState = remLSwitchState;


      if (remLSwitchState == LOW && oldremLSwitchState == HIGH) {
        remLState = !remLState;
        pcf8575.digitalWrite(remL, remLState);
      }
      oldremLSwitchState = remLSwitchState;


      if (mistLSwitchState == LOW && oldmistLSwitchState == HIGH) {
        mistLState = !mistLState;
        pcf8575.digitalWrite(mistL, mistLState);
      }
      oldmistLSwitchState = mistLSwitchState;


      if (aUitSwitchState == LOW && oldaUitSwitchState == HIGH) {
        aUitState = !aUitState;
        pcf8575.digitalWrite(aUit, aUitState);
      }
      oldaUitSwitchState = aUitSwitchState;


      keyPressed = false;
    }
  }
}

void keyPressedOnPCF8575() {
  // Interrupt
  keyPressed = true;
}

edit: working code to read 7 pins to toggle 7 leds using the PCF8575.
might need some more refining.

#include <PCF8575.h>
PCF8575 pcf8575(0x20);

int inputSwitch[7] = { P0, P1, P2, P3, P4, P5, P6 };      //pcf8575 pins
int outputLeds[7] = { P8, P9, P10, P11, P12, P13, P14 };  //pcf8575 pins

int ledState[7];
int switchState[7];
int oldSwitchState[7];

unsigned long debounceDelay;

void setup() {

  Serial.begin(115200);
  for (int i = 0; i < 7; i++) {
    pcf8575.pinMode(outputLeds[i], OUTPUT);
  }

  for (int i = 0; i < 7; i++) {
    pcf8575.pinMode(inputSwitch[i], INPUT);
  }

  pcf8575.begin();
}



void loop() {

  if (millis() - debounceDelay > 20) {   
    debounceDelay = millis();
    for (int i = 0; i < 7; i++) {
      switchState[i] = pcf8575.digitalRead(inputSwitch[i]); // input switches into swithcState array
    }
  }


  for (int i = 0; i < 7; i++) {
    if (switchState[i] == LOW && oldSwitchState[i] == HIGH) {  //check if the switchstate has changed.
      ledState[i] = !ledState[i];  // if HIGH switch to LOW and viceversa if the switch state has changed.
      pcf8575.digitalWrite(outputLeds[i], !ledState[i]);  //set the leds according to the ledstate.
    }

    oldSwitchState[i] = switchState[i];   //save the current switchState to the oldSwitchState so it can be checked against.
  }
}

Nice Schematic. I suggest you redo your schematic and circuit. Each input port pin should have its pull up resistor to +5 not is series as you show them.

Look again. Weird arrangement, but not in series. Had to look twice!

Each input has its own resistor to the 5v rail, schematic is drawn a bit akward (rushed it a bit) but they are definitely not in series.

Tripple checked the circuit on the breadboard and redid all the wiring.
Also checked the solderjoints on the board with my microscope and they look good.
Also tried 1k pull-ups.
The pins still behave a bit like they are floating.

1 Like

You are correct, I did not read it properly. You can turn on the pull ups on the input if you want, it will not damage anything. Is this a new or used Arduino, from your description it sounds like something is bad.

Its a new arduino nano and as far as i can tell its working as it should.

How do i turn the pull ups of the pcf8575 on? It has no way to set pins as (INPUT_PULLUP) as far as i understand it.

One thing that i was thinking about is why is it not calling an interupt constantly.
When i push a button it does go through a single interupt routine.
if a pin would be floating shouldn't that give constant interupts if it goes from 1 to 0?

I'll hook up the oscilloscope later today and see whats happening on the i2c lines, and pins that are connected to the switches.

Really not sure if its hardware or code tho.

You do not have control over the 'pull up resistors'. Simply set the output as 0x0FF which turns all the pins high. Then reading them will give you the actual state. The pull up function is not very strong but it works. Normally you use a logic "0" as an output as it will sink current.

It has been a while but I believe when it generates an interrupt it is cleared when accessed.

There are many application on the web using this part, it is very popular.

guess i have some more learning to do, no idea yet how to "simply set the output as 0x0FF" :sweat_smile:

did take my time to do all the wiring again and instead of the nano use the mega from one of my 3d printers to eliminate that factor.
no difference at all.

not seeing any strange things on my ZT-702 on the SCL line

SCL

SDA has some more noise.
SDA

the input pins go from 5v to 0 very sharp, no noise at all.

also did some more diagnostics printing the pin states using this piece of code.

  delay(50);
  Serial.println("start");
  Serial.println(Timestamp);

  if (millis() - debounceDelay > 300) {
    uint8_t kLinksSwitchState = pcf8575.digitalRead(P0);   //kLinksSwitch
    uint8_t sLinksSwitchState = pcf8575.digitalRead(P1);   //sLinksSwitch
    uint8_t sRechtsSwitchState = pcf8575.digitalRead(P2);  //sRechtsSwitch
    uint8_t kRechtsSwitchState = pcf8575.digitalRead(P3);  //kRechtsSwitch
    uint8_t remLSwitchState = pcf8575.digitalRead(P4);     //remLSwitch
    uint8_t mistLSwitchState = pcf8575.digitalRead(P5);    //mistLSwitch
    uint8_t aUitSwitchState = pcf8575.digitalRead(P6);     //aUitSwitch


    Serial.println("P0");
    Serial.println(kLinksSwitchState);
    Serial.println("P1");
    Serial.println(sLinksSwitchState);
    Serial.println("P2");
    Serial.println(sRechtsSwitchState);
    Serial.println("P3");
    Serial.println(kRechtsSwitchState);
    Serial.println("P4");
    Serial.println(remLSwitchState);
    Serial.println("P5");
    Serial.println(mistLSwitchState);
    Serial.println("P6");
    Serial.println(aUitSwitchState);

without delay it prints as follows.

start
322
P0
0
P1
0
P2
0
P3
0
P4
0
P5
0
P6
0
start
325
P0
1
P1
1
P2
1
P3
1
P4
1
P5
1
P6
1
start
330
P0
0
P1
0
P2
0
P3
0
P4
0
P5
0
P6
0
start
338
P0
1
P1
1
P2
1
P3
1
P4
1
P5
1
P6
1
start

with the delay

start
3971
P0
1
P1
1
P2
1
P3
1
P4
1
P5
1
P6
1
start
4022
P0
1
P1
1
P2
1
P3
1
P4
1
P5
1
P6
1
start
4074
P0
1
P1
1
P2
1
P3
1
P4
1
P5
1
P6
1
start
4125
P0
1
P1
1
P2
1
P3
1
P4
1
P5
1
P6
1
start

so clearly with delay its behaving as it should however since i plan to use Bluetooth in this project i want to avoid delay completely.


I don't know if this may be your issue, but I always connect a 4 post push button using diagonal corners. That way, if you accidentally rotate it 90 degrees, it still works. It also means you have to reposition them on your breadboard to span the middle gap or else each side of the button is tied together through the breadboard.

1 Like

i'll keep that in mind for the next time i am building something on a breadboard.
the buttons are all correctly connected.
it currently works perfect when adding a delay and it works correctly 95% of the time when i only run the code (debounced) with an interupt.
sometimes it just gives the wrong results.

This variable is used in both your regular code and the ISR. It needs to be declared volatile or else the compiler is free to do optimizations that may break your code.
volatile bool keyPressed = false;

1 Like

Doesn't that sound like a contact bounce problem?

A cheap way to debounce a naive sketch is to slow the loop so much that contact bounce is completely missed.

At the end of a lazy loop

  delay(50);  // poor man's debouncing
}

Take one input and apply any legit debounce algorithm to it and see if it starts behaving.

Then debounce them all.

But before you do that, your sketch would be much tidier and easier to perfect, modify and enhance if all those N things were in arrays.

You can still use human names for them by employing an enum, which basically here would let you associate an index into the arrays by a nice name.

Then the debouncing could be array-based and handle all the switches in a loop that did the debouncing and state change detecting.

a7

1 Like

thanks for giving me the heads up, didn't make a difference but good thing to learn.
going to dive a bit more into that.

debounce was the issue and i have overlooked it a lot apparently.
when i read that part of your post i first thought thats exactly what i am doing already but looked into it again and i did make a small mistake with it.

before reading the pins i did have:
if (millis() - debounceDelay > 50) {

but forgot this part in the code and it is kind of important to set that to the current millis so it functions more then once :flushed:
debounceDelay = millis();

this if now running perfectly in the loop without using interupt or delay.

#include <PCF8575.h>
#define NOT_SEQUENTIAL_PINOUT
#define INTERRUPTED_PIN 3

void keyPressedOnPCF8575();
PCF8575 pcf8575(0x20, INTERRUPTED_PIN, keyPressedOnPCF8575);

//powerhandeling
const int powerSwitch = 2;

// defining output pins for LEDs

const int kLinks(pcf8575, P8);
const int sLinks(pcf8575, P9);
const int sRechts(pcf8575, P10);
const int kRechts(pcf8575, P11);
const int remL(pcf8575, P12);
const int mistL(pcf8575, P13);
const int aUit(pcf8575, P14);


// defining button pins

const int kLinksSwitch(pcf8575, P0);
const int sLinksSwitch(pcf8575, P1);
const int sRechtsSwitch(pcf8575, P2);
const int kRechtsSwitch(pcf8575, P3);
const int remLSwitch(pcf8575, P4);
const int mistLSwitch(pcf8575, P5);
const int aUitSwitch(pcf8575, P6);

// defining button state

int kLinksSwitchState;
int sLinksSwitchState;
int sRechtsSwitchState;
int kRechtsSwitchState;
int remLSwitchState;
int mistLSwitchState;
int aUitSwitchState;

//defining previous button state

int oldkLinksSwitchState = LOW;
int oldsLinksSwitchState = LOW;
int oldsRechtsSwitchState = LOW;
int oldkRechtsSwitchState = LOW;
int oldremLSwitchState = LOW;
int oldmistLSwitchState = LOW;
int oldaUitSwitchState = LOW;


// definging outputstates states

int kLinksState = HIGH;
int sLinksState = HIGH;
int sRechtstate = HIGH;
int kRechtstate = HIGH;
int remLState = HIGH;
int mistLState = HIGH;
int aUitState = HIGH;


//defining housekeeping states
int powerState = HIGH;
int ledState = LOW;

unsigned long ledMillis = 0;
unsigned long powerMillis = 0;

const long ledinterval = 1000;
const long powerinterval = 20000;

unsigned long debounceDelay;
unsigned long Timestamp = 0;


void setup() {

  Serial.begin(115200);

  pinMode(powerSwitch, OUTPUT);
  digitalWrite(powerSwitch, HIGH);  // Keeping power latching circuit ON

  //IO expander pinmodes
  pcf8575.pinMode(P0, INPUT);
  pcf8575.pinMode(P1, INPUT);
  pcf8575.pinMode(P2, INPUT);
  pcf8575.pinMode(P3, INPUT);
  pcf8575.pinMode(P4, INPUT);
  pcf8575.pinMode(P5, INPUT);
  pcf8575.pinMode(P6, INPUT);

  pcf8575.pinMode(P8, OUTPUT);  //P10
  pcf8575.pinMode(P9, OUTPUT);
  pcf8575.pinMode(P10, OUTPUT);
  pcf8575.pinMode(P11, OUTPUT);
  pcf8575.pinMode(P12, OUTPUT);
  pcf8575.pinMode(P13, OUTPUT);
  pcf8575.pinMode(P14, OUTPUT);

  pcf8575.digitalWrite(P0, HIGH);
  pcf8575.digitalWrite(P1, HIGH);
  pcf8575.digitalWrite(P2, HIGH);
  pcf8575.digitalWrite(P3, HIGH);
  pcf8575.digitalWrite(P4, HIGH);
  pcf8575.digitalWrite(P5, HIGH);
  pcf8575.digitalWrite(P6, HIGH);
  pcf8575.begin();
}

volatile bool keyPressed = false;

void loop() {

  Timestamp = millis();

  // bluetooth code goes here.

  // rtc + powerhandeling goes here

  // Mosfet handeling code goes here


  // delay(50);

  // if (keyPressed) {  //interupt routine
  //Serial.write("start");
  //Serial.println(Timestamp);

  if (millis() - debounceDelay > 50) {
    debounceDelay = millis();
    uint8_t kLinksSwitchState = pcf8575.digitalRead(P0);   //kLinksSwitch
    uint8_t sLinksSwitchState = pcf8575.digitalRead(P1);   //sLinksSwitch
    uint8_t sRechtsSwitchState = pcf8575.digitalRead(P2);  //sRechtsSwitch
    uint8_t kRechtsSwitchState = pcf8575.digitalRead(P3);  //kRechtsSwitch
    uint8_t remLSwitchState = pcf8575.digitalRead(P4);     //remLSwitch
    uint8_t mistLSwitchState = pcf8575.digitalRead(P5);    //mistLSwitch
    uint8_t aUitSwitchState = pcf8575.digitalRead(P6);     //aUitSwitch


    Serial.println("P0");
    Serial.println(kLinksSwitchState);
    Serial.println("P1");
    Serial.println(sLinksSwitchState);
    Serial.println("P2");
    Serial.println(sRechtsSwitchState);
    Serial.println("P3");
    Serial.println(kRechtsSwitchState);
    Serial.println("P4");
    Serial.println(remLSwitchState);
    Serial.println("P5");
    Serial.println(mistLSwitchState);
    Serial.println("P6");
    Serial.println(aUitSwitchState);

    //keyPressed = false;

    if (kLinksSwitchState == LOW && oldkLinksSwitchState == HIGH) {
      kLinksState = !kLinksState;
      pcf8575.digitalWrite(kLinks, kLinksState);
    }
    oldkLinksSwitchState = kLinksSwitchState;



    if (sLinksSwitchState == LOW && oldsLinksSwitchState == HIGH) {
      sLinksState = !sLinksState;
      pcf8575.digitalWrite(sLinks, sLinksState);
    }
    oldsLinksSwitchState = sLinksSwitchState;



    if (sRechtsSwitchState == LOW && oldsRechtsSwitchState == HIGH) {
      sRechtstate = !sRechtstate;
      pcf8575.digitalWrite(sRechts, sRechtstate);
    }
    oldsRechtsSwitchState = sRechtsSwitchState;



    if (kRechtsSwitchState == LOW && oldkRechtsSwitchState == HIGH) {
      kRechtstate = !kRechtstate;
      pcf8575.digitalWrite(kRechts, kRechtstate);
    }
    oldkRechtsSwitchState = kRechtsSwitchState;



    if (remLSwitchState == LOW && oldremLSwitchState == HIGH) {
      remLState = !remLState;
      pcf8575.digitalWrite(remL, remLState);
    }
    oldremLSwitchState = remLSwitchState;


    if (mistLSwitchState == LOW && oldmistLSwitchState == HIGH) {
      mistLState = !mistLState;
      pcf8575.digitalWrite(mistL, mistLState);
    }
    oldmistLSwitchState = mistLSwitchState;



    if (aUitSwitchState == LOW && oldaUitSwitchState == HIGH) {
      aUitState = !aUitState;
      pcf8575.digitalWrite(aUit, aUitState);
    }
    oldaUitSwitchState = aUitSwitchState;
  }
  //}
}


void keyPressedOnPCF8575() {
  Serial.println("Interupt");
  keyPressed = true;
}

i don't fully understand arrays yet and i don't want to just cross my fingers and go copy paste some code in someone else wrote and is like abracadabra if you know what i mean :wink:
pretty much wrote this with what i already know but more importantly using code that i mostly understand the function of.
going to have a look at some tutorials on the subject later and probably re-write it again from scratch, think that is the best way to learn things.

You 50 millisecond millis()-based loop throttle is a way to employ the poor man's debouncing without using delay. The rest of the loop will run as fast as it ever did, and the code (except for the printing) in the throttle takes no time, to a first approximation, meaning you have almost an entire Arduino's full resources left over.

Yes. This is a perfect sketch to use when you have learned a bit more.

Arrays are conceptually fairly simple and the syntax isn't too pesky. And an enum can let you keep referring to things with nice names.

For whenver you get to it, I took some lines of your code and spaced them out a bit to see

// defining button state

int kLinks        SwitchState;
int sLinks        SwitchState;
int sRechts       SwitchState;
int kRechts       SwitchState;
int remL          SwitchState;
int mistL         SwitchState;
int aUit          SwitchState;

//defining previous button state

int old   kLinks   SwitchState = LOW;
int old   sLinks   SwitchState = LOW;
int old   sRechts  SwitchState = LOW;
int old   kRechts  SwitchState = LOW;
int old   remL     SwitchState = LOW;
int old   mistL    SwitchState = LOW;
int old   aUit     SwitchState = LOW;


// definging outputstates states

int kLinks    State  = HIGH;
int sLinks    State  = HIGH;
int sRecht    State  = HIGH;
int kRecht    State  = HIGH;
int remL      State  = HIGH;
int mistL     State  = HIGH;
int aUit      State  = HIGH;

With arrays

// make up any names you want
enum {KLinks = 0, SLinks, SRecht, KRecht, RemL, MistL, AUit};    

// room for all those scalars
int switchState[7];
int oldSwitchState[7];
int state[7];

// in setup do it in a loop
  for (int ii = 0; ii < 7; ii++) state[ii] = HIGH;

In the rest of the code, you can refer to any switchState, oldSwitchState or state either using its enum name or an integer serving as an index number.

If you use index numbers for the array indices, repeated code can now just be looped-over code.

Whenever. You'll like it.

a7

1 Like

probably even better if i read the pins only when an interrupt is triggered.
printing was only for the diagnostics of the issue i was having.

i do have a basic understanding of an array like this and that it has indexes that you can define.

int switchState[7];
int oldSwitchState[7];
int state[7];

how to use it is the biggest hurdle for me.

  for (int ii = 0; ii < 7; ii++) state[ii] = HIGH;

if i break it down.

using a for loop

for

starting at the first index of the array as index 0 is the first entry of the array

(int ii = 0;

it goes to index 7 of the array incrementing, 0,1,2,3,4,5,6

 ii < 7; ii++)

not sure about this, it sets something to HIGH (1), guessing its setting all the states to high or is that defining what int its using?

state[ii] = HIGH;

so maybe i do get it but just need to spend some time with it?
after writing everything above this i looked at the array example code, looked at your code, and i think it might have clicked a bit....
just put a couple examples besides each other and was able to type out the code i think would be correct.

int outputLeds[7] = { P8, P9, P10, P11, P12, P13, P14 };

void setup() {

  for (int i = 0; i < 7; i++) {
    pinMode(outputLeds[i], OUTPUT);
  }
}

// forum example.
//for (int ii = 0; ii < 7; ii++) state[ii] = HIGH;

// example sketch example
//for (int thisPin = 0; thisPin < pinCount; thisPin++) {

//  pinMode(ledPins[thisPin], OUTPUT);
//  }

void loop() {
  // put your main code here, to run repeatedly:
}

Looks good. It's all downhill from here, I won't explain further lest I damage the delicate structures that are being formed in your brain as you learn. :wink:

I am assuming that the pcf8575 objects are meeting you more than halfway, it looks like a well designed library oh wait, you prolly need

pcf8575.pinMode(leds[ii], OUTPUT);

in that loop. I'm at the beach and cannot verify. But you on your way to becoming dangerous and little subtleties like this can be figured out or ask back here and someone who knows more than I can say better.

I'll guess that the other expressions involving inputs and outputs on the pcf8575 would look similar.

One matter of the most minor significance… even in a very localized circumstance, a single letter is a bad name for a variable.

I use ii by habit in for loops if I don't need the index variable outside the loop. Pick something you like better and use it, just not j or k or whatever.

a7

yes, compiler was not happy about that, not only because of that but i didn't include the library to begin with.

i am??? tutorials, knowing how to google and just trying things out works great when it comes to learning...
then again, alot of basic things are tough to find.

#include <PCF8575.h>
#define NOT_SEQUENTIAL_PINOUT

PCF8575 pcf8575(0x20);

enum { kLinks = 0,
       sLinks,
       sRechts,
       kRechts,
       remL,
       mistL,
       aUit };
const int inputSwitch[] = { P0, P1, P2, P3, P4, P5, P6, P7 };
const int outputLeds[] = { P8, P9, P10, P11, P12, P13, P14 };


int switchState[7];
int oldSwitchState[7];
int state[7];



unsigned long debounceDelay;


void setup() {

  Serial.begin(115200);

  for (int i = 0; i < 7; i++) {
    pcf8575.pinMode(outputLeds[i], OUTPUT);
  }

  for (int i = 0; i < 7; i++) {
    pcf8575.pinMode(inputSwitch[i], INPUT);
  }

  for (int i = 0; i < 7; i++) {
    state[i], HIGH;
  }

  pcf8575.begin();
}


volatile bool keyPressed = false;


void loop() {

  if (millis() - debounceDelay > 50) {
    debounceDelay = millis();
  }
}

next on the list is going to be figuring out how to read those pins and make it toggle the LED's again.
Plenty of information about outputting an array not so much on the inputs.

Haha, yes.

This

  for (int i = 0; i < 7; i++) {
    state[i], HIGH;
  }

will compile, but it won't do what it looks like you intended.

Compare

  for (int i = 0; i < 7; i++) {
    state[i] = HIGH;
  }

Leaving aside the complication of the pcf8575 objects, state[i] is an integer and it can appear anywhere you would need one, whether you are getting at the integer stored at index i or assigning a new value.

    int xx = state[i];     // xx gets the integer stored at i

    state[i] = 42;     // now that integer is 42

    state[i]++;      // increment just like with an integer, now it's 43

    state[i] = xx;  // and back to what it was before... 

I'll learn something about how to do the pcf8575 stuff if you or someone hasn't figured it out. I am just not where I can do the best research, and there are the waves and stuff.

a7

1 Like

after thinking about it i figured out its not necessary to do it that way.
much easier to just set them to 1 from the start.

int state[7] = { 1, 1, 1, 1, 1, 1, 1 };

ah yes waves and sand, much better then pixels so what are you doing on a screen replying to my posts :joy:

with a little bit of sleep and another try button presses work and leds light up.
not completely the way i want to yet but at least i am heading in the right direction.
what i did was using the old code and try to implement using the arrays and came up with this.

#include <PCF8575.h>
#define NOT_SEQUENTIAL_PINOUT
PCF8575 pcf8575(0x20);

byte inputSwitch[] = { P0, P1, P2, P3, P4, P5, P6, P7 };
byte outputLeds[] = { P8, P9, P10, P11, P12, P13, P14 };

int ledState[7];
int switchState[7];
int oldSwitchState[7];

unsigned long debounceDelay;

void setup() {

  Serial.begin(115200);

  for (int i = 0; i < 7; i++) {
    pcf8575.pinMode(outputLeds[i], OUTPUT);
  }

  for (int i = 0; i < 7; i++) {
    pcf8575.pinMode(inputSwitch[i], INPUT);
  }

  pcf8575.begin();
}


void loop() {


  if (millis() - debounceDelay > 20) {
    debounceDelay = millis();

    for (int i = 0; i < 7; i++) {
      switchState[i] = pcf8575.digitalRead(inputSwitch[i]);

      if (switchState[i] == LOW && oldSwitchState[i] == HIGH) {
        ledState[i] = !ledState[i];
        pcf8575.digitalWrite(outputLeds[i], !ledState[i]);
      }
      oldSwitchState[i] = switchState[i];
    }
  }
}

pretty much used this portion of the code thats confirmed to be working and tried to convert that to using an array.

    uint8_t kLinksSwitchState = pcf8575.digitalRead(P0);
    if (kLinksSwitchState == LOW && oldkLinksSwitchState == HIGH) {
      kLinksState = !kLinksState;
      pcf8575.digitalWrite(kLinks, kLinksState);
    }
    oldkLinksSwitchState = kLinksSwitchState;

its not exactly behaving as i want yet as pressing button tied to P7 the led tied to p14 lights up and goes off when i press it again, thats the correct behavior.
when i press the button tied to P6 then the leds tied to P14 AND P13 both toggle.
with P5 its P14, P13 and P12 that toggle.
all the way to when i press the switch tied to P1 all the leds toggle.
and if all the leds are on and press for example P6 again then the leds tied to P14 and P13 turn off and the rest stays on.