#include #define I2C_ADDRESS 8 #define BUTTON_PIN 4 #define SOLENOID_PIN 1 bool switchHasToggled = false; unsigned long activationTimestamp = 0; bool solenoidState = false; void setup() { Wire.begin(I2C_ADDRESS); // join i2c bus with address defined in I2C_ADDRESS Wire.onRequest(requestEvent); // register event pinMode(BUTTON_PIN, INPUT_PULLUP); pinMode(SOLENOID_PIN, OUTPUT); digitalWrite(SOLENOID_PIN, LOW); } void loop() { unsigned long currentTime = millis(); if (digitalRead(BUTTON_PIN) == LOW && currentTime - activationTimestamp > 800) { switchHasToggled = true; activationTimestamp = currentTime; digitalWrite(SOLENOID_PIN, HIGH); solenoidState = true; } if (currentTime - activationTimestamp > 500 && solenoidState == true) { digitalWrite(SOLENOID_PIN, LOW); solenoidState = false; } } // function that executes whenever data is requested by master // this function is registered as an event, see setup() void requestEvent() { if (switchHasToggled) { Wire.write('a'); switchHasToggled = false; } else { Wire.write('b'); } //Wire.write("hello "); // respond with message of 6 bytes // as expected by master }