Delay a pin state after event

First off any advice is appreciated.

What I am trying to do is use my Oscopes trigger to control a relay, and have that part working fine. But, I need a delay between the time the TTL signal is received and the output pin goes low( normally closed system).

I have been searching for examples to point me in the right direction and was looking at MIllis, but am not sure that's what I am looking for.

If anyone could steer me in the right direction I will sing at your wedding (not literally) :D

Start by identifying the time at which the signal is received. Record that time with millis. Then periodically check to see how long it has been since the event was recorded, and turn off the relay when that time comes.

The delay… how much or little and how precise must it be?

There is likely a 555 circuit or even simpler, an R-C circuit with PNP trigger that can make such a delay.

I knew a EE student who used the latter to ‘fuse’ fireworks back in the 70’s.

I am needing a 1000ms (1sec) delay from the time I receive the trigger form the scope until the relay de energizes. I am using this to control an old power system that requires you to press a button after discharge. I am just using 1 second as a buffer ± a few ms should not hurt.

I am trying to use millis() but do not quite have it working right. I have posted a snippet that I am trying to learn from and adapt but it is not working out like I plan. If anyone can help me out I will sing and dance at your wedding.

void loop()
{
  if(digitalRead(switchPin) == LOW)
  {
     // here if the switch is pressed
     startTime = millis();
     while(digitalRead(switchPin) == LOW)
        ; // wait while the switch is still pressed
     long duration = millis() - startTime;
     Serial.println(duration);
  }
}

All that snippet does is tell you how long the button was pushed down for. You need some type of structure like this:

if (someEventOccurs)
{
   // turn a relay on
   relayTurnOnTime = millis();
}

if (millis() - relayTurnOnTime > 1000)
{
   // turn off relay
}

I have posted a snippet that I am trying to learn from

Then you will be needing snippets are us:-

http://snippets-r-us.com/

Thanks, I will try and write a piece of code using that structure and see what happens.

Do you want the quick and dirty code that does not debounce the button but works... that if you try and develop from there will lead to hair loss or

Do you want the code that debounces the button and is structured accomodate further development?

The first is much shorter and simpler, a way to replace a few cheap components with a far more expensive controller board.

The second makes some beginner coders cringe or run in fear while others declare joy.

Either way, I don't care. Your goals are what matters here.

Grumpy_Mike:

I have posted a snippet that I am trying to learn from

Then you will be needing snippets are us:-

http://snippets-r-us.com/

Priceless! Karma to you! I wish I could give you 100 Karma right now for that! And PaulS too!

If your willing to share I would like to see the 2nd option.

Thanks

Okay, just got back from shopping. Mmmmmm, cherries for $1.69/lb! Even got a bag for my neighbor. Next I post here will be code modified from an existing example. Input will be pin 2 grounded and output will be led 13, tested on an UNO and IDE 1.5. Comments will be trimmed, I could spend an hour+ just there so ask about what isn't clear.

This may have behavior you don’t want but here goes.

Button to ground on pin 2 … I use a jumper that I ground on the Arduino USB connector box.

Touch once and after it debounces (as now, a long debounce of 100ms with no change to match dirty jumper contact) the 1 second delay to light the led starts.

Touch again and the led turns off.
IF the off-touch is done within the 1 second before the led lights, the led will not light.
Is it good or bad that a double-click cancels the light?

// All you need to do this example is an Arduino and a button on pin 2.
// Push the button once, led lights after delay. Again, led turns off.
// Button is debounced and wired directly to ground. The button can be a 
// jumper from pin 2 grounded on USB connector. Tested with jumper.
// By GoForSmoke for free public use. Using Arduino 1.0.5-r2

const byte ledPin =  13;      // this is the onboard led pin
const byte buttonPin = 2;


byte setLedPin = 0;
const unsigned long ledDelay = 1000UL; // blink interval
unsigned long ledDelayStart = 0U; // 16 bit blink (on or off) time ms

// button variables
byte buttonRead;  // wired for pullup -- if down then LOW, if up then HIGH 
byte lastButtonRead = HIGH; // so debounce knows previous read
byte checkDebounce = 0; // only checks decounce after every button pin change
byte lastButtonState = 0; // last stable button state
byte buttonState = 0;  // stable button state
// 0 = button is up after debounce
// 1 = button is down after debounce

// button debounce timing variables
const unsigned long debounceDelayMs = 100UL; 
unsigned long debounceStartMs; 
unsigned long msNow; 


byte processState = 0; 


void setup() 
{
  Serial.begin( 115200 );

  pinMode( ledPin, OUTPUT );  // default is LOW 
  pinMode( buttonPin, INPUT_PULLUP ); // my button connects to ground, not 5V
  // however that means that when the button is pressed the pin is LOW.
}

void loop() // make sure that loop() runs fast and stays in the "now".
{

  // BUTTON CODE BLOCK, it handles debouncing. 
  // the task is to set the variable buttonState when a Stable Button State is reached.
  // other sensor code could change the same variable if desired

  // read the pin which may be changing fast and often as the button bounces
  buttonRead = digitalRead( buttonPin ); // momentary state

  msNow = millis(); 

  if ( buttonRead != lastButtonRead )
  {
    debounceStartMs = msNow;
    checkDebounce = 1;
  }
  else if ( checkDebounce )
  { 
    if ( msNow - debounceStartMs >= debounceDelayMs ) // stable button state achieved
    {
      buttonState = !buttonRead; // mission accomplished, button is stable
      // note that buttonState is opposite buttonRead
      checkDebounce = 0; // stop debounce checking until pin change
    }
  }
  lastButtonRead = buttonRead;
  //
  // End of the BUTTON CODE BLOCK

  // CONTROL CODE BLOCK that uses buttonState and processState
  
  if ( lastButtonState != buttonState )
  {
    lastButtonState = buttonState;

    Serial.println( F( "============================================================" ));    
    Serial.print( F( "processState " ));
    Serial.print( processState );
    Serial.print( F( "  buttonState " ));
    Serial.println( buttonState );
    Serial.print( F( "  millis " ));
    Serial.println( msNow ); 
    Serial.println( F( "============================================================" ));    

    switch ( processState )
    {
    case 0: 
      if ( buttonState == 1 ) // button is pressed 
      {
        processState = 1;
        setLedPin = 1;
        ledDelayStart = millis();
      }
      break; 

    case 1: 
      if ( buttonState == 0 ) // button is released 
      {
        processState = 2;
      }
      break; 

    case 2: 
      if ( buttonState == 1 ) // button is pressed 
      {
        processState = 3;
        setLedPin = 2;
      }
      break; 

    case 3: 
      if ( buttonState == 0 ) // button is released 
      {
        processState = 0; 
      }
      break; 
    } 
  }
  // End of the CONTROL CODE

  // LED CODE BLOCK
  if ( setLedPin == 1 )
  { 
    if ( millis() - ledDelayStart >= ledDelay )
    { 
      digitalWrite(ledPin, HIGH );
      setLedPin = 0;
    }
  }
  else if ( setLedPin == 2 )
  {
    digitalWrite(ledPin, LOW );
    setLedPin = 0;
  }
  // End of the LED CODE

}