Advertisement
babyyoda_

Master_Touch

Jul 20th, 2021
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Master program set both Load LOW MED HIGH via Serial and also via touchPad
  2.  
  3. #include <Wire.h>
  4. #define I2C_SLAVE_1  1
  5. #define I2C_SLAVE_2  2
  6.  
  7. const byte interruptPin1 = 14;  // D5
  8. const byte interruptPin2 = 12;  // D6
  9. bool button1;
  10. bool button2;
  11.  
  12. volatile int Load1state = LOW; // To make sure variables shared between an ISR
  13. volatile int Load2state = LOW; // To make sure variables shared between an ISR
  14.  
  15. #define I2C_SDA 4 // D2                     // ESP8266
  16. #define I2C_SCL 5 // D1
  17.  
  18. //#define I2C_SDA 21    // ESP32
  19. //#define I2C_SCL 22
  20.  
  21.  
  22. ICACHE_RAM_ATTR void handleInterruptOn1() {
  23.   Load1state = !Load1state;
  24.   if (Load1state) {
  25.     Wire.beginTransmission(1);
  26.     Wire.write('3');
  27.     Wire.endTransmission();
  28.     //      Serial.println("Device 1 HIGH Via Touch");
  29.   }
  30.   else if (!Load1state) {
  31.     Wire.beginTransmission(1);
  32.     Wire.write('0');
  33.     Wire.endTransmission();
  34.     //      Serial.println("Device 1 OFF Via Touch");
  35.   }
  36. }
  37.  
  38. ICACHE_RAM_ATTR void handleInterruptOn2() {
  39.   Load2state = !Load2state;
  40.   if (Load2state) {
  41.     Wire.beginTransmission(2);
  42.     Wire.write('3');
  43.     Wire.endTransmission();
  44.     //      Serial.println("Device 2 HIGH Via Touch");
  45.   }
  46.   else if (!Load2state) {
  47.     Wire.beginTransmission(2);
  48.     Wire.write('0');
  49.     Wire.endTransmission();
  50.     //      Serial.println("Device 2 OFF Via Touch");
  51.   }
  52. }
  53.  
  54.  
  55. void setup() {
  56.   Wire.begin(I2C_SDA, I2C_SCL);           // uncommnet for esp8266
  57.   //  Wire.begin();                         // uncommnet for esp32
  58.   Serial.begin(115200);
  59.   attachInterrupt(digitalPinToInterrupt(interruptPin1), handleInterruptOn1, CHANGE);
  60.   attachInterrupt(digitalPinToInterrupt(interruptPin2), handleInterruptOn2, CHANGE);
  61.  
  62.  
  63.   Serial.print("Ready");
  64. }
  65.  
  66.  
  67. char x = 0;
  68.  
  69. void loop() {
  70.  
  71.  
  72.   //    if (Load1state) {
  73.   //      Wire.beginTransmission(1);
  74.   //      Wire.write('3');
  75.   //      Wire.endTransmission();
  76.   ////      Serial.println("Device 1 HIGH Via Touch");
  77.   //    }
  78.   //    else if(!Load1state) {
  79.   //      Wire.beginTransmission(1);
  80.   //      Wire.write('0');
  81.   //      Wire.endTransmission();
  82.   ////      Serial.println("Device 1 OFF Via Touch");
  83.   //    }
  84.   //
  85.   //
  86.   //    if (Load2state) {
  87.   //      Wire.beginTransmission(2);
  88.   //      Wire.write('3');
  89.   //      Wire.endTransmission();
  90.   ////      Serial.println("Device 2 HIGH Via Touch");
  91.   //    }
  92.   //    else if(!Load2state) {
  93.   //      Wire.beginTransmission(2);
  94.   //      Wire.write('0');
  95.   //      Wire.endTransmission();
  96.   ////      Serial.println("Device 2 OFF Via Touch");
  97.   //    }
  98.  
  99.  
  100.   //-------------------------------------//
  101.   if (Serial.available() > 0) {
  102.  
  103.     x = Serial.read();
  104.  
  105.     if (x == '0') {
  106.       Wire.beginTransmission(1);
  107.       Wire.write('0');
  108.       Wire.endTransmission();
  109.       Serial.println("Device 1 OFF");
  110.     }
  111.     if (x == '1') {
  112.       Wire.beginTransmission(1);
  113.       Wire.write('1');
  114.       Wire.endTransmission();
  115.       Serial.println("Device 1 LOW");
  116.     }
  117.     if (x == '2') {
  118.       Wire.beginTransmission(1);
  119.       Wire.write('2');
  120.       Wire.endTransmission();
  121.       Serial.println("Device 1 MED");
  122.     }
  123.     if (x == '3') {
  124.       Wire.beginTransmission(1);
  125.       Wire.write('3');
  126.       Wire.endTransmission();
  127.       Serial.println("Device 1 HIGH");
  128.     }
  129.  
  130.     if (x == '7') {
  131.       Wire.beginTransmission(2);
  132.       Wire.write('0');
  133.       Wire.endTransmission();
  134.       Serial.println("Device 2 OFF");
  135.     }
  136.     if (x == '4') {
  137.       Wire.beginTransmission(2);
  138.       Wire.write('1');
  139.       Wire.endTransmission();
  140.       Serial.println("Device 2 LOW");
  141.     }
  142.     if (x == '5') {
  143.       Wire.beginTransmission(2);
  144.       Wire.write('2');
  145.       Wire.endTransmission();
  146.       Serial.println("Device 2 MED");
  147.     }
  148.     if (x == '6') {
  149.       Wire.beginTransmission(2);
  150.       Wire.write('3');
  151.       Wire.endTransmission();
  152.       Serial.println("Device 2 HIGH");
  153.     }
  154.   }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement