writeAllEsp8266.ino 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. KeyPressed with interrupt and digital write all
  3. from P4 to P7
  4. by Mischianti Renzo <http://www.mischianti.org>
  5. https://www.mischianti.org/2019/01/02/pcf8574-i2c-digital-i-o-expander-fast-easy-usage/
  6. */
  7. #include "Arduino.h"
  8. #include "PCF8574.h"
  9. // For arduino uno only pin 1 and 2 are interrupted
  10. #define ARDUINO_UNO_INTERRUPTED_PIN D3
  11. // Function interrupt
  12. void ICACHE_RAM_ATTR keyPressedOnPCF8574();
  13. // Set i2c address
  14. PCF8574 pcf8574(0x38, ARDUINO_UNO_INTERRUPTED_PIN, keyPressedOnPCF8574);
  15. unsigned long timeElapsed;
  16. void setup()
  17. {
  18. Serial.begin(115200);
  19. delay(1000);
  20. Serial.println("INIT");
  21. pcf8574.pinMode(P0, INPUT);
  22. pcf8574.pinMode(P1, INPUT_PULLUP);
  23. pcf8574.pinMode(P2, INPUT);
  24. pcf8574.pinMode(P3, INPUT);
  25. pcf8574.pinMode(P7, OUTPUT);
  26. pcf8574.pinMode(P6, OUTPUT, HIGH);
  27. pcf8574.pinMode(P5, OUTPUT, LOW);
  28. pcf8574.pinMode(P4, OUTPUT, LOW);
  29. Serial.print("Init pcf8574...");
  30. if (pcf8574.begin()){
  31. Serial.println("OK");
  32. }else{
  33. Serial.println("KO");
  34. }
  35. Serial.println("START");
  36. timeElapsed = millis();
  37. }
  38. unsigned long lastSendTime = 0; // last send time
  39. unsigned long interval = 3000; // interval between sends
  40. bool startVal = HIGH;
  41. bool keyPressed = false;
  42. void loop()
  43. {
  44. if (keyPressed){
  45. uint8_t val0 = pcf8574.digitalRead(P0);
  46. uint8_t val1 = pcf8574.digitalRead(P1);
  47. uint8_t val2 = pcf8574.digitalRead(P2);
  48. uint8_t val3 = pcf8574.digitalRead(P3);
  49. Serial.print("P0 ");
  50. Serial.print(val0);
  51. Serial.print(" P1 ");
  52. Serial.println(val1);
  53. Serial.print("P2 ");
  54. Serial.print(val2);
  55. Serial.print(" P3 ");
  56. Serial.println(val3);
  57. keyPressed= false;
  58. }
  59. if (millis() - lastSendTime > interval) {
  60. Serial.print("WRITE ALL VALUE FROM P4 TO P7 ");
  61. Serial.println(startVal);
  62. // pcf8574.digitalWrite(P7, startVal);
  63. bool startVal2 = LOW;
  64. if (startVal==HIGH) {
  65. startVal = LOW;
  66. startVal2 = HIGH;
  67. }else{
  68. startVal = HIGH;
  69. startVal2 = LOW;
  70. }
  71. PCF8574::DigitalInput digitalInput;
  72. digitalInput.p4 = startVal2;
  73. digitalInput.p5 = startVal;
  74. digitalInput.p6 = startVal2;
  75. digitalInput.p7 = startVal;
  76. pcf8574.digitalWriteAll(digitalInput);
  77. lastSendTime = millis();
  78. }
  79. }
  80. void keyPressedOnPCF8574(){
  81. // Interrupt called (No Serial no read no wire in this function, and DEBUG disabled on PCF library)
  82. keyPressed = true;
  83. }