Version 02 of Relay

This commit is contained in:
fatimaxsen committed 2025-05-18 10:06:27 +03:00
1 parent ec078c0d00
commit dd5a8b28f7
5 files changed
+343 -237

No files matched your search

+61 -43
View File
@@ -1,57 +1,75 @@
#include <I2CRelay.h>
#include <Wire.h>
#include "I2CRelay.h"
I2CRelay relay;
I2CRelay relay(8, 3); // Use your SDA/SCL pins
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("I2CRelay Master Example with 'i' command for ID");
relay.begin();
Serial.begin(115200);
relay.begin();
// Run test functions as before
relay.setRelay(1, 1);
relay.setRelay(8, 1);
Serial.println("Turned ON relay 1 and relay 8");
delay(500);
uint8_t addr = 0x08;
relay.setRelay(1, 0);
relay.setRelay(8, 0);
Serial.println("Turned OFF relay 1 and relay 8");
delay(500);
// 1. Scan the I2C bus
Serial.println("=== I2C Scan ===");
relay.scan();
relay.setAllRelays(1);
Serial.println("Turned ON all relays");
delay(500);
// 2. Get info from the slave
Serial.println("=== Request Info ===");
String info = relay.requestSlaveInfo(addr);
Serial.print("Info: ");
Serial.println(info);
relay.setAllRelays(0);
Serial.println("Turned OFF all relays");
delay(500);
// 3. Change the slave name
Serial.println("=== Change Name ===");
relay.setSlaveName(addr, "FirstRelay");
delay(100);
Serial.println("Name changed.");
relay.setDeviceName("TestPanel");
Serial.println("Device name set to 'TestPanel'");
delay(500);
// 4. Turn relay 1 ON
Serial.println("=== Turn Relay 1 ON ===");
relay.setRelay(addr, 1, 1); // 1 = ON
delay(1500);
String id = relay.getIdentification();
Serial.print("Identification string: ");
Serial.println(id);
delay(500);
// 5. Turn relay 1 OFF
Serial.println("=== Turn Relay 1 OFF ===");
relay.setRelay(addr, 1, 0); // 0 = OFF
delay(500);
relay.setI2CAddress(0x09);
Serial.println("Changed I2C address to 0x09");
Serial.println("** Update master address to 0x09 for further communication **");
// 6. Turn all relays ON
Serial.println("=== Turn All Relays ON ===");
relay.setAllRelays(addr, 1); // 1 = ON
delay(500);
// 7. Turn all relays OFF
Serial.println("=== Turn All Relays OFF ===");
relay.setAllRelays(addr, 0); // 0 = OFF
delay(500);
// 8. Turn relay 2 ON for 2 seconds
Serial.println("=== Relay 2 ON for 2 seconds ===");
relay.setRelayTimer(addr, 2, 2000); // relay 2, 2000 ms
delay(2500);
// 9. Turn all relays ON for 3 seconds
Serial.println("=== All relays ON for 3 seconds ===");
relay.setAllRelaysTimer(addr, 3000); // 3000 ms
delay(3500);
// 10. Change slave address from 0x08 to 0x09
Serial.println("=== Change Address 0x08 -> 0x09 ===");
relay.setSlaveAddress(0x08, 0x08);
addr = 0x08; // Use new address for further commands
delay(1500);
// 11. Confirm new address by requesting info
Serial.println("=== Request Info from 0x09 ===");
info = relay.requestSlaveInfo(addr);
Serial.print("Info: ");
Serial.println(info);
Serial.println("=== Test Complete ===");
}
void loop() {
if (Serial.available()) {
String cmd = Serial.readStringUntil('\n');
cmd.trim();
// Replace "id" with "i" here:
if (cmd == "i") {
String id = relay.getIdentification();
Serial.print("ID: ");
Serial.println(id);
} else {
relay.handleSerialCommand(cmd);
}
}
// Nothing here
}