questions about dealys

hello,i have 3 led and a object sensor, i need to turn on the first led when the sensor detect an object then 3 seconds later let the second led goes on and 3 seconds later the third led goes on, after that everything goes off together.. i have tried to use delay but its not working out with me,

Take a look at the blink without delay example.

Mark

i looked at it but it looks hard for me. im still beginner. so is there a way using dealy or simple way. also i have tried, but i dont know how to develop the code in the example to do what i want it to do

From what you describe delay is probably an ok way to do it.

Thing is that without your code and a description of what isn't working out for you, the best anyone can do is just feel bad for you.

int sensor=7;
int led1=17;
int led2=18;
int led3=19;
int val=0;

void setup()
{
pinMode(sensor,INPUT);
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
}
void loop()
{
val = digitalRead(sensor);
if (val == 1)
{
digitalWrite(led1, HIGH);
delay(3000);
digitalWrite(led2, HIGH);
delay(3000);
digitalWrite(led3, HIGH);
delay(3000);

digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
}
}

thats my program and what happens is : first led goes on , 3 seconds later second led goes on , three seconds later third led goes on, 3 seconds later third and second led goes off but first one stays on .

remo0101:
thats my program and what happens is : first led goes on , 3 seconds later second led goes on , three seconds later third led goes on, 3 seconds later third and second led goes off but first one stays on .

I don't see anything in the code that would cause that - the code looks OK. The only thing I can think of is that if the input was still HIGH at the end of your processing, it would immediately turn led1 HIGH again and restart the sequence. Is it possible that's what you're seeing?

I'm with Peter on this. I don't see any issue with the code and wonder the same thing he does. Try adding a long delay of ten seconds or so right after you turn off the last LED. That should help determine if just starting over is what's happening.

PeterH:
The only thing I can think of is that if the input was still HIGH at the end of your processing, it would immediately turn led1 HIGH again and restart the sequence. Is it possible that's what you're seeing?

Almost certainly this is the case. How is the sensor wired up?

ok i see, yes the sensor still high, and it will stay high for rest of program. but how can i change my code to let the third led goes off after that 3 seconds.

You can't because that code will not do what you say it is doing.
Therefore there is something you are not telling us or something you are missunderstanding.

I thought it was the first led that stayed on.

yes jimmy i meant i need to let the first led goes off, is there is away

Grumpy_Mike, all what i need the program to do is when is something detect the sensor let the firs led goes on 3 seconds later second led goes on 3 seconds later third led goes on 3 seconds later let the 3 leds goes off, all that while the sensor still detecting this object. this is a part of long program im doing, it will take forever to explain it. but that what i need

remo0101:
yes jimmy i meant i need to let the first led goes off, is there is away

Certainly it's possible, but you need to be clear exactly what behaviour you want.

What should happen if the button is pressed and held down? Do you do the cycle once and wait for the button to be released and then pressed again before running the cycle again, or have the cycle repeat immediately, or only after some delay, or what?

this is a part of long program im doing

Hmm.... if you put that code inside a bigger program, beware of the fact that you'll stop the arduino from doing almost anything other than turning on and off those led for about 10 seconds.
My advice is to try to understand the blink without delay example. If the program is more than a few lines long, chances are you'll have to anyway...

I'm quite certain the first led does shut off, just briefly before it turns back on. So brief that you never see it as off. Did you try that delay I suggested as a test?

its not a button its sensor. i need when the sensor detect any object , the program does all the process i said. like what i said it part of long program after few seconds the sensor wont be detecting anything , here i want to reset the sensor and start the new cycle. plz help me guys

"reset" the sensor ? What does that mean ? In the program you showed before, if the sensor is still "high", then the first led will turn on again. You probably want to ignore the sensor for a certain amount of time. That's done with an additional delay like suggested or... well you'll probably have to show where this three-led-loop will live :slight_smile:

yes jimmy, it goes off and on back , how can i stop the sensor for while, or can i use break to get out of the if statement loop.

An if statement isn't a loop. It's looping because it's running in loop(). A delay after LED 3 is turned off will slow the loop down. If you only want it to execute once put it in setup().