xreef / PCF8574_library

PCF8574 library. i2c digital expander for Arduino, Raspberry Pi Pico and rp2040 boards, esp32, SMT32 and ESP8266. Can read write digital values with only 2 wire. Very simple to use and encoder support.
Other
216 stars 62 forks source link

Using read+write: After a write, no longer get interrupt to fire #1

Closed petervanderwalt closed 6 years ago

petervanderwalt commented 6 years ago

Based off the example sketch: (redacted)

This works

#include "Arduino.h"
#include "PCF8574.h"
void keyPressedOnPCF8574();
PCF8574 pcf8574(0x20, 4, 5, 14, keyPressedOnPCF8574);
bool keyPressed = false;

void setup() {
 pcf8574.pinMode(P0, INPUT);
    pcf8574.pinMode(P1, INPUT);
    pcf8574.pinMode(P2, INPUT);
    pcf8574.pinMode(P3, INPUT);
    pcf8574.pinMode(P6, OUTPUT);
    pcf8574.pinMode(P7, OUTPUT);
    pcf8574.begin();
}

void loop() {
    if (keyPressed){
      Serial.print(pcf8574.digitalRead(P0));
      Serial.print(" / ");
      Serial.print(pcf8574.digitalRead(P1));
      Serial.print(" / ");
      Serial.print(pcf8574.digitalRead(P2));
      Serial.print(" / ");
      Serial.println(pcf8574.digitalRead(P3));
      keyPressed= false;
    }
}

void keyPressedOnPCF8574(){
  // Interrupt called (No Serial no read no wire in this function, and DEBUG disabled on PCF library)
   keyPressed = true;
}
#include "Arduino.h"
#include "PCF8574.h"
void keyPressedOnPCF8574();
PCF8574 pcf8574(0x20, 4, 5, 14, keyPressedOnPCF8574);
bool keyPressed = false;

void setup() {
 pcf8574.pinMode(P0, INPUT);
    pcf8574.pinMode(P1, INPUT);
    pcf8574.pinMode(P2, INPUT);
    pcf8574.pinMode(P3, INPUT);
    pcf8574.pinMode(P6, OUTPUT);
    pcf8574.pinMode(P7, OUTPUT);
    pcf8574.begin();
}

void loop() {
    if (keyPressed){
      pcf8574.digitalWrite(P6, LOW);  // turn on the LED on P6
      Serial.print(pcf8574.digitalRead(P0));
      Serial.print(" / ");
      Serial.print(pcf8574.digitalRead(P1));
      Serial.print(" / ");
      Serial.print(pcf8574.digitalRead(P2));
      Serial.print(" / ");
      Serial.println(pcf8574.digitalRead(P3));
      keyPressed= false;
      pcf8574.digitalWrite(P6, HIGH); // turn off the LED on P6
    }
}

void keyPressedOnPCF8574(){
  // Interrupt called (No Serial no read no wire in this function, and DEBUG disabled on PCF library)
   keyPressed = true;
}

The only difference is me trying to show a status LED on keyPressed

(Redacted version, in my real application i want to turn LED on at other times too.

Problem is, once the digitalWrite happened, the interrupt never fires again

xreef commented 6 years ago

Hi, thanks for your reporting. I try various test but with this sketch works correctly

`#include "Arduino.h"

include "PCF8574.h"

// For arduino uno only pin 1 and 2 are interrupted

define ARDUINO_UNO_INTERRUPTED_PIN 2

// Function interrupt void keyChangedOnPCF8574();

// Set i2c address PCF8574 pcf8574(0x39, ARDUINO_UNO_INTERRUPTED_PIN, keyChangedOnPCF8574); unsigned long timeElapsed; void setup() { Serial.begin(115200);

pcf8574.pinMode(P6, OUTPUT);
pcf8574.pinMode(P0, INPUT);
pcf8574.pinMode(P1, INPUT);
pcf8574.begin();

Serial.println("START");

timeElapsed = millis();

}

bool keyChanged = false; void loop() { if (keyChanged){ Serial.println("P6 OFF"); pcf8574.digitalWrite(P6, LOW); // turn off the LED on P6

    // PCF8574::DigitalInput di = pcf8574.digitalReadAll();
    Serial.println("READ VALUE FROM PCF: ");
    Serial.print(pcf8574.digitalRead(P0));
    Serial.print(" - ");
    Serial.println(pcf8574.digitalRead(P1));

    keyChanged= false;
    delay(1000);

    Serial.println("P6 ON");
    pcf8574.digitalWrite(P6, HIGH);  // turn on the LED on P6

}

}

void keyChangedOnPCF8574(){ // Interrupt called (No Serial no read no wire in this function, and DEBUG disabled on PCF library) keyChanged = true; } `

But I think you can't see the change of led status if you don't add a delay.

Can you give me some additional information?