readAll_Interrupt.ino 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. Read all data after interrupt
  3. by Mischianti Renzo <http://www.mischianti.org>
  4. https://www.mischianti.org/2019/01/02/pcf8574-i2c-digital-i-o-expander-fast-easy-usage/
  5. */
  6. #include "Arduino.h"
  7. #include "PCF8574.h"
  8. // For arduino uno only pin 1 and 2 are interrupted
  9. #define ARDUINO_UNO_INTERRUPTED_PIN 2
  10. // Function interrupt
  11. void keyChangedOnPCF8574();
  12. // Set i2c address
  13. PCF8574 pcf8574(0x39, ARDUINO_UNO_INTERRUPTED_PIN, keyChangedOnPCF8574);
  14. unsigned long timeElapsed;
  15. void setup()
  16. {
  17. Serial.begin(115200);
  18. delay(1000);
  19. pcf8574.pinMode(P0, OUTPUT);
  20. pcf8574.pinMode(P1, INPUT);
  21. pcf8574.pinMode(P2, INPUT);
  22. Serial.print("Init pcf8574...");
  23. if (pcf8574.begin()){
  24. Serial.println("OK");
  25. }else{
  26. Serial.println("KO");
  27. }
  28. Serial.println("START");
  29. timeElapsed = millis();
  30. }
  31. bool keyChanged = false;
  32. void loop()
  33. {
  34. if (keyChanged){
  35. PCF8574::DigitalInput di = pcf8574.digitalReadAll();
  36. Serial.print("READ VALUE FROM PCF P1: ");
  37. Serial.print(di.p0);
  38. Serial.print(" - ");
  39. Serial.print(di.p1);
  40. Serial.print(" - ");
  41. Serial.print(di.p2);
  42. Serial.print(" - ");
  43. Serial.println(di.p3);
  44. // delay(5);
  45. keyChanged= false;
  46. }
  47. }
  48. void keyChangedOnPCF8574(){
  49. // Interrupt called (No Serial no read no wire in this function, and DEBUG disabled on PCF library)
  50. keyChanged = true;
  51. }