i have 4 relay and 4 buttons
I want to push 1 switch 1 time 1 relay on and 3 relays or relay 2,3,4 automatic off and push 2 the relay 1,3,4 automatic off
You can refer to Arduino - Button toggles relay. For multiple buttons, you can refer to how to use the button array
const byte ButtonPin1 = 3;
const byte ButtonPin2 = 7;
const byte ButtonPin3 = 4;
const byte ButtonPin4 = 6;
const byte RelayPin1 = 2;
const byte RelayPin2 = 5;
const byte RelayPin3 = 8;
const byte RelayPin4 = 9;
const int ButtonPressed = LOW;
const int RelayOn = LOW;
const int RelayOff = HIGH;
void setup()
{
pinMode(ButtonPin1, INPUT_PULLUP);
pinMode(ButtonPin2, INPUT_PULLUP);
pinMode(ButtonPin3, INPUT_PULLUP);
pinMode(ButtonPin4, INPUT_PULLUP);
}
void setRelay(int relayPin, boolean buttonPressed)
{
if (buttonPressed)
{
digitalWrite(relayPin, RelayOn);
}
else
{
digitalWrite(relayPin, RelayOff);
}
}
void loop()
{
int currentButton;
// Find the first button that is pressed, if any
if (digitalRead(ButtonPin1) == ButtonPressed)
currentButton = 1;
else if (digitalRead(ButtonPin2) == ButtonPressed)
currentButton = 2;
else if (digitalRead(ButtonPin3) == ButtonPressed)
currentButton = 3;
else if (digitalRead(ButtonPin4) == ButtonPressed)
currentButton = 4;
else
currentButton = 0;
// If a button is pressed, set the relays to match
if (currentButton != 0)
{
setRelay(RelayPin1, (currentButton == 1));
setRelay(RelayPin2, (currentButton == 2));
setRelay(RelayPin3, (currentButton == 3));
setRelay(RelayPin4, (currentButton == 4));
}
}
i appreciate your help
the relay not working
Noong Lun, Ago 29, 2022 nang 11:32 PM, sinulat ni johnwasser via Arduino Forum <notifications@arduino.discoursemail.com> ang:
You did not say how the buttons and relays are wired so I had to guess. Maybe I guessed wrong.
How the wiring set up .maybe my diagram and wire wrong
how the buttons and relays are wired
Hello limor91
Take a view.
Many thanks to LarryD
Have a nice day and enjoy programming in C++ and learning.
MIND THE GAP
thank you ..
thank you ..
Hello limor91
See below a small example of a sketch using class-less OOP.
I´ve made an object containing all relevant information like buttonPin, relayPin and etc.
You can add buttons and relays as you like without changing the related service.
Try it and play around to learn.
/* BLOCK COMMENT
ATTENTION: This Sketch contains elements of C++.
https://www.learncpp.com/cpp-tutorial/
Many thanks to LarryD
https://europe1.discourse-cdn.com/arduino/original/4X/7/e/0/7e0ee1e51f1df32e30893550c85f0dd33244fb0e.jpeg
https://forum.arduino.cc/t/can-you-help-me-i-want-to-push-1-switch-1-time-1-relay-on-and-the-3-relays-automatic-off/1026635
Tested with Arduino: Mega[x] - UNO [ ] - Nano [ ]
*/
#define ProjectName "Can you help me I want to push 1 switch 1 time 1 relay on, and the 3 relays automatic off "
// HARDWARE AND TIMER SETTINGS
constexpr unsigned long Blink512Hz {9}; // blinkrate for heartbeat function
// VARIABLE DECLARATION AND DEFINITION
unsigned long currentTime;
// -- objects -----------------------------------------
struct BUTTONRELAY {
byte buttonPin;
int stateOld;
byte relayPin;
unsigned long duration;
unsigned long stamp;
};
BUTTONRELAY buttonRelays[] {
{ A0, false, 8, 20, 0},
{ A1, false, 9, 20, 0},
{ A2, false, 10, 20, 0},
{ A3, false, 11, 20, 0},
};
void heartBeat(int rate)
{
bool myBlink {currentTime & bit(rate)};
digitalWrite(LED_BUILTIN, myBlink);
}
// -------------------------------------------------------------------
void setup() {
Serial.begin(9600);
Serial.println(F("."));
Serial.print(F("File : ")), Serial.println(__FILE__);
Serial.print(F("Date : ")), Serial.println(__DATE__);
Serial.print(F("Project: ")), Serial.println(ProjectName);
pinMode (LED_BUILTIN, OUTPUT); // used as heartbeat indicator
// https://www.learncpp.com/cpp-tutorial/for-each-loops/
for (auto buttonRelay : buttonRelays) pinMode(buttonRelay.buttonPin, INPUT_PULLUP), pinMode(buttonRelay.relayPin, OUTPUT);
}
void loop () {
currentTime = millis();
heartBeat(Blink512Hz);
for (auto &buttonRelay : buttonRelays) {
if (currentTime - buttonRelay.stamp >= buttonRelay.duration) {
buttonRelay.stamp = currentTime;
int stateNew = !digitalRead(buttonRelay.buttonPin);
if (buttonRelay.stateOld != stateNew) {
buttonRelay.stateOld = stateNew;
if (stateNew) {
for (auto buttonRelay : buttonRelays) digitalWrite(buttonRelay.relayPin, LOW);
digitalWrite(buttonRelay.relayPin, HIGH);
}
}
}
}
}
Have a nice day and enjoy programming in C++ and learning.
MIND THE GAP
thank you for the sharing code it working ...i have one question if i added next press turning off the relay
To toggle you will have to add StateChangeDetection (see the built-in example of the same name) and will probably want to add debounce. Then you will need to keep track of if the relay is on or off so you can turn the relay on if it is off and off if it is on.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.