Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Master program set both Load LOW MED HIGH via Serial and also via touchPad
- #include <Wire.h>
- #define I2C_SLAVE_1 1
- #define I2C_SLAVE_2 2
- const byte interruptPin1 = 14; // D5
- const byte interruptPin2 = 12; // D6
- bool button1;
- bool button2;
- volatile int Load1state = LOW; // To make sure variables shared between an ISR
- volatile int Load2state = LOW; // To make sure variables shared between an ISR
- #define I2C_SDA 4 // D2 // ESP8266
- #define I2C_SCL 5 // D1
- //#define I2C_SDA 21 // ESP32
- //#define I2C_SCL 22
- ICACHE_RAM_ATTR void handleInterruptOn1() {
- Load1state = !Load1state;
- if (Load1state) {
- Wire.beginTransmission(1);
- Wire.write('3');
- Wire.endTransmission();
- // Serial.println("Device 1 HIGH Via Touch");
- }
- else if (!Load1state) {
- Wire.beginTransmission(1);
- Wire.write('0');
- Wire.endTransmission();
- // Serial.println("Device 1 OFF Via Touch");
- }
- }
- ICACHE_RAM_ATTR void handleInterruptOn2() {
- Load2state = !Load2state;
- if (Load2state) {
- Wire.beginTransmission(2);
- Wire.write('3');
- Wire.endTransmission();
- // Serial.println("Device 2 HIGH Via Touch");
- }
- else if (!Load2state) {
- Wire.beginTransmission(2);
- Wire.write('0');
- Wire.endTransmission();
- // Serial.println("Device 2 OFF Via Touch");
- }
- }
- void setup() {
- Wire.begin(I2C_SDA, I2C_SCL); // uncommnet for esp8266
- // Wire.begin(); // uncommnet for esp32
- Serial.begin(115200);
- attachInterrupt(digitalPinToInterrupt(interruptPin1), handleInterruptOn1, CHANGE);
- attachInterrupt(digitalPinToInterrupt(interruptPin2), handleInterruptOn2, CHANGE);
- Serial.print("Ready");
- }
- char x = 0;
- void loop() {
- // if (Load1state) {
- // Wire.beginTransmission(1);
- // Wire.write('3');
- // Wire.endTransmission();
- //// Serial.println("Device 1 HIGH Via Touch");
- // }
- // else if(!Load1state) {
- // Wire.beginTransmission(1);
- // Wire.write('0');
- // Wire.endTransmission();
- //// Serial.println("Device 1 OFF Via Touch");
- // }
- //
- //
- // if (Load2state) {
- // Wire.beginTransmission(2);
- // Wire.write('3');
- // Wire.endTransmission();
- //// Serial.println("Device 2 HIGH Via Touch");
- // }
- // else if(!Load2state) {
- // Wire.beginTransmission(2);
- // Wire.write('0');
- // Wire.endTransmission();
- //// Serial.println("Device 2 OFF Via Touch");
- // }
- //-------------------------------------//
- if (Serial.available() > 0) {
- x = Serial.read();
- if (x == '0') {
- Wire.beginTransmission(1);
- Wire.write('0');
- Wire.endTransmission();
- Serial.println("Device 1 OFF");
- }
- if (x == '1') {
- Wire.beginTransmission(1);
- Wire.write('1');
- Wire.endTransmission();
- Serial.println("Device 1 LOW");
- }
- if (x == '2') {
- Wire.beginTransmission(1);
- Wire.write('2');
- Wire.endTransmission();
- Serial.println("Device 1 MED");
- }
- if (x == '3') {
- Wire.beginTransmission(1);
- Wire.write('3');
- Wire.endTransmission();
- Serial.println("Device 1 HIGH");
- }
- if (x == '7') {
- Wire.beginTransmission(2);
- Wire.write('0');
- Wire.endTransmission();
- Serial.println("Device 2 OFF");
- }
- if (x == '4') {
- Wire.beginTransmission(2);
- Wire.write('1');
- Wire.endTransmission();
- Serial.println("Device 2 LOW");
- }
- if (x == '5') {
- Wire.beginTransmission(2);
- Wire.write('2');
- Wire.endTransmission();
- Serial.println("Device 2 MED");
- }
- if (x == '6') {
- Wire.beginTransmission(2);
- Wire.write('3');
- Wire.endTransmission();
- Serial.println("Device 2 HIGH");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement