03-24-2022, 06:53 AM
(This post was last modified: 03-24-2022, 06:55 AM by KinCony Support.)
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"
// Instantiate Wire for generic use at 400kHz
TwoWire I2Cone = TwoWire(0);
// Instantiate Wire for generic use at 100kHz
TwoWire I2Ctwo = TwoWire(1);
// Set i2c address
PCF8574 pcf8574_I1(0x24);
PCF8574 pcf8574_I2(0x25);
PCF8574 pcf8574_I3(0x21);
PCF8574 pcf8574_I4(0x22);
unsigned long timeElapsed;
void setup()
{
Serial.begin(115200);
delay(1000);
// pcf8574.pinMode(P0, OUTPUT);
pcf8574_I1.pinMode(P0, INPUT);
pcf8574_I1.pinMode(P1, INPUT);
pcf8574_I1.pinMode(P2, INPUT);
pcf8574_I1.pinMode(P3, INPUT);
pcf8574_I1.pinMode(P4, INPUT);
pcf8574_I1.pinMode(P5, INPUT);
pcf8574_I1.pinMode(P6, INPUT);
pcf8574_I1.pinMode(P7, INPUT);
pcf8574_I2.pinMode(P0, INPUT);
pcf8574_I2.pinMode(P1, INPUT);
pcf8574_I2.pinMode(P2, INPUT);
pcf8574_I2.pinMode(P3, INPUT);
pcf8574_I2.pinMode(P4, INPUT);
pcf8574_I2.pinMode(P5, INPUT);
pcf8574_I2.pinMode(P6, INPUT);
pcf8574_I2.pinMode(P7, INPUT);
pcf8574_I3.pinMode(P0, INPUT);
pcf8574_I3.pinMode(P1, INPUT);
pcf8574_I3.pinMode(P2, INPUT);
pcf8574_I3.pinMode(P3, INPUT);
pcf8574_I3.pinMode(P4, INPUT);
pcf8574_I3.pinMode(P5, INPUT);
pcf8574_I3.pinMode(P6, INPUT);
pcf8574_I3.pinMode(P7, INPUT);
pcf8574_I4.pinMode(P0, INPUT);
pcf8574_I4.pinMode(P1, INPUT);
pcf8574_I4.pinMode(P2, INPUT);
pcf8574_I4.pinMode(P3, INPUT);
pcf8574_I4.pinMode(P4, INPUT);
pcf8574_I4.pinMode(P5, INPUT);
pcf8574_I4.pinMode(P6, INPUT);
pcf8574_I4.pinMode(P7, INPUT);
Serial.print("Init pcf8574...");
if (pcf8574_I1.begin()){
Serial.println("pcf8574_I1_OK");
}else{
Serial.println("pcf8574_I1_KO");
}
Serial.print("Init pcf8574...");
if (pcf8574_I2.begin()){
Serial.println("pcf8574_I2_OK");
}else{
Serial.println("pcf8574_I2_KO");
}
Serial.print("Init pcf8574_I3...");
if (pcf8574_I3.begin()){
Serial.println("pcf8574_I3_OK");
}else{
Serial.println("pcf8574_I3_KO");
}
Serial.print("Init pcf8574_I4...");
if (pcf8574_I4.begin()){
Serial.println("pcf8574_I4_OK");
}else{
Serial.println("pcf8574_I4_KO");
}
}
void loop()
{
uint8_t val1 = pcf8574_I1.digitalRead(P0);
uint8_t val2 = pcf8574_I1.digitalRead(P1);
uint8_t val3 = pcf8574_I1.digitalRead(P2);
uint8_t val4 = pcf8574_I1.digitalRead(P3);
uint8_t val5 = pcf8574_I1.digitalRead(P4);
uint8_t val6 = pcf8574_I1.digitalRead(P5);
uint8_t val7 = pcf8574_I1.digitalRead(P6);
uint8_t val8 = pcf8574_I1.digitalRead(P7);
uint8_t val9 = pcf8574_I2.digitalRead(P0);
uint8_t val10 = pcf8574_I2.digitalRead(P1);
uint8_t val11 = pcf8574_I2.digitalRead(P2);
uint8_t val12 = pcf8574_I2.digitalRead(P3);
uint8_t val13 = pcf8574_I2.digitalRead(P4);
uint8_t val14 = pcf8574_I2.digitalRead(P5);
uint8_t val15 = pcf8574_I2.digitalRead(P6);
uint8_t val16 = pcf8574_I2.digitalRead(P7);
uint8_t val17 = pcf8574_I3.digitalRead(P0);
uint8_t val18 = pcf8574_I3.digitalRead(P1);
uint8_t val19 = pcf8574_I3.digitalRead(P2);
uint8_t val20 = pcf8574_I3.digitalRead(P3);
uint8_t val21 = pcf8574_I3.digitalRead(P4);
uint8_t val22 = pcf8574_I3.digitalRead(P5);
uint8_t val23 = pcf8574_I3.digitalRead(P6);
uint8_t val24 = pcf8574_I3.digitalRead(P7);
uint8_t val25 = pcf8574_I4.digitalRead(P0);
uint8_t val26 = pcf8574_I4.digitalRead(P1);
uint8_t val27 = pcf8574_I4.digitalRead(P2);
uint8_t val28 = pcf8574_I4.digitalRead(P3);
uint8_t val29 = pcf8574_I4.digitalRead(P4);
uint8_t val30 = pcf8574_I4.digitalRead(P5);
uint8_t val31 = pcf8574_I4.digitalRead(P6);
uint8_t val32 = pcf8574_I4.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");
delay(300);
}