Skip to content

Instantly share code, notes, and snippets.

@tranchausky
Created October 30, 2024 04:08
Show Gist options
  • Save tranchausky/87527007dcd6d216a70ac25028a83842 to your computer and use it in GitHub Desktop.
Save tranchausky/87527007dcd6d216a70ac25028a83842 to your computer and use it in GitHub Desktop.
switch relay 3 with interrupt -ok
{
"version": 1,
"author": "cccc",
"editor": "wokwi",
"parts": [
{ "type": "board-esp32-devkit-c-v4", "id": "esp", "top": -86.4, "left": -23.96, "attrs": {} },
{
"type": "wokwi-relay-module",
"id": "relay1",
"top": -77.4,
"left": -300.8,
"rotate": 180,
"attrs": {}
},
{
"type": "wokwi-relay-module",
"id": "relay2",
"top": 153,
"left": -310.4,
"rotate": 180,
"attrs": {}
},
{
"type": "wokwi-relay-module",
"id": "relay3",
"top": 37.8,
"left": -310.4,
"rotate": 180,
"attrs": {}
},
{
"type": "wokwi-slide-switch",
"id": "sw1",
"top": -113.6,
"left": 154.1,
"rotate": 90,
"attrs": {}
},
{
"type": "wokwi-slide-switch",
"id": "sw2",
"top": 206,
"left": 204.7,
"rotate": 90,
"attrs": {}
},
{
"type": "wokwi-slide-switch",
"id": "sw3",
"top": 110,
"left": 204.7,
"rotate": 90,
"attrs": {}
}
],
"connections": [
[ "esp:TX", "$serialMonitor:RX", "", [] ],
[ "esp:RX", "$serialMonitor:TX", "", [] ],
[ "esp:GND.2", "sw1:1", "black", [ "h28.8", "v-19.2" ] ],
[ "esp:5V", "relay1:VCC", "red", [ "h-86.25", "v-115.2" ] ],
[ "esp:GND.1", "relay1:GND", "black", [ "h-67.05", "v-77.2" ] ],
[ "sw1:2", "esp:5", "green", [ "h-9.6", "v124.9" ] ],
[ "esp:GND.2", "sw3:1", "black", [ "h28.8", "v201.6" ] ],
[ "esp:GND.2", "sw2:1", "black", [ "h28.8", "v297.6" ] ],
[ "sw3:2", "esp:17", "green", [ "h-31.4", "v-89.1", "h-68.9", "v0" ] ],
[ "sw2:2", "esp:16", "green", [ "h-117.8", "v-175.5" ] ],
[ "esp:GND.1", "relay3:GND", "black", [ "h0" ] ],
[ "esp:5V", "relay3:VCC", "red", [ "h-134.25", "v-38.4" ] ],
[ "esp:13", "relay2:IN", "green", [ "h-95.85", "v95.8" ] ],
[ "esp:12", "relay3:IN", "green", [ "h-76.65", "v-0.2" ] ],
[ "esp:14", "relay1:IN", "green", [ "h-19.05", "v-105.8" ] ],
[ "esp:5V", "relay2:VCC", "red", [ "h-86.25", "v76.8" ] ],
[ "esp:GND.1", "relay2:GND", "black", [ "h-67.05", "v114.8" ] ]
],
"dependencies": {}
}
#define BUTTON_PIN_1 5
#define BUTTON_PIN_2 17
#define BUTTON_PIN_3 16
const int RELAY_1 = 14;
const int RELAY_2 = 12;
const int RELAY_3 = 13;
volatile bool buttonPressed[3] = {false, false, false}; // Flags to indicate button press
unsigned long lastDebounceTime[3] = {0, 0, 0}; // Debounce timers for each button
const unsigned long debounceDelay = 500; // Debounce delay in milliseconds
const int buttonPins[3] = {BUTTON_PIN_1, BUTTON_PIN_2, BUTTON_PIN_3}; // Store button pins
const int relayPins[3] = {RELAY_1, RELAY_2, RELAY_3}; // Store relay pins
void setup() {
Serial.begin(115200);
pinMode(BUTTON_PIN_1, INPUT_PULLUP);
pinMode(BUTTON_PIN_2, INPUT_PULLUP);
pinMode(BUTTON_PIN_3, INPUT_PULLUP);
pinMode(RELAY_1, OUTPUT);
pinMode(RELAY_2, OUTPUT);
pinMode(RELAY_3, OUTPUT);
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN_1), myInterruptFunction1, CHANGE);
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN_2), myInterruptFunction2, CHANGE);
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN_3), myInterruptFunction3, CHANGE);
}
void loop() {
// Handle the button press flags and change relay state
for (int i = 0; i < 3; i++) {
if (buttonPressed[i]) {
buttonPressed[i] = false; // Reset the flag
toggleRelay(i); // Toggle the corresponding relay
}
}
if (Serial.available() > 0) {
String input = Serial.readStringUntil('\n'); // Read the input until newline character
// Check for specific values
if (input == "1") {
Serial.println("You entered '1'");
digitalWrite(relayPins[0], 1);
} else if (input == "2") {
Serial.println("You entered '2'");
digitalWrite(relayPins[0], 0);
} else if (input == "34") {
Serial.println("You entered '34'");
} else if (input == "4") {
Serial.println("You entered '4'");
} else {
Serial.println("Unknown input");
}
Serial.println("Type another number (1, 2, 34, or 4) and press Enter:");
}
}
void myInterruptFunction1() {
handleButtonInterrupt(0, BUTTON_PIN_1);
}
void myInterruptFunction2() {
handleButtonInterrupt(1, BUTTON_PIN_2);
}
void myInterruptFunction3() {
handleButtonInterrupt(2, BUTTON_PIN_3);
}
void handleButtonInterrupt(int index, int pin) {
unsigned long currentTime = millis();
if (currentTime - lastDebounceTime[index] > debounceDelay) { // Check debounce time
lastDebounceTime[index] = currentTime; // Update debounce time
buttonPressed[index] = true; // Set the button press flag
}
}
void toggleRelay(int index) {
int currentRelayState = digitalRead(relayPins[index]);
digitalWrite(relayPins[index], !currentRelayState); // Toggle relay
Serial.print("Relay ");
Serial.print(index + 1);
Serial.print(" is now ");
Serial.println(!currentRelayState ? "ON" : "OFF");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment