Digitalwrite and Duration

I can't in my ignorance find how to make a Digitalwrite command go on only for a certain duration. I'm sure there's an easy basic way of doing it though I can't find any on the Forum, but I have a hard time searching for it since my knowledge is far to nonexisting (I've been searching the webb for some days now). I need to find an answer though.

I mean I feel it should be possible to write something like Digitalwrite(pin5, HIGH, 3000), but it doesn't work. To many arguments.

Can anyone summon some pity and just tell me the easiest way that it should be done (without using a delay)?

I have seen the severalthingsatonce of Robin2, and I've checked the Blink without delay, but I'm not able to figure out the answer from that. Or is it so that it can't be done as easy as i think?

I am learning, it's just that my needs doesn't synkronize with the learning process.

Thank you!

I can't in my ignorance find how to make a Digitalwrite command go on only for a certain duration.

   digitalWrite(somePin, HIGH);
   delay(someTime);
   digitalWrite(somePin, LOW);

Now, you CAN use the blink without delay philosophy instead of delay(). But, you will need to invest a minimum amount of effort before we give you the answer.

Well, I can't use the delay because I have to read sensors all the time.

Don't think I'm not trying to get it. What messes it up for me is that I need to use the duration inside an if-statement. But I'm on it. I only have been coding for a week, so human as I am I hoped there was a really easy way that I just missed, like duration(someTime); instead of delay(someTime);

Thanks anyway.

Look at the example sketch called 'BlinkWithoutDelay'.

Yes, I tried that. But I couldn't figure out how to use it as a single delay. I want to turn on a led when a sensor is triggered, for say 3 sek, then off, then after the first led have been of in 4 sek turn on a different led for 2 sek, then of, then when the second led been of for 2 sek, turn on a third led for 5 sek. All while reading if the sensor is triggered to break the sequence and instead start a similar sequence with a whole different set of leds.

Keep track of the "State" (hint, hint) of the LED.

BlinkWithoutDelay is a start. I would also suggest that you try and find out about Finite State Machines as this is a typical application for that style of programming.

To answer (dash your hopes) your real question: No, there is no one line command to do what you want.

evanmars:
To answer (dash your hopes) your real question: No, there is no one line command to do what you want.

Yep.

To make a LED come on for a duration, you turn it on, and then you turn it off when the duration has elapsed. You work this out by using millis() - a function that returns the number of milliseconds since the arduino was powered up. When you turn the LED on, record the time it was turned on. Every time loop() runs, check if the led is currently on AND the time has elapsed. If this is the case, turn the LED off.

A big thank you to you all! To bad to hear there's no oneliner, but I'll work with the BlinkWithOutDelay model then. Reading your posts I'm starting to get how it works.

Thank you!

Of course, nothing prevents you from creating your own one-liner using a timer interrupt to create your own non-blocking delay. It could actually be quite useful.