#include const int deviceAddress = 0x68; // I2C address of the device (change as needed) const uint8_t relay1 = 2; // LOW ACTIVE const uint8_t relay2 = 3; // LOW ACTIVE const uint8_t oe = 5; const uint8_t d6 = 6; // Define pin D6 bool actionPerformed = false; // Flag to track if the action has been performed void relaysOn() { //Relays are low active digitalWrite(relay1,LOW); digitalWrite(relay2,LOW); delay(5); } void relaysOff() { //Relays are low active digitalWrite(relay1,HIGH); digitalWrite(relay2,HIGH); delay(5); } // Function to transmit 16-bit data in little-endian format void transmitData(uint8_t registerAddress, uint16_t data) { digitalWrite(oe,HIGH); Wire.beginTransmission(deviceAddress); Wire.write(registerAddress); // Send the register address Wire.write(data & 0xFF); // Send the low byte (LSB) Wire.write((data >> 8) & 0xFF); // Send the high byte (MSB) Wire.endTransmission(); digitalWrite(oe,LOW); } // Function to read back data from the register uint16_t readData(uint8_t registerAddress) { uint16_t receivedData = 0; digitalWrite(oe,HIGH); // First, send the register address to read from Wire.beginTransmission(deviceAddress); Wire.write(registerAddress); // Send the register address Wire.endTransmission(); if (Wire.endTransmission() != 0) { Serial.println("Device not found"); digitalWrite(oe,LOW); return 0; // Return 0 or some error code } // Now request 2 bytes from the device Wire.requestFrom(deviceAddress, 2); if (Wire.available() == 2) { receivedData |= Wire.read(); // Read low byte (LSB) receivedData |= Wire.read() << 8; // Read high byte (MSB) } digitalWrite(oe,LOW); return receivedData; } void setup() { Serial.begin(9600); Wire.begin(); // Initialize I2C pinMode(relay1, OUTPUT); pinMode(relay2, OUTPUT); pinMode(oe, OUTPUT); pinMode(d6, INPUT); // Set D6 as input digitalWrite(oe,LOW); for(int i = 8; i <= 13; i++) { pinMode(i,OUTPUT); digitalWrite(i,LOW); } } void printHex(uint16_t value) { Serial.print("0x"); if (value < 0x10) { Serial.print("000"); } else if (value < 0x100) { Serial.print("00"); } else if (value < 0x1000) { Serial.print("0"); } Serial.println(value, HEX); // Print the value in hexadecimal } unsigned long lastDebounceTime = 0; // Last time the pin state changed unsigned long debounceDelay = 50; // Debounce delay in milliseconds int lastD6State = LOW; // Last stable state of D6 int currentD6State; // Current state of D6 void loop() { // Read the current state of D6 int reading = digitalRead(d6); // Check if the state has changed if (reading != lastD6State) { // Reset the debounce timer lastDebounceTime = millis(); } // If the state has been stable for the debounce delay, update the state if ((millis() - lastDebounceTime) > debounceDelay) { // Only proceed if the state has changed if (reading != currentD6State) { currentD6State = reading; // Only act if the new state is HIGH and the action hasn't been performed yet if (currentD6State == HIGH && !actionPerformed) { relaysOn(); uint8_t registerAddress = 0xBF; uint16_t dataToSend = 0x0000; // Transmit the data transmitData(registerAddress, dataToSend); // Transmit data // Read back data uint16_t receivedData = readData(registerAddress); // Read back data relaysOff(); Serial.println("Received data : "); // Use the method to print received data in hexadecimal format printHex(receivedData); // Print formatted received data Serial.println(); // New line for better readability // Set the action performed flag to true actionPerformed = true; } } } // If the state is LOW, reset the action performed flag if (currentD6State == LOW) { actionPerformed = false; } // Update the last stable state lastD6State = reading; }