Advertisement
mikroavr

TCA_read_digital

Nov 17th, 2023 (edited)
1,211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "EEPROM.h"
  2.  
  3. #include "Wire.h"
  4. #include "TCA9555.h"
  5. TCA9555 TCA1(0x20);
  6. TCA9555 TCA2(0x21);
  7.  
  8. unsigned long cur_time, old_time;
  9. int ledState = LOW;
  10.  
  11. byte pin_tca[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
  12. byte pin_led = 16;
  13. byte dt_tca1[20];
  14. byte dt_tca2[20];
  15.  
  16. byte addr_status = 2;
  17. int dt_status = 0;
  18.  
  19. void setup() {
  20.   // put your setup code here, to run once:
  21.   delay(10);
  22.   Serial.begin(115200);
  23.   Serial.println();
  24.  
  25.     if (!EEPROM.begin(1000)) {
  26.       Serial.println("Failed to initialise EEPROM");
  27.       Serial.println("Restarting...");
  28.       delay(1000);
  29.       ESP.restart();
  30.     }
  31.     delay(100);
  32.  
  33.   dt_status = EEPROM.readInt(addr_status);
  34.   if(dt_status == -1 )dt_status = 0;
  35.   Serial.print("dt_status: ");
  36.   Serial.println(dt_status);
  37.   delay(2000);
  38.  
  39.   pinMode(pin_led, OUTPUT);
  40.  
  41.   Wire.begin();
  42.   TCA1.begin();
  43.   TCA2.begin();
  44.  
  45.   for (int i = 0; i < sizeof(pin_tca); i++) {
  46.     Serial.print(i); Serial.print(",");
  47.     TCA1.pinMode(pin_tca[i], INPUT_PULLUP);
  48.     TCA2.pinMode(pin_tca[i], INPUT_PULLUP);
  49.   }
  50.   Serial.println();
  51. }
  52.  
  53. void loop() {
  54.   // put your main code here, to run repeatedly:
  55.   cur_time = millis();
  56.   if (cur_time - old_time >= 100) {
  57.     ledState = !ledState;
  58.     digitalWrite(pin_led, ledState);
  59.     Serial.print("TCA1: ");
  60.     for (int i = 0; i < sizeof(pin_tca) - 4; i++) {
  61.       dt_tca1[i] = TCA1.digitalRead(pin_tca[i]);
  62.       Serial.print(dt_tca1[i]);
  63.       Serial.print(",");
  64.       if(dt_tca1[i] == 0){
  65.         dt_status ++;
  66.         EEPROM.writeInt(addr_status, dt_status);
  67.         EEPROM.commit();
  68.       }
  69.       delay(20);
  70.     }
  71.     Serial.println();
  72.     Serial.print("TCA2: ");
  73.     for (int i = 0; i < sizeof(pin_tca) - 4; i++) {
  74.       dt_tca2[i] = TCA2.digitalRead(pin_tca[i]);
  75.       Serial.print(dt_tca2[i]);
  76.       Serial.print(",");
  77.       if(dt_tca2[i] == 0){
  78.         dt_status ++;
  79.         EEPROM.writeInt(addr_status, dt_status);
  80.         EEPROM.commit();
  81.       }
  82.       delay(20);
  83.     }
  84.     Serial.println();
  85.     Serial.print("dt_status: ");
  86.     Serial.println(dt_status);
  87.     old_time = millis();
  88.   }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement