keyPressed_withInterrupt.ino 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. KeyPressed with 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 keyPressedOnPCF8574();
  12. // Set i2c address
  13. PCF8574 pcf8574(0x39, ARDUINO_UNO_INTERRUPTED_PIN, keyPressedOnPCF8574);
  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. Serial.print("Init pcf8574...");
  22. if (pcf8574.begin()){
  23. Serial.println("OK");
  24. }else{
  25. Serial.println("KO");
  26. }
  27. timeElapsed = millis();
  28. }
  29. bool keyPressed = false;
  30. void loop()
  31. {
  32. if (keyPressed){
  33. uint8_t val = pcf8574.digitalRead(P1);
  34. Serial.print("READ VALUE FROM PCF ");
  35. Serial.println(val);
  36. keyPressed= false;
  37. }
  38. }
  39. void keyPressedOnPCF8574(){
  40. // Interrupt called (No Serial no read no wire in this function, and DEBUG disabled on PCF library)
  41. keyPressed = true;
  42. }