Advertisement
DrRandom

2 PCF Expander

May 7th, 2020
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifndef ExternalIO_h
  2. #define ExternalIO_h
  3.  
  4.  
  5. #define DEBOUNCEE 150
  6. unsigned long previousMilliss = 0;
  7. int InterruptPIN = 17;
  8. boolean keyChanged = false;
  9. void InterruptOnIn(){keyChanged = true;}
  10.  
  11. PCF8574 ExtOuts(0x20);
  12. PCF8574 ExtIns(0x21);
  13.  
  14. static const inline void Setup_Expanders(){
  15.     for(int i=0;i<8;i++) {ExtOuts.pinMode(i, OUTPUT);}
  16.     ExtOuts.begin();
  17.  
  18.     pinMode(InterruptPIN, INPUT_PULLUP);
  19.     attachInterrupt(digitalPinToInterrupt(InterruptPIN), InterruptOnIn, FALLING);
  20.  
  21.     for(int i=0;i<8;i++) {ExtIns.pinMode(i, INPUT);}
  22.     ExtIns.begin();
  23.     Wire.beginTransmission(0x21);
  24.     Wire.write(0xFF);
  25.     Wire.endTransmission(true);
  26.  
  27.     for(int i=0;i<8;i++) {ExtOuts.digitalWrite(i, HIGH);delay(100);}
  28.     for(int i=8;i>=0;i--) {ExtOuts.digitalWrite(i, LOW);delay(100);}
  29. }
  30.  
  31.  
  32. static const inline void Expander_Loop(){
  33.   if (keyChanged){
  34.   unsigned long currentMilliss = millis();
  35.     if (currentMilliss - previousMilliss < DEBOUNCEE){return;}
  36.  
  37.     PCF8574::DigitalInput val = ExtIns.digitalReadAll();
  38.     if (val.p0==HIGH){
  39.       if(ExtOuts.digitalRead(P0) == LOW){
  40.         ExtOuts.digitalWrite(P0, HIGH);
  41.       }else{
  42.         ExtOuts.digitalWrite(P0, LOW);
  43.       }
  44.     }
  45.     if (val.p1==HIGH) {
  46.       if(ExtOuts.digitalRead(P1) == LOW){
  47.         ExtOuts.digitalWrite(P1, HIGH);
  48.       }else{
  49.         ExtOuts.digitalWrite(P1, LOW);
  50.       }
  51.     }
  52.     if (val.p2==HIGH) {
  53.       if(ExtOuts.digitalRead(P2) == LOW){
  54.         ExtOuts.digitalWrite(P2, HIGH);
  55.       }else{
  56.         ExtOuts.digitalWrite(P2, LOW);
  57.       }
  58.     }
  59.     if (val.p3==HIGH) {
  60.       if(ExtOuts.digitalRead(P3) == LOW){
  61.         ExtOuts.digitalWrite(P3, HIGH);
  62.       }else{
  63.         ExtOuts.digitalWrite(P3, LOW);
  64.       }
  65.     }
  66.     if (val.p4==HIGH) {
  67.       if(ExtOuts.digitalRead(P4) == LOW){
  68.         ExtOuts.digitalWrite(P4, HIGH);
  69.       }else{
  70.         ExtOuts.digitalWrite(P4, LOW);
  71.       }
  72.     }
  73.     if (val.p5==HIGH) {
  74.       if(ExtOuts.digitalRead(P5) == LOW){
  75.         ExtOuts.digitalWrite(P5, HIGH);
  76.       }else{
  77.         ExtOuts.digitalWrite(P5, LOW);
  78.       }
  79.     }
  80.     if (val.p6==HIGH) {
  81.       if(ExtOuts.digitalRead(P6) == LOW){
  82.         ExtOuts.digitalWrite(P6, HIGH);
  83.       }else{
  84.         ExtOuts.digitalWrite(P6, LOW);
  85.       }
  86.     }
  87.     if (val.p7==HIGH) {
  88.       if(ExtOuts.digitalRead(P7) == LOW){
  89.         ExtOuts.digitalWrite(P7, HIGH);
  90.       }else{
  91.         ExtOuts.digitalWrite(P7, LOW);
  92.       }
  93.     }
  94.  
  95.     keyChanged = false;
  96.     previousMilliss = currentMilliss;
  97.  
  98.   }
  99. }
  100.  
  101. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement