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
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.
}
}