Advertisement
mikroavr

input_pcf8574

Jan 14th, 2024
904
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include "Arduino.h"
  3. #include "PCF8574.h"
  4.  
  5. // Set i2c address
  6. PCF8574 pcf1(0x20);
  7. PCF8574 pcf2(0x21);
  8. PCF8574 pcf3(0x22);
  9. unsigned long cur_time, old_time;
  10. bool stateLed = 0;
  11. byte pin_led = 5;
  12.  
  13. byte pin_pcf[] = {0, 1, 2, 3, 4, 5, 6, 7};
  14. byte dt_pcf1[10];
  15. byte dt_pcf2[10];
  16. byte dt_pcf3[10];
  17.  
  18. void setup() {
  19.   // put your setup code here, to run once:
  20.   delay(100);
  21.   Serial.begin(115200);
  22.   pinMode(pin_led, OUTPUT);
  23.  
  24.   Serial.print("Init pcf8574...");
  25.  
  26.   for (int i = 0; i < sizeof(pin_pcf); i++) {
  27.     Serial.print(i); Serial.print(",");
  28.     pcf1.pinMode(pin_pcf[i], INPUT);
  29.     pcf2.pinMode(pin_pcf[i], INPUT);
  30.     pcf3.pinMode(pin_pcf[i], INPUT);
  31.   }
  32.   Serial.println();
  33.  
  34.   if (pcf1.begin()) {
  35.     Serial.println("pcf1 OK");
  36.   } else {
  37.     Serial.println("pcf1 NOK");
  38.   }
  39.  
  40.   if (pcf2.begin()) {
  41.     Serial.println("pcf2 OK");
  42.   } else {
  43.     Serial.println("pcf2 NOK");
  44.   }
  45.  
  46.   if (pcf3.begin()) {
  47.     Serial.println("pcf3 OK");
  48.   } else {
  49.     Serial.println("pcf3 NOK");
  50.   }
  51.  
  52.   delay(100);
  53. }
  54.  
  55. void loop() {
  56.   // put your main code here, to run repeatedly:
  57.   cur_time = millis();
  58.   if (cur_time - old_time >= 500) {
  59.     stateLed = !stateLed;
  60.     digitalWrite(pin_led, stateLed);
  61.     Serial.print("pcf1: ");
  62.     for (int i = 0; i < sizeof(pin_pcf); i++) {
  63.       Serial.print(pcf1.digitalRead(pin_pcf[i]));
  64.       Serial.print(",");
  65.       delay(5);
  66.     }
  67.     Serial.println();
  68.     Serial.print("pcf2: ");
  69.     for (int i = 0; i < sizeof(pin_pcf); i++) {
  70.       Serial.print(pcf2.digitalRead(pin_pcf[i]));
  71.       Serial.print(",");
  72.       delay(5);
  73.     }
  74.  
  75.     Serial.println();
  76.     Serial.print("pcf3: ");
  77.     for (int i = 0; i < sizeof(pin_pcf); i++) {
  78.       Serial.print(pcf3.digitalRead(pin_pcf[i]));
  79.       Serial.print(",");
  80.       delay(5);
  81.     }
  82.     Serial.println();
  83.     Serial.println();
  84.    
  85.     old_time = cur_time;
  86.   }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement