One Shot without DELAY

I am working with this code on UNO for a one shot or functions like a mono stable works with a very short duration of time
(Just A Tick) . .

I want to design the system without delay so going with the code below "

const int SWT1 = 3;        // the number of the pushbutton pin
const int LED1 = 2;
const int DEBOUNCE_DELAY = 10;   // the debounce time; increase if the output flickers

// Variables will change:
int lastSteadyStateA = LOW;       // the previous steady state from the input pin
int lastFlickerableStateA = LOW;  // the previous flickerable state from the input pin
int currentStateA;                // the current reading from the input pin

unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  pinMode(SWT1, INPUT_PULLUP);
  pinMode(LED1, OUTPUT);
  digitalWrite(LED1, LOW);
}

void loop() {
  // read the state of the switch/button:
  currentStateA = digitalRead(SWT1);

  if (currentStateA != lastFlickerableStateA) {
    // reset the debouncing timer
    lastDebounceTime = millis();
    // save the the last flickerable state
    lastFlickerableStateA = currentStateA;
  }

  if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY) {
    if (lastSteadyStateA == HIGH && currentStateA == LOW)
      Serial.println("The button is pressed");
      if(!digitalRead(SWT1))
      {
      digitalWrite(LED1, lastSteadyStateA);
      }
    lastSteadyStateA = currentStateA;
  }
}

Problem is :-

Its not always working
OR
I have to wait for 4 to 5 seconds to make action again

Too with fast switching of micro switch button
for 5 clicks it works for only 2 clicks hardly

I just want to remove the value
delay(10)

Please guide !!!!

The code works without "wait for 4 to 5 seconds"... check your wiring. I'm using a wire as a button, and no bounce.

2 Likes

Ya everything is fine here and with a delay it works very well
I am on it right now

just trying to modify a code obtained from one of this forum post only
Here it is . . .

const int SWT1 = 3;        // the number of the pushbutton pin
const int LED1 = 2;

int start;
int flag;
int lastMillis;

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  pinMode(SWT1, INPUT_PULLUP);
  pinMode(LED1, OUTPUT);
  // digitalWrite(LED1, HIGH);
}

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
  flag = true;                // this enables the timing checking
}
if (flag == true && millis() - lastMillis >= 20UL)  // if we in in checking mode, has the time expired?
{
  digitalWrite(LED1, LOW);  // turn led off
  flag = false;              // this disables timing checking
}
}

}

It works fine but once only

How would I repeat forever ??

LED is not always responding to the fullest thus not able to trigger something connected as extension there , say a 100 watt bulb with a triac or a motor with a driver

Just examples of extension I am telling . . . . !!!!

Issue is with proper output at every click !!

The sketch from Post #1 works all the time. LED is good, serial monitor says "button pressed," no "wait 5 seconds"... check your wiring.

1 Like

Please let me know where to check exactly and what changes or update I need ??

I just checked it on Serial Monitor
It shows result but the LED is not always glowing and at the same moment any other load connected is also not working
Don't know whats going on actually !!!

You did not program the LED to always glow.
You did not mention any other "load."

1 Like

load is external circuit or any board like for example a motor driver an LED driver or anything

How I dint program LED to always glow on every click

I just want it to glow for a fraction of time period decided

"Just A Full On Blink"

It's not clear to me what you need.

So far I understand:
You want your project to be a single shot at the push of a button.
From here the questions remain:

When you fire the shot, you want it to be ready again for another shot, or you want it to prevent another shot.

If you want a new shot,
want it to be immediately ready for another shot,
or do you want there to be time between the end of a shot and the possibility of the next shot?

Be clearer about your difficulty.

Shot is going well on board and serial monitors
But all the time LED connected to PIN number 2 is not glowing
But on serial monitor its working very well
and just now tested with MEGA
There also a Red SMD LED is responding means the picture is clear with code very well . . . .
Don't why output is having this kind of abnormality !!

I just want to have a one shot LED glow and OFF
Like a blink of eye
And ya ready for another shot
No matter how many times or how fast I use to trigger with micro switch, that output should respond all the time !!!

See simulator:

1 Like

O that's a great work
Output is responding with high speed switching
Thanks !!!

Sir is this delay(5) part would effect in any of the way ??

I mean I am just thinking that if we go for multiple outputs say a bunch of 16 pairs of inputs and outputs suppose . . .

Would this repeated entry of delay(5) 16 times within the running code is going to effect any of the way ??

Please comment !!!

Another Stone Soup.

Can you post this code of yours 16 times so that I can analyze the impact of this 5 useg on your project.

Ya I need to check for the whole package of 16

Let me design and revert you back !!!!

I'm betting over 80 "garnishes."

Ya Sir with this code below I started one by one for 16 entries

have a look :

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
  //   }


  
}

Now up to 7th pair its working fine

But introducing the 8th one damage everything . . .

Here I mean, all the outputs are simply getting HIGH on pressing & holding the switch and remains in that HIGH state only

Now, when the switch gets released, it goes OFF

For all the 8 Inputs this type of function is going on

After blocking the code for 8th pair all the 7 outputs are working very well

In the code above I blocked the 8th on as you can check there . . .

Well I worked on WOKWI only
No practical implementation till now due to lack of few components presently

The link for that is here :

Please Help !!!

Hopefully Waiting Sir . . . .

Hello King_RAJ_Enters

Consider:

//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[] {A0, A1, A2, A3};
constexpr uint8_t LedPins[] {9, 10, 11, 12};
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(115200);
  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();
}

Have a nice day and enjoy coding in C++.

2 Likes

... and it even does a setup lamp test.