ja und hast du mittlerweile rausgefunden was in deiner enumeration für Nine drinnen steht?
gib es dir auf der Seriellen Schnittstelle aus.
Serial.println(Nine).
Und jetzt überleg mal was aus dieser Anweisung wird:
digitalWrite(radioButtons1[radioButton1.name_1].relayPin1, On);
na?
verstehst du deinen Fehler jetzt?
edit:
quick und dirty könntest du das enum teilen, dann beginnt Nine wieder mit 0 und es passt zu den Array Index.
// https://forum.arduino.cc/t/a-b-umschalter-mit-taster/934347/
enum {On, Off}; // low active relay type
// CONSTANT DEFINITION
enum {One, Two, Three, Four, Five, Six, Seven, Eight}; // button names
enum {Nine, Ten, Eleven, Twelve, Thirteen, Fourteen, Fiveteen, Sixteen}; // button names
// VARIABLE DECLARATION AND DEFINITION
unsigned long currentTime;
struct RADIOBUTTON {
byte name_; // button name
byte buttonPin; // portPin o---|button|---GND
int statusQuo; // current pin state
byte relayPin; // portPin o---|relay shield|---GND
unsigned long stamp; // time information for debouncing
unsigned long duration; // time information for debouncing
} radioButtons [] {
{One, A8, false, 22, 0, 20},
{Two, A9, false, 23, 0, 20},
{Three, A10, false, 24, 0, 20},
{Four, A11, false, 25, 0, 20},
{Five, A12, false, 26, 0, 20},
{Six, A13, false, 27, 0, 20},
{Seven, A14, false, 28, 0, 20},
{Eight, A15, false, 29, 0, 20},
};
RADIOBUTTON radioButtons1 [] {
{Nine, A0, false, 30, 0, 20},
{Ten, A1, false, 31, 0, 20},
{Eleven, A2, false, 32, 0, 20},
{Twelve, A3, false, 33, 0, 20},
{Thirteen, A4, false, 34, 0, 20},
{Fourteen, A5, false, 35, 0, 20},
{Fiveteen, A6, false, 36, 0, 20},
{Sixteen, A7, false, 37, 0, 20},
};
// -------------------------------------------------------------------
void setup() {
pinMode(LED_BUILTIN, OUTPUT); // hat meines erachtens im Ausgangssketch gefehlt
for (auto &radioButton : radioButtons) {
pinMode(radioButton.buttonPin, INPUT_PULLUP);
digitalWrite(radioButton.relayPin, Off);
pinMode(radioButton.relayPin, OUTPUT);
}
digitalWrite(radioButtons[One].relayPin, On);
for (auto &radioButton1 : radioButtons1) {
pinMode(radioButton1.buttonPin, INPUT_PULLUP);
digitalWrite(radioButton1.relayPin, Off);
pinMode(radioButton1.relayPin, OUTPUT);
}
digitalWrite(radioButtons1[Nine].relayPin, On);
}
void loop () {
currentTime = millis();
digitalWrite(LED_BUILTIN, (currentTime / 500) % 2);
for (auto &radioButton : radioButtons) {
if (currentTime - radioButton.stamp - radioButton.duration) {
radioButton.stamp = currentTime;
int stateNew = !digitalRead(radioButton.buttonPin);
if (radioButton.statusQuo != stateNew) {
radioButton.statusQuo = stateNew;
if (radioButton.statusQuo) {
for (auto &radioButton : radioButtons) digitalWrite(radioButton.relayPin, Off);
digitalWrite(radioButtons[radioButton.name_].relayPin, On);
}
}
}
}
// dirty duplicate code now...
for (auto &radioButton : radioButtons1) {
if (currentTime - radioButton.stamp - radioButton.duration) {
radioButton.stamp = currentTime;
int stateNew = !digitalRead(radioButton.buttonPin);
if (radioButton.statusQuo != stateNew) {
radioButton.statusQuo = stateNew;
if (radioButton.statusQuo) {
for (auto &radioButton : radioButtons) digitalWrite(radioButton.relayPin, Off);
digitalWrite(radioButtons[radioButton.name_].relayPin, On);
}
}
}
}
}
ungetestet, 2282Flash/221SRAM am Mega.
Du siehst, es gibt nur EINE Struktur. Jetzt mit zwei Instanzen. Daher brauchen die Mmember Variablen kein Anhängsel 1.
Die Lösung ist aber nicht besonders schön, weils noch immer duplizierten Code gibt.
Alternativ:
Wenn du nun die Anweisungen im Loop zu einer Member function machst, wäre es nur mehr einmal im Sketch.
Wir machen also aus der Struktur eine Klasse.
Weiter gedacht.
die 8 Buttons/Relays bilden eine logische Einheit - sie entriegeln jeweils die anderen Ausgänge
Das heißt wir Modellieren jetzt eine Klasse die 8 Relais und 8 Buttons als Einheit verwaltet.
16 pins an ein Objekt übergeben schaut zwar graußig aus, aber das ist dem Controller egal.
"Entprellen" brauchst meines erachtens Taster nicht, wenn sie nur etwas aktivieren sollen.
Als "Goodie" legen wir dann nicht zwei separate Instanzen (objekte) mit radioButton und radioButton1 an, sondern wir machen uns ein ARRAY von radiobutton.
Wir haben jetzt also zwei Objekte: radioButton[0] und radioButton[1]. Das können wir jeweils in Forschleifen schön durchackern.
für jede weitere 8er Einheit braucht es eine einzige Zeile - nämlich die Übergabe der 16 Pins. Bzw. wenn man individuelle "Buttonnamen" in einem enum haben will halt auch noch ein Enum.
// https://forum.arduino.cc/t/a-b-umschalter-mit-taster/934347/
// CONSTANT DEFINITION
enum {One, Two, Three, Four, Five, Six, Seven, Eight}; // button names
enum {Nine, Ten, Eleven, Twelve, Thirteen, Fourteen, Fiveteen, Sixteen}; // button names
constexpr byte ON = LOW; // low active button
constexpr byte OFF = HIGH;
constexpr byte noOfButtons = 8; // buttons/relays per group
unsigned long currentTime;
class Radiobutton {
public:
byte currentState = 0; // current active output
const byte buttonPin[noOfButtons]; // portPin o---|button|---GND
const byte relayPin[noOfButtons]; // portPin o---|relay shield|---GND
Radiobutton (byte buttonPin0, byte buttonPin1, byte buttonPin2, byte buttonPin3, byte buttonPin4, byte buttonPin5, byte buttonPin6, byte buttonPin7,
byte relayPin0, byte relayPin1, byte relayPin2, byte relayPin3, byte relayPin4, byte relayPin5, byte relayPin6, byte relayPin7 ) :
buttonPin{buttonPin0, buttonPin1, buttonPin2, buttonPin3, buttonPin4, buttonPin5, buttonPin6, buttonPin7, },
relayPin {relayPin0, relayPin1, relayPin2, relayPin3, relayPin4, relayPin5, relayPin6, relayPin7,}
{}
void begin()
{
for (byte i = 0; i < noOfButtons; i++)
{
if (buttonPin[i] != 255) {
pinMode(buttonPin[i], INPUT_PULLUP);
digitalWrite(buttonPin[i], OFF);
pinMode(relayPin[i], OUTPUT);
}
}
//set(0); // optional init with an active pin
}
void set(byte newState)
{
currentState = newState;
for (byte i = 0; i < noOfButtons; i++)
{
if (currentState == i) digitalWrite(relayPin[i], HIGH); else digitalWrite(relayPin[i], LOW); // evtl. LOW/HIGH tauschen falls man low active Relais hat (ich habe aber high active LEDs
}
}
void update()
{
for (byte i = 0; i < noOfButtons; i++)
{
if (digitalRead(buttonPin[i]) == ON && currentState != i) // only on change
{
set(i);
}
}
}
};
Radiobutton radioButton[] {
// 8 input pins 8 output pins
{A8, A9, A10, A11, A12, A13, A14, A15, 22, 23, 24, 25, 26, 27, 28, 29},
{A0, A1, A2, A3, A4, A5, A6, A7, 30, 31, 32, 33, 34, 35, 36, 37}
};
/*
// mein Test
Radiobutton radioButton[] {
// 8 input pins 8 output pins
{A0, A1, 255, 255, 255, 255, 255, 255, 2, 3, 255, 255, 255, 255, 255, 255, },
{A2, A3, 255, 255, 255, 255, 255, 255, 4, 5, 255, 255, 255, 255, 255, 255, }
};
*/
// -------------------------------------------------------------------
void setup() {
pinMode(LED_BUILTIN, OUTPUT); // hat meines erachtens im Ausgangssketch gefehlt
for (auto &i : radioButton) i.begin();
radioButton[0].set(One); // optional activate one
radioButton[1].set(Nine);
}
void loop () {
digitalWrite(LED_BUILTIN, (millis()/ 500) % 2);
for (auto &i : radioButton) i.update();
}
getestet, 1954 Flash/43 byte SRAM am Mega.
300 byte weniger Flash, nur ein fünftel SRAM
Und der loop nur mehr ein Zweizeiler 