// const int SWT1 = 22; // the number of the pushbutton pin
// const int SWT2 = 24;
// const int SWT3 = 26;
// const int SWT4 = 28;
// const int SWT5 = 30;
// const int SWT6 = 32;
// const int SWT7 = 34;
// const int SWT8 = 36;
// const int SWT9 = 38;
// const int SWT10 = 40;
// const int SWT11 = 42;
// const int SWT12 = 44;
// const int SWT13 = 46;
// const int SWT14 = 48;
// const int SWT15 = 50;
// const int SWT16 = 52;
// const int LED1 = 23;
// const int LED2 = 25;
// const int LED3 = 27;
// const int LED4 = 29;
// const int LED5 = 31;
// const int LED6 = 33;
// const int LED7 = 35;
// const int LED8 = 37;
// const int LED9 = 39;
// const int LED10 = 41;
// const int LED11 = 43;
// const int LED12 = 45;
// const int LED13 = 47;
// const int LED14 = 49;
// const int LED15 = 51;
// const int LED16 = 53;
// int start;
// int lastMillis;
// void setup() {
// // initialize serial communication at 9600 bits per second:
// Serial.begin(9600);
// pinMode(SWT1, INPUT_PULLUP);
// pinMode(SWT2, INPUT_PULLUP);
// pinMode(SWT3, INPUT_PULLUP);
// pinMode(SWT4, INPUT_PULLUP);
// pinMode(SWT5, INPUT_PULLUP);
// pinMode(SWT6, INPUT_PULLUP);
// pinMode(SWT7, INPUT_PULLUP);
// pinMode(SWT8, INPUT_PULLUP);
// pinMode(SWT9, INPUT_PULLUP);
// pinMode(SWT10, INPUT_PULLUP);
// pinMode(SWT11, INPUT_PULLUP);
// pinMode(SWT12, INPUT_PULLUP);
// pinMode(SWT13, INPUT_PULLUP);
// pinMode(SWT14, INPUT_PULLUP);
// pinMode(SWT15, INPUT_PULLUP);
// pinMode(SWT16, INPUT_PULLUP);
// pinMode(LED1, OUTPUT);
// pinMode(LED2, OUTPUT);
// pinMode(LED3, OUTPUT);
// pinMode(LED4, OUTPUT);
// pinMode(LED5, OUTPUT);
// pinMode(LED6, OUTPUT);
// pinMode(LED7, OUTPUT);
// pinMode(LED8, OUTPUT);
// pinMode(LED9, OUTPUT);
// pinMode(LED10, OUTPUT);
// pinMode(LED11, OUTPUT);
// pinMode(LED12, OUTPUT);
// pinMode(LED13, OUTPUT);
// pinMode(LED14, OUTPUT);
// pinMode(LED15, OUTPUT);
// pinMode(LED16, OUTPUT);
// }
// void loop() {
// if (!digitalRead(SWT1))
// {
// if (start == false) // do we want to start the LED on/off cycle?
// {
// lastMillis = millis(); // initialize the timing
// digitalWrite(LED1, HIGH); // turn LED on
// start = true; // this prevents reinitializing lastMillis
// }
// }
// if ( millis() - lastMillis >= 20UL) // if we in in checking mode, has the time expired?
// {
// digitalWrite(LED1, LOW); // turn led off
// start = false;
// while(!digitalRead(SWT1)){}
// delay(1); // For debouncing
// }
// if (!digitalRead(SWT2))
// {
// if (start == false) // do we want to start the LED on/off cycle?
// {
// lastMillis = millis(); // initialize the timing
// digitalWrite(LED2, HIGH); // turn LED on
// start = true; // this prevents reinitializing lastMillis
// }
// }
// if ( millis() - lastMillis >= 20UL) // if we in in checking mode, has the time expired?
// {
// digitalWrite(LED2, LOW); // turn led off
// start = false;
// while(!digitalRead(SWT2)){}
// delay(1); // For debouncing
// }
// if (!digitalRead(SWT3))
// {
// if (start == false) // do we want to start the LED on/off cycle?
// {
// lastMillis = millis(); // initialize the timing
// digitalWrite(LED3, HIGH); // turn LED on
// start = true; // this prevents reinitializing lastMillis
// }
// }
// if ( millis() - lastMillis >= 20UL) // if we in in checking mode, has the time expired?
// {
// digitalWrite(LED3, LOW); // turn led off
// start = false;
// while(!digitalRead(SWT3)){}
// delay(1); // For debouncing
// }
// if (!digitalRead(SWT4))
// {
// if (start == false) // do we want to start the LED on/off cycle?
// {
// lastMillis = millis(); // initialize the timing
// digitalWrite(LED4, HIGH); // turn LED on
// start = true; // this prevents reinitializing lastMillis
// }
// }
// if ( millis() - lastMillis >= 20UL) // if we in in checking mode, has the time expired?
// {
// digitalWrite(LED4, LOW); // turn led off
// start = false;
// while(!digitalRead(SWT4)){}
// delay(1); // For debouncing
// }
// if (!digitalRead(SWT5))
// {
// if (start == false) // do we want to start the LED on/off cycle?
// {
// lastMillis = millis(); // initialize the timing
// digitalWrite(LED5, HIGH); // turn LED on
// start = true; // this prevents reinitializing lastMillis
// }
// }
// if ( millis() - lastMillis >= 20UL) // if we in in checking mode, has the time expired?
// {
// digitalWrite(LED5, LOW); // turn led off
// start = false;
// while(!digitalRead(SWT5)){}
// delay(1); // For debouncing
// }
// if (!digitalRead(SWT6))
// {
// if (start == false) // do we want to start the LED on/off cycle?
// {
// lastMillis = millis(); // initialize the timing
// digitalWrite(LED6, HIGH); // turn LED on
// start = true; // this prevents reinitializing lastMillis
// }
// }
// if ( millis() - lastMillis >= 20UL) // if we in in checking mode, has the time expired?
// {
// digitalWrite(LED6, LOW); // turn led off
// start = false;
// while(!digitalRead(SWT6)){}
// delay(1); // For debouncing
// }
// if (!digitalRead(SWT7))
// {
// if (start == false) // do we want to start the LED on/off cycle?
// {
// lastMillis = millis(); // initialize the timing
// digitalWrite(LED7, HIGH); // turn LED on
// start = true; // this prevents reinitializing lastMillis
// }
// }
// if ( millis() - lastMillis >= 20UL) // if we in in checking mode, has the time expired?
// {
// digitalWrite(LED7, LOW); // turn led off
// start = false;
// while(!digitalRead(SWT7)){}
// delay(1); // For debouncing
// }
// // if (!digitalRead(SWT8))
// // {
// // if (start == false) // do we want to start the LED on/off cycle?
// // {
// // lastMillis = millis(); // initialize the timing
// // digitalWrite(LED8, HIGH); // turn LED on
// // start = true; // this prevents reinitializing lastMillis
// // }
// // }
// // if ( millis() - lastMillis >= 20UL) // if we in in checking mode, has the time expired?
// // {
// // digitalWrite(LED8, LOW); // turn led off
// // start = false;
// // while(!digitalRead(SWT8)){}
// // delay(1); // For debouncing
// // }
// }
//https://forum.arduino.cc/t/one-shot-without-delay/1225555
//https://europe1.discourse-cdn.com/arduino/original/4X/7/e/0/7e0ee1e51f1df32e30893550c85f0dd33244fb0e.jpeg
#define ProjectName "One Shot without DELAY"
#define NotesOnRelease "Arduino MEGA tested"
// make names
enum TimerEvent {NotExpired, Expired};
enum TimerControl {Halt, Run};
enum ButtonControl {Released, Pressed};
enum OnOff {Off, On};
// make variables
constexpr uint8_t ButtonPins[] {22, 24, 26, 28};
constexpr uint8_t LedPins[] {23, 25, 27, 29};
constexpr uint32_t FlashTimes[] {10, 20, 30, 40};
constexpr uint32_t TestLedDelay {250};
uint32_t currentMillis = millis();
// make structures
struct TIMER
{
uint32_t interval;
uint8_t control;
uint32_t now;
uint8_t expired(uint32_t currentMillis)
{
uint8_t timerEvent = currentMillis - now >= interval and control;
if (timerEvent == Expired) now = currentMillis;
return timerEvent;
}
};
struct LEDFLASH
{
uint8_t button;
uint8_t stateOld;
uint8_t led;
TIMER flash;
TIMER debounce;
void make(uint8_t button_, uint8_t led_, uint32_t delay_)
{
button = button_;
pinMode(button, INPUT_PULLUP);
stateOld = digitalRead(button) ? LOW : HIGH;
led = led_,
pinMode(led, OUTPUT);
digitalWrite(led, On);
delay(TestLedDelay);
digitalWrite(led, Off);
delay(TestLedDelay);
flash.interval = delay_;
debounce.interval = 20;
debounce.control = Run;
}
void run()
{
if (debounce.expired(currentMillis) == Expired)
{
uint8_t stateNew = digitalRead(button) ? LOW : HIGH;
if (stateOld != stateNew)
{
stateOld = stateNew;
if (stateNew == Pressed)
{
digitalWrite(led, On);
flash.control = Run;
flash.now = currentMillis;
}
}
}
if (flash.expired(currentMillis) == Expired)
{
digitalWrite(led, Off);
flash.control = Halt;
}
}
} ledFlashs[sizeof(LedPins)];
// make support
void heartBeat(const uint8_t LedPin, uint32_t currentMillis)
{
static bool setUp = false;
if (setUp == false) pinMode (LedPin, OUTPUT), setUp = true;
digitalWrite(LedPin, (currentMillis / 500) % 2);
}
// make application
void setup()
{
Serial.begin(9600);
Serial.print("Source: "), Serial.println(__FILE__);
Serial.print(ProjectName), Serial.print(" - "), Serial.println(NotesOnRelease);
uint8_t element = 0;
for (auto &ledFlash : ledFlashs)
{
ledFlash.make(ButtonPins[element], LedPins[element], FlashTimes[element]);
element++;
}
Serial.println(" =-> and off we go\n");
}
void loop()
{
currentMillis = millis();
heartBeat(LED_BUILTIN, currentMillis);
for (auto &ledFlash : ledFlashs) ledFlash.run();
}