Millis function

Guys, can someone pleaee help me?

I want to make a project where a led should light up if button is pressed less than 2 seconds continuously, it should not light up if it is pressed continuously more than 2 seconds. I have no idea how to program it and the arduino should not take action only after the button is released, it should take action when it is being pressed. So please guys, can someone help me build the code.
Fast reply would be appreciated.

So you are asking the program to predict the future. How do you know when the button is first pressed if it is going to be released within two seconds or not.

Simply you can't.

What you can do is to trigger things when the button is released. So on first seeing the button pressed you make a note of the millis time, and on release see how long it has been held down and then do stuff.

Look at the state change example in the IDE to see how to detect when a button becomes pressed as opposed it it just being down.

While I agree fully with Grumpy_Mike's opinion based on what OP said, I'm wondering if this isn't possibly a language thing and the OP's not actually saying what he means.

Suggestion to OP:write that all down to explain to another human to do what you ask, with you pressing your hand on his shoulder and releasing it to be the pressing and releasing of a switch. All the other person is allowed is to see the clock on the wall, and read your instructions. He can feel your hand on his shoulder, and he can put his hand up to mean the light is on.

Get that right, then talk about coding.

Yes, you guys are right, but i have a question.

Why can't we do like this, we should start the timer when a button is pressed and if the timer crosses 2 seconds, the led should turn off and when it is released the timer should reset.

Can someone give me the code for this idea?

Yes you could do that.

Can someone give me the code for this idea?

That is not how it works round here. You write something and we help.

To give you a start detect when the button is pressed and copy the value in the millis timer. Then when the button is released subtract this time from the millis timer and print how long it is held down for.
This is not what you want but is a step towards what you want. You never write all the code at once, you always do a bit and test.

Do that and get it working then you are ready for the next step.

Yes i have done it before and it was working before, now can you say how to record the timer when the button is pressed?

Aamir__cool:
say how to record the timer when the button is pressed?

You need variables to hold the button's current and previous state and another to hold the time at which it became pressed.

Then in loop() you read the button into the current state variable, and compare that to the previous. If they are the same, do nothing. If they differ, then it means it was either just pressed or just released. So you need another if inside the first one, to see if its low (we will assume pullups and active low). If it's low, that means it just got pressed, so you just set your start variable equal to millis().

I dont know so much coding as i even copied an example frkm a website to do the test (the button release timer), sk can you pls give me the code or teach how to?

Aamir__cool:
pls give me the code

That's unlikely, but you will get help to fix what you try if you run into problems.

teach how to?

Best is to start with IDE File > Examples, paying particular attention to State Change Detection and Blink Without Delay.

Aamir__cool:
Yes i have done it before and it was working before,

So post what you have done before.

now can you say how to record the timer when the button is pressed?

Simple:-

startTime = millis();

where startTime was previously declared as a global variable of type unsigned long.

Ok guys, thanks for your reply. But i found that i need to delay after the sms sending function (instead of led because this is my project) so that it wont repeat sending the sms until the delay is finished, but i have a problem here. There is more than one button (which triggers the sms) so i need to create a program where if one button is pressed and sms1 is sent and it should go into delay mode but at the same time, if another button is triggered, that button function should send the sms2. So guys, can you say me the steps do it with the millis function
?

But i found that i need to delay after the sms sending function (instead of led because this is my project) so that it wont repeat sending the sms until the delay is finished

No you don't, that is a sticking plaster because you have not coded the other stuff correctly.

So guys, can you say me the steps do it with the millis function

What part of:-

Grumpy_Mike:
So post what you have done before.

Are you having difficulty understanding?

Hey guys i will make it simple. I wanna make delay with millis () but after a function (ex:After a button press) So pleaee guide me?

i will make it simple

You don't show what you have done, we can't show you how to change it.

I wanna make delay with millis ()

No you don't that would be:-

start = millis();
while (start-millis() > delayInterval) { }

Which is exactly the same as:-

delay(delayInterval);

So how does that help you?

while (start-millis() > delayInterval) { }Close, Mike :wink:

Not my day today, of course

while (millis()-start < delayInterval) { }

I haven't seen what's the problem here, he can't count the time analog is reading hig values ?