Dears,
I have a problem that I could not solve for a while, I tried the code below but it did not work. There will be 4 buttons controlling 4 relays but in a way that button one will turn relays 1&3 on, if button 2 is pressed only relay 2 turns on, if button 3 is pressed relays 1,2 and 3 will turn on, if button 4 is pressed it is supposed to toggle relays 1&4. one more thing: I need to be able to pass from any button to any of the others and the corresponding relays will be activated while the others will go off.
#include <Bounce2.h>
const int BUTTON_COUNT = 4;
const int16_t DEBOUNCE_DELAY = 50;
const uint8_t buttonPins[BUTTON_COUNT] = {28, 29, 30, 31};
const uint8_t ledPins[BUTTON_COUNT] = {34, 35, 36, 37};
Bounce buttons[BUTTON_COUNT];
uint8_t ledStates[BUTTON_COUNT];
void setup() {
for (int i = 0; i < BUTTON_COUNT; i++) {
buttons[i].attach(buttonPins[i], INPUT);
buttons[i].interval(DEBOUNCE_DELAY);
pinMode(ledPins[i], OUTPUT);
ledStates[i] = HIGH;
digitalWrite(ledPins[i], ledStates[i]);
}
}
void loop() {
// Update the debouncers.
for (int i = 0; i < BUTTON_COUNT; i++)
buttons[i].update();
// When the first "laban" button is pressed:
if (buttons[0].rose()) {
// Light up the first LED.
digitalWrite(ledPins[1], LOW);
digitalWrite(ledPins[3], LOW);
// and turn off all the others.
for (int i = 0; i < BUTTON_COUNT; i++) {
ledStates[0] = HIGH;
ledStates[2] = HIGH;
digitalWrite(ledPins[i], ledStates[i]);
}
}
// When the first button is released:
if (buttons[0].fell()) {
// turn off relays.
digitalWrite(ledPins[1], HIGH);
digitalWrite(ledPins[3], HIGH);
}
// When the first "Soda" button is pressed:
if (buttons[1].rose()) {
// Light up the first LED.
digitalWrite(ledPins[2], LOW);
// and turn off all the others.
for (int i = 0; i < BUTTON_COUNT; i++) {
ledStates[0] = HIGH;
ledStates[1] = HIGH;
ledStates[3] = HIGH;
digitalWrite(ledPins[i], ledStates[i]);
}
}
// When the first button is released:
if (buttons[1].fell()) {
// turn off the first LED.
digitalWrite(ledPins[2], HIGH);
}
// When the first "labanUP" button is pressed:
if (buttons[2].rose()) {
// Light up the first LED.
digitalWrite(ledPins[1], LOW);
digitalWrite(ledPins[2], LOW);
digitalWrite(ledPins[3], LOW);
// and turn off all the others.
for (int i = 0; i < BUTTON_COUNT; i++) {
ledStates[0] = HIGH;
digitalWrite(ledPins[i], ledStates[i]);
}
}
// When the first button is released:
if (buttons[0].fell()) {
// turn off the first LED.
digitalWrite(ledPins[1], HIGH);
digitalWrite(ledPins[2], HIGH);
digitalWrite(ledPins[3], HIGH);
}
// When circulation button is pressed:
if (buttons[3].rose()) {
// Toggle its state.
ledStates[3] = !ledStates[3];
digitalWrite(ledPins[3], ledStates[3]);
digitalWrite(ledPins[0], ledStates[0]);
}
}