Is a small delay needed for a quick high/low pulse?

For background please see my post here:

This is the code, which does two things:

  1. Every 1000ms send a count to the serial monitor
  2. Every 5000ms sends a message when pin D6 (DONE) is written high/low.

Of course I have used millis instead of delays, but for the high/low pulse I used a very small 50ms delay.

Is there another way of doing this without the delay? Or not worth worrying about?

Thanks

// Watchdog timer test using TPL5010 and Wemos D1 mini
//Wiring
//3.3V to VDD
//GND to GND
//Resistor to DELAY (30K resistor - actual 158 seconds)
//RST to RST (100K pullup)
//WAKE - not connected
//D6 to DONE (100K pullup)


// constants won't change. They're used here to set pin numbers:
const int done_pin = D6;    // the number of the pushbutton pin
int n = 0;
long previous_done_Millis = 0;        // will store last time DONE pulse was send
long done_interval = 5000;           // DONE pulse interval

long previous_seconds_Millis = 0;        // will store last time seconds was read
long seconds_interval = 1000;       // serial monitor seconds counter


void setup() {

  Serial.begin(115200);
  pinMode(done_pin, OUTPUT);

  // set initial LED state
  digitalWrite(done_pin, LOW);
}


void loop()  //acts as DONE signal to reset watchdog timer
{
  unsigned long currentMillis = millis();


  if (currentMillis - previous_done_Millis > done_interval)
  {
    // save the last time you sent DONE signal
    previous_done_Millis = currentMillis;
    digitalWrite(done_pin, HIGH);
    delay(50);
    digitalWrite(done_pin, LOW);
    Serial.println("Done pulse received");
  }


  if (currentMillis - previous_seconds_Millis > seconds_interval)
  {
    // save the last time you sent seconds to terminal
    previous_seconds_Millis = currentMillis;
    n = n + 1;
    Serial.println(n);
  }


}

Is there another way of doing this without the delay?

Why do you think you need a delay? Pulsing a pin to make a stepper motor driver make a stepper step does not require a delay between turning the pin on and turning it off. At least, not when using digitalWrite() to do that. That function takes a fair bit of time to determine that the pin is a valid pin for the board and which port the pin is on. Actually turning the pin on is then very fast. Repeating the process of getting ready to turn it off takes time, too. That may be enough time for whatever is supposed to react to the pin state change to notice that the state has changed.

Hi @PaulS

I’m not sure the delay is required to be honest. Given what you have said it seems not. Reading the datasheet for the TPL5010 it specifies a typical pulse time of 100ns for the done signal, most likely the delays you mentioned might exceed that.

I’ll try it with no delay and see how it behaves.

Thanks

There are occasions in code where you may need short ‘delays’, as in delay() - (blocked)... so delay() can be convenient.

But there’s nothing to say you can’t use multiple millis() timers (non-blocking) - in different parts of the code at different rates..., or nested - if that’s what your logic requires.

Two NOOPs gives ~120ns.

“typical pulse time of 100ns” minimum/maximum ?

This technique uses a hardware timer - Generating precise one-shots on avr8

PaulS:
Why do you think you need a delay?

It's working fine without the delay, at least I learned something today :slight_smile: Thanks

lastchancename:
There are occasions in code where you may need short ‘delays’, as in delay() - (blocked)... so delay() can be convenient.

But there’s nothing to say you can’t use multiple millis() timers (non-blocking) - in different parts of the code at different rates..., or nested - if that’s what your logic requires.

Thanks that makes sense.

larryd:
Two NOOPs gives ~120ns.

“typical pulse time of 100ns” minimum/maximum ?

I had not head of NOOPs, I read a bit about it here. Quite interesting.

How did you arrive at the figure of 120ns may I ask? This works out to 8.33MHz doesn't it?

Typical pulse time: - I think I misread the datasheet, 100ns seems to be the minimum, there is no maximum stated.