interruptWemos.ino 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * PCF8574 GPIO Port Expand
  3. * http://nopnop2002.webcrow.jp/WeMos/WeMos-25.html
  4. *
  5. * PCF8574 ----- WeMos
  6. * A0 ----- GRD
  7. * A1 ----- GRD
  8. * A2 ----- GRD
  9. * VSS ----- GRD
  10. * VDD ----- 5V/3.3V
  11. * SDA ----- GPIO_4
  12. * SCL ----- GPIO_5
  13. * INT ----- GPIO_13
  14. *
  15. * P0 ----------------- BUTTON0
  16. * P1 ----------------- BUTTON1
  17. * P2 ----------------- BUTTON2
  18. * P3 ----------------- BUTTON3
  19. * P4 ----------------- BUTTON4
  20. * P5 ----------------- BUTTON5
  21. * P6 ----------------- BUTTON6
  22. * P7 ----------------- BUTTON7
  23. *
  24. */
  25. #include "Arduino.h"
  26. #include "PCF8574.h" // https://github.com/xreef/PCF8574_library
  27. #define ESP8266_INTERRUPTED_PIN 13
  28. // Set i2c address
  29. PCF8574 pcf8574(0x20);
  30. // Function interrupt
  31. bool keyPressed = false;
  32. void ICACHE_RAM_ATTR keyPressedOnPCF8574(){
  33. // Serial.println("keyPressedOnPCF8574");
  34. keyPressed = true;
  35. }
  36. void setup()
  37. {
  38. Serial.begin(9600);
  39. delay(1000);
  40. pinMode(ESP8266_INTERRUPTED_PIN, INPUT_PULLUP);
  41. attachInterrupt(digitalPinToInterrupt(ESP8266_INTERRUPTED_PIN), keyPressedOnPCF8574, FALLING);
  42. for(int i=0;i<8;i++) {
  43. pcf8574.pinMode(i, INPUT);
  44. }
  45. Serial.print("Init pcf8574...");
  46. if (pcf8574.begin()){
  47. Serial.println("OK");
  48. }else{
  49. Serial.println("KO");
  50. }
  51. }
  52. void loop()
  53. {
  54. if (keyPressed){
  55. PCF8574::DigitalInput val = pcf8574.digitalReadAll();
  56. if (val.p0==HIGH) Serial.println("KEY0 PRESSED");
  57. if (val.p1==HIGH) Serial.println("KEY1 PRESSED");
  58. if (val.p2==HIGH) Serial.println("KEY2 PRESSED");
  59. if (val.p3==HIGH) Serial.println("KEY3 PRESSED");
  60. if (val.p4==HIGH) Serial.println("KEY4 PRESSED");
  61. if (val.p5==HIGH) Serial.println("KEY5 PRESSED");
  62. if (val.p6==HIGH) Serial.println("KEY6 PRESSED");
  63. if (val.p7==HIGH) Serial.println("KEY7 PRESSED");
  64. keyPressed= false;
  65. }
  66. }