Help how to make a code for this...

Example 1

Implementing a simple timer

unsigned long Timer "ALWAYS use unsigned long for timers, not int" (variable declaration outside setup and loop, of course)

if digitalRead==Down then "set timer" Timer = millis , digitalWrite ON if "timer expired" millis-Timer >= 15000UL then digitalWrite Off

(the "UL" after the number is a syntax detail that is important when dealing with large numbers in millis and micros, therefore it is shown although this is pseudo code)

Looks like a school assignment to me. We can help you understand things but generally we do not do your assignment for you.

How do you thing you should do it?

Have a go, and ask specifically on what you don't understand. Or post your code and say what it does and what you want it to do.

its not an assignment or anything... i just wanna learn how to do this...i got this example from here http://arduino.cc/playground/Code/AvoidDelay

i try to do this but the led doesnt turn off after 15 secs...

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
int button = 0;         // variable for reading the pushbutton status
int timer;


void setup()
  {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
}

void loop()
{
  
  button = digitalRead(buttonPin);

if (button == HIGH) 
{     
  
  unsigned long timer = millis();

  digitalWrite(ledPin, HIGH);  
} 
if (millis() - timer >= 15000UL)    // turn LED off:
    digitalWrite(ledPin, LOW); 
}
int timer;
  unsigned long timer = millis();

Global and local variables of the same name, but different types are rarely a good idea.

In this case, they are particularly a bad idea.

Here's i notice... the timer counts even if you haven't press the button... then after the timer reach 15 secs... it turns off... then the process doesn't repeat even if you press the button again...

how to repeat the process again???

the timer counts even if you haven't press the button

Then you don't have the switch wired correctly. You are not using the internal pullup resistor, so you must have an external pullup or pulldown resistor.

how to repeat the process again???

It happens automatically. But, you posted bad code, you haven't said that you have fixed the problem, or how you fixed it. And, you appear to have a hardware problem, too. Fix some of the problems that you already have before looking for new ones.

PaulS...

after trial and error its working now ehehhee

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
int button = LOW;         // variable for reading the pushbutton status
unsigned long timer = millis();

void setup()
  {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
  Serial.begin(9600);

}

void loop()
{
  
  button = digitalRead(buttonPin);
{
if (button == HIGH) 
{     
  
timer = millis(); 
  digitalWrite(ledPin, HIGH);  
} 
if (millis() - timer >= 5000UL)  
{// turn LED off:
    digitalWrite(ledPin, LOW); 
}
Serial.println(millis());
}
}