Hello plauborg
Welcome to the best ever Arduino Forum.
Here is a simple example that uses classless object-oriented programming.
The mirco switches are declared in the INPUTS object and then initialised. The pattern for the order of the switches is set in the SEQUENCE object.
In this way, you can easily change both the set of micro-switches and the pattern for the order of the buttons without changing the programme code.
Try it out. You only need to adapt the port pin addresses to your project.
/* 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/correct-sequence-order-of-input/1019627
Tested with Arduino: Mega[x] - UNO [ ] - Nano [ ]
*/
#define ProjectName "Correct sequence order of input"
// HARDWARE AND TIMER SETTINGS
// YOU MAY NEED TO CHANGE THESE CONSTANTS TO YOUR HARDWARE AND NEEDS
constexpr byte MicroSwitches[] {A0, A1, A2, A3}; // portPin o---|button|---GND
constexpr byte LedPins[] {9}; // portPin o---|220|---|LED|---GND
constexpr unsigned long Blink512Hz {9}; // blinkrate for heartbeat function
#define OutPutTest
constexpr unsigned long OutPutTestTime {1000};
// CONSTANT DEFINITION
enum InputOutput {One, Two, Three, Four};
// VARIABLE DECLARATION AND DEFINITION
unsigned long currentTime;
// -- objects -----------------------------------------
struct TIMER { // has the following members
unsigned long duration; // memory for interval time
unsigned long stamp; // memory for actual time
int onOff; // control for stop/start/repeat
};
struct BUTTON { // has the following members
byte pin; // port pin
bool statusOld; // current state
TIMER scan; // see timer struct
};
struct INPUTS {
int ident;
BUTTON knop;
} inputs [] {
{One, MicroSwitches[One], false, 20, 0, false},
{Two, MicroSwitches[Two], false, 20, 0, false},
{Three, MicroSwitches[Three], false, 20, 0, false},
{Four, MicroSwitches[Four], false, 20, 0, false},
};
struct SEQUENCE {
int counter;
int pattern[4];
}sequence {0,{One,Two,Three,Four}};
int sequenceSize {(sizeof(sequence.pattern)/sizeof(sequence.pattern[0])-1)};
// -- services -----------------------------------------
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 MicroSwitch : MicroSwitches) pinMode(MicroSwitch, INPUT_PULLUP);
for (auto LedPin : LedPins) pinMode(LedPin, OUTPUT);
#ifdef OutPutTest
// check outputs
for (auto LedPin : LedPins) digitalWrite(LedPin, HIGH), delay(OutPutTestTime);
for (auto LedPin : LedPins) digitalWrite(LedPin, LOW), delay(OutPutTestTime);
#endif
}
void loop () {
currentTime = millis();
heartBeat(Blink512Hz);
for (auto &input : inputs)
{
if (currentTime - input.knop.scan.stamp >= input.knop.scan.duration)
{
input.knop.scan.stamp=currentTime;
int statusNew = !digitalRead(input.knop.pin);
if (input.knop.statusOld != statusNew)
{
input.knop.statusOld = statusNew;
if (statusNew)
{
if (sequence.pattern[sequence.counter]==input.ident)
{
if (sequence.counter==sequenceSize)
{
digitalWrite(LedPins[One],HIGH);
sequence.counter=0;
}
else
{
sequence.counter++;
digitalWrite(LedPins[One],LOW);
}
}
else
{
digitalWrite(LedPins[One],LOW);
sequence.counter=0;
}
}
}
}
}
}
Have a nice day and enjoy programming in C++ and learning.
MIND THE GAP