hi guys
i am working on a project where i would like to use millis instead of the common delay function
like i want to turn on a led after a interval say 3000ms when i press a button
i hope this might be the simple but i couldn't figure it out =(
int led1 = 13;
int buttonpin = 5;
int buttonState = 0;
unsigned long interval =2000;
unsigned int timer;
void setup ()
{
Serial.begin(9600);
pinMode(led1,OUTPUT);
pinMode(buttonpin,INPUT);
}
void toggle(){
buttonState = digitalRead(buttonpin);
if (buttonState == HIGH)
{
Serial.println("button pressed");
timer=millis();
}
}
void loop()
{
if ((millis()-timer) >=interval)
{
toggle();
digitalWrite(led1,HIGH);
digitalWrite(led2,HIGH);
digitalWrite(led3,HIGH);
}
}
this example is for a different project like i need to turn the servo ,hold the position for 5 seconds and get back to the previous position ,
where the servo movement should be triggered by 2 button (bump switches)
if ((millis()-timer) >=interval)
This will be true the first time through loop() so toggle() will be called and on return the LEDs will be turned on regardless of whether the button is pressed or not. You need to turn the LEDs on only when the delay is finished and after the button has been pressed.
boolean timing = false;
byte LEDpin = 13;
byte buttonPin = 7;
unsigned long startTime;
void setup()
{
pinMode(LEDpin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop()
{
if (digitalRead(buttonPin) == LOW)
{
timing = true;
startTime = millis();
}
if ((timing) && millis() - startTime >= 3000)
{
digitalWrite(LEDpin, HIGH);
}
}
i am really confused about millis function ,,when to initialize the timer , how to check the elapsed time
i wanted to use it for controlling my servo instead of delay function
can any 1 clearly help me in this issue 
Well to start with: millis() is not a programmable timer. It is an independent counter (unsigned long) that starts a 0 when the Arduino is switched on (or reset of the Arduino) and increments with 1 every millisecond. After about 49 days the counter reaches its maximum and starts at zero again.
To understand usage of millis() is to think of a time line.
If I want to do something after 3 seconds:
a) I get the current counter value through millis() and add 3000 to that result. So if millis was 1250 when I called it, I add 3000 giving 4250. This means that when millis() reaches 4250 of goes past this value I have to do something.
b) now I can loop endlessly doing all kinds of things but I have to check regularly if the counter millis() has reached or exceeded the number I saved in A.
c) if a little later I want to place another check with millis() I read it again. And maybe its value is now 15000 and I want to do something after 4 seconds (=4000 milliseconds) So I save a value of 19000 (15000 + 4000) in a variable. etc etc
Once you see the "light" of millis you will appreciate its power and possibilities.
At the same time realize that you will probably never reach the EXACT value when testing against millis because millis is a counter that simpy increments every millisecond. But you do not know how long a loop in your code will take.
And to add to Helibobs example:
{
pinMode(LEDpin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop()
{
if (digitalRead(buttonPin) == LOW)
{
//
// the button was pressed so set a marker for the current millisctr + 3000 milliseconds
//
timing = true; // set this to know the button was pressed and that we have to check millis()
startTime = millis() + 3000L;
}
if ((timing) && (millis() >= startTime))
{
//
// we set the marker AND millis is equal or greater than the marker
// so light the LED. Keep in mind that in this code the led stays on as timing stays true (it is nowhere set false anymore) and the LED is nowhere switched off anywhere
//
digitalWrite(LEDpin, HIGH);
}
}