Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Arduino IDE demo source code for KC868-AI]--#01-KC868-AI_pcf8574_digital input_code
#1
[Arduino IDE demo source code for KC868-AI]--#01-KC868-AI_pcf8574_digital input_code

Code:
/*
KeyPressed on PIN1
by Mischianti Renzo <http://www.mischianti.org>

https://www.mischianti.org/2019/01/02/pcf8574-i2c-digital-i-o-expander-fast-easy-usage/
*/

#include "Arduino.h"
#include "PCF8574.h"

// Set i2c address
PCF8574 pcf8574_1(0x24);
PCF8574 pcf8574_2(0x25);
PCF8574 pcf8574_3(0x21);
PCF8574 pcf8574_4(0x22);
PCF8574 pcf8574_5(0x26);
PCF8574 pcf8574_6(0x23);
unsigned long timeElapsed;
void setup()
{
    Serial.begin(115200);
    delay(1000);

pcf8574_1.pinMode(P0, INPUT);
pcf8574_1.pinMode(P1, INPUT);
pcf8574_1.pinMode(P2, INPUT);
pcf8574_1.pinMode(P3, INPUT);
pcf8574_1.pinMode(P4, INPUT);
pcf8574_1.pinMode(P5, INPUT);
pcf8574_1.pinMode(P6, INPUT);
pcf8574_1.pinMode(P7, INPUT);

pcf8574_2.pinMode(P0, INPUT);
pcf8574_2.pinMode(P1, INPUT);
pcf8574_2.pinMode(P2, INPUT);
pcf8574_2.pinMode(P3, INPUT);
pcf8574_2.pinMode(P4, INPUT);
pcf8574_2.pinMode(P5, INPUT);
pcf8574_2.pinMode(P6, INPUT);
pcf8574_2.pinMode(P7, INPUT);

pcf8574_3.pinMode(P0, INPUT);
pcf8574_3.pinMode(P1, INPUT);
pcf8574_3.pinMode(P2, INPUT);
pcf8574_3.pinMode(P3, INPUT);
pcf8574_3.pinMode(P4, INPUT);
pcf8574_3.pinMode(P5, INPUT);
pcf8574_3.pinMode(P6, INPUT);
pcf8574_3.pinMode(P7, INPUT);

pcf8574_4.pinMode(P0, INPUT);
pcf8574_4.pinMode(P1, INPUT);
pcf8574_4.pinMode(P2, INPUT);
pcf8574_4.pinMode(P3, INPUT);
pcf8574_4.pinMode(P4, INPUT);
pcf8574_4.pinMode(P5, INPUT);
pcf8574_4.pinMode(P6, INPUT);
pcf8574_4.pinMode(P7, INPUT);

pcf8574_5.pinMode(P0, INPUT);
pcf8574_5.pinMode(P1, INPUT);
pcf8574_5.pinMode(P2, INPUT);
pcf8574_5.pinMode(P3, INPUT);
pcf8574_5.pinMode(P4, INPUT);
pcf8574_5.pinMode(P5, INPUT);
pcf8574_5.pinMode(P6, INPUT);
pcf8574_5.pinMode(P7, INPUT);

pcf8574_6.pinMode(P0, INPUT);
pcf8574_6.pinMode(P1, INPUT);
pcf8574_6.pinMode(P2, INPUT);
pcf8574_6.pinMode(P3, INPUT);
pcf8574_6.pinMode(P4, INPUT);
pcf8574_6.pinMode(P5, INPUT);
pcf8574_6.pinMode(P6, INPUT);
pcf8574_6.pinMode(P7, INPUT);


    Serial.print("Init pcf8574...");
    if (pcf8574_1.begin()){
        Serial.println("pcf8574_1_OK");
    }else{
        Serial.println("pcf8574_1_KO");
    }

  Serial.print("Init pcf8574...");
  if (pcf8574_2.begin()){
    Serial.println("pcf8574_2_OK");
  }else{
    Serial.println("pcf8574_2_KO");
  }

  Serial.print("Init pcf8574...");
  if (pcf8574_3.begin()){
    Serial.println("pcf8574_3_OK");
  }else{
    Serial.println("pcf8574_3_KO");
  }

  Serial.print("Init pcf8574...");
  if (pcf8574_4.begin()){
    Serial.println("pcf8574_4_OK");
  }else{
    Serial.println("pcf8574_4_KO");
  }

  Serial.print("Init pcf8574...");
  if (pcf8574_5.begin()){
    Serial.println("pcf8574_5_OK");
  }else{
    Serial.println("pcf8574_5_KO");
  }

  Serial.print("Init pcf8574...");
  if (pcf8574_6.begin()){
    Serial.println("pcf8574_6_OK");
  }else{
    Serial.println("pcf8574_6_KO");
  }


}

void loop()
{
uint8_t val1 = pcf8574_1.digitalRead(P0);
uint8_t val2 = pcf8574_1.digitalRead(P1);
uint8_t val3 = pcf8574_1.digitalRead(P2);
uint8_t val4 = pcf8574_1.digitalRead(P3);
uint8_t val5 = pcf8574_1.digitalRead(P4);
uint8_t val6 = pcf8574_1.digitalRead(P5);
uint8_t val7 = pcf8574_1.digitalRead(P6);
uint8_t val8 = pcf8574_1.digitalRead(P7);

uint8_t val9 = pcf8574_2.digitalRead(P0);
uint8_t val10 = pcf8574_2.digitalRead(P1);
uint8_t val11 = pcf8574_2.digitalRead(P2);
uint8_t val12 = pcf8574_2.digitalRead(P3);
uint8_t val13 = pcf8574_2.digitalRead(P4);
uint8_t val14 = pcf8574_2.digitalRead(P5);
uint8_t val15 = pcf8574_2.digitalRead(P6);
uint8_t val16 = pcf8574_2.digitalRead(P7);

uint8_t val17 = pcf8574_3.digitalRead(P0);
uint8_t val18 = pcf8574_3.digitalRead(P1);
uint8_t val19 = pcf8574_3.digitalRead(P2);
uint8_t val20 = pcf8574_3.digitalRead(P3);
uint8_t val21 = pcf8574_3.digitalRead(P4);
uint8_t val22 = pcf8574_3.digitalRead(P5);
uint8_t val23 = pcf8574_3.digitalRead(P6);
uint8_t val24 = pcf8574_3.digitalRead(P7);


uint8_t val25 = pcf8574_4.digitalRead(P0);
uint8_t val26 = pcf8574_4.digitalRead(P1);
uint8_t val27 = pcf8574_4.digitalRead(P2);
uint8_t val28 = pcf8574_4.digitalRead(P3);
uint8_t val29 = pcf8574_4.digitalRead(P4);
uint8_t val30 = pcf8574_4.digitalRead(P5);
uint8_t val31 = pcf8574_4.digitalRead(P6);
uint8_t val32 = pcf8574_4.digitalRead(P7);

uint8_t val33 = pcf8574_5.digitalRead(P0);
uint8_t val34 = pcf8574_5.digitalRead(P1);
uint8_t val35 = pcf8574_5.digitalRead(P2);
uint8_t val36 = pcf8574_5.digitalRead(P3);
uint8_t val37 = pcf8574_5.digitalRead(P4);
uint8_t val38 = pcf8574_5.digitalRead(P5);
uint8_t val39 = pcf8574_5.digitalRead(P6);
uint8_t val40 = pcf8574_5.digitalRead(P7);

uint8_t val41 = pcf8574_6.digitalRead(P0);
uint8_t val42 = pcf8574_6.digitalRead(P1);
uint8_t val43 = pcf8574_6.digitalRead(P2);
uint8_t val44 = pcf8574_6.digitalRead(P3);
uint8_t val45 = pcf8574_6.digitalRead(P4);
uint8_t val46 = pcf8574_6.digitalRead(P5);
uint8_t val47 = pcf8574_6.digitalRead(P6);
uint8_t val48 = pcf8574_6.digitalRead(P7);


 
if (val1==LOW) Serial.println("KEY1 PRESSED");
if (val2==LOW) Serial.println("KEY2 PRESSED");
if (val3==LOW) Serial.println("KEY3 PRESSED");
if (val4==LOW) Serial.println("KEY4 PRESSED");
if (val5==LOW) Serial.println("KEY5 PRESSED");
if (val6==LOW) Serial.println("KEY6 PRESSED");
if (val7==LOW) Serial.println("KEY7 PRESSED");
if (val8==LOW) Serial.println("KEY8 PRESSED");

if (val9==LOW) Serial.println("KEY9 PRESSED");
if (val10==LOW) Serial.println("KEY10 PRESSED");
if (val11==LOW) Serial.println("KEY11 PRESSED");
if (val12==LOW) Serial.println("KEY12 PRESSED");
if (val13==LOW) Serial.println("KEY13 PRESSED");
if (val14==LOW) Serial.println("KEY14 PRESSED");
if (val15==LOW) Serial.println("KEY15 PRESSED");
if (val16==LOW) Serial.println("KEY16 PRESSED");

if (val17==LOW) Serial.println("KEY17 PRESSED");
if (val18==LOW) Serial.println("KEY18 PRESSED");
if (val19==LOW) Serial.println("KEY19 PRESSED");
if (val20==LOW) Serial.println("KEY20 PRESSED");
if (val21==LOW) Serial.println("KEY21 PRESSED");
if (val22==LOW) Serial.println("KEY22 PRESSED");
if (val23==LOW) Serial.println("KEY23 PRESSED");
if (val24==LOW) Serial.println("KEY24 PRESSED");

if (val25==LOW) Serial.println("KEY25 PRESSED");
if (val26==LOW) Serial.println("KEY26 PRESSED");
if (val27==LOW) Serial.println("KEY27 PRESSED");
if (val28==LOW) Serial.println("KEY28 PRESSED");
if (val29==LOW) Serial.println("KEY29 PRESSED");
if (val30==LOW) Serial.println("KEY30 PRESSED");
if (val31==LOW) Serial.println("KEY31 PRESSED");
if (val32==LOW) Serial.println("KEY32 PRESSED");

if (val33==LOW) Serial.println("KEY33 PRESSED");
if (val34==LOW) Serial.println("KEY34 PRESSED");
if (val35==LOW) Serial.println("KEY35 PRESSED");
if (val36==LOW) Serial.println("KEY36 PRESSED");
if (val37==LOW) Serial.println("KEY37 PRESSED");
if (val38==LOW) Serial.println("KEY38 PRESSED");
if (val39==LOW) Serial.println("KEY39 PRESSED");
if (val40==LOW) Serial.println("KEY40 PRESSED");

if (val41==LOW) Serial.println("KEY41 PRESSED");
if (val42==LOW) Serial.println("KEY42 PRESSED");
if (val43==LOW) Serial.println("KEY43 PRESSED");
if (val44==LOW) Serial.println("KEY44 PRESSED");
if (val45==LOW) Serial.println("KEY45 PRESSED");
if (val46==LOW) Serial.println("KEY46 PRESSED");
if (val47==LOW) Serial.println("KEY47 PRESSED");
if (val48==LOW) Serial.println("KEY48 PRESSED");



    delay(300);
}


Attached Files
.zip   8574-DI.zip (Size: 1.38 KB / Downloads: 246)
Reply


Forum Jump:


Users browsing this thread:
1 Guest(s)