Hello, I tried searching but couldn't phrase or find what I need help with.
I have 8 momentary switches which are used as toggles. Each switch toggles a relay on/off
My code right now for each btn/relay combo look like this:
int btn1 = 2;
int ch1 = 14;
int state1 = HIGH;
int reading1;
int previous1 = LOW;
long time = 0;
long debounce = 200;
void setup() {
// put your setup code here, to run once:
pinMode (ch1, OUTPUT);
pinMode (btn1, INPUT_PULLUP);
void loop()
{
reading1 = digitalRead(btn1);
// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading1 == HIGH && previous1 == LOW && millis() - time > debounce) {
if (state1 == HIGH)
state1 = LOW;
else
state1 = HIGH;
time = millis();
}
digitalWrite(LED, state1);
previous1 = reading1;
And it works as intended but to control 8 btn/relays i have just duplicated the function 8 times and renamed each state with the next number like so:
reading1 = digitalRead(btn1);
// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading1 == HIGH && previous1 == LOW && millis() - time > debounce) {
if (state1 == HIGH)
state1 = LOW;
else
state1 = HIGH;
time = millis();
}
digitalWrite(ch1, state1);
previous1 = reading1;
//Next Channel/BTN
reading2 = digitalRead(btn2);
if (reading2 == HIGH && previous2 == LOW && millis() - time > debounce) {
if (state2 == HIGH)
state2 = LOW;
else
state2 = HIGH;
time = millis();
}
digitalWrite(ch2, state2);
previous2 = reading2;
//Next Channel/BTN
reading3 = digitalRead(btn3);
if (reading3 == HIGH && previous3 == LOW && millis() - time > debounce) {
if (state3 == HIGH)
state3 = LOW;
else
state3 = HIGH;
time = millis();
}
digitalWrite(ch3, state3);
previous3 = reading3;
How could I do this with an array instead? Im very new to this.
Guys this is so nice, thanks to all of you I will try your code out and write here. I just have to say, it is so nice I can just come here with an issue my smooth brain cant solve and 3 people within hours have solutions just like that. Thanks for your time
Hello
I also have another sketch.
It uses a classless OOP approach. An object has been formed from the combination of switch and relay, which can be extended as desired.
[code]
/* BLOCK COMMENT
ATTENTION: This Sketch contains elements of C++.
https://www.learncpp.com/cpp-tutorial/
https://forum.arduino.cc/t/using-array-to-toggle-multiple-momentary-switches/938405
*/
#define ProjectName "Using array to toggle multiple momentary switches"
// HARDWARE AND TIMER SETTINGS
// YOU MAY NEED TO CHANGE THESE CONSTANTS TO YOUR HARDWARE AND NEEDS
#define OutPutTest
constexpr unsigned long ScanTime {20};
constexpr unsigned long OutPutTestTime {500};
struct BUTTONRELAY {
byte buttonPin; // portPin o---|button|---GND
byte relayPin; // portPin o---|relay|---GND
int statusQuo; // state (t-1)
unsigned long stamp;
} buttonRelays [] { // add button and relay combinations as you like
{A0, 3, false, 0},
{A1, 5, false, 0},
{A2, 6, false, 0},
{A3, 7, false, 0},
};
// -------------------------------------------------------------------
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
for (auto buttonRelay : buttonRelays) {
pinMode(buttonRelay.buttonPin, INPUT_PULLUP);
pinMode(buttonRelay.relayPin, OUTPUT);
#ifdef OutPutTest // check outputs
digitalWrite(buttonRelay.relayPin, HIGH), delay(OutPutTestTime), digitalWrite(buttonRelay.relayPin, LOW);
#endif
}
}
void loop () {
unsigned long currentTime = millis();
digitalWrite(LED_BUILTIN, (currentTime / 500) % 2);
for (auto &buttonRelay : buttonRelays) {
if (currentTime - buttonRelay.stamp >= ScanTime) {
buttonRelay.stamp = currentTime;
int stateNew = !digitalRead(buttonRelay.buttonPin);
if (buttonRelay.statusQuo != stateNew) {
buttonRelay.statusQuo = stateNew;
if (stateNew) digitalWrite(buttonRelay.relayPin, !digitalRead(buttonRelay.relayPin));
}
}
}
}
[/code]