delay code

hello everyone
i'm trying to create a simple project about automation. the logic is very simple :if sensor is high then i want the led turning on in a minute
i'd like to put a delay/timer (about a minute) only on the led, so the sensor reading won't be affected with the delay
but i don't know how i should put the delay code
enlighten me please..
thanks before :slight_smile:

int pirPin = 1;
int ledPin =12;

void setup(){
pinMode (pirPin, INPUT);
pinMode (ledPin, OUTPUT);
}
void loop(){
if (pirPin == HIGH){
digitalWrite(ledPin, HIGH);
}
else{
digitalWrite(ledPin, LOW);
}
}

Don't use "delay", or your program will be "blind" for the duration of the delay.

Have a look at the "blink-without-delay" example in the IDE to see how to do it properly.

AWOL:
Don't use "delay", or your program will be "blind" for the duration of the delay.

Have a look at the "blink-without-delay" example in the IDE to see how to do it properly.

yap i knew i shouldn't use "delay".. and i've seen "blink-without-delay" too
but i'm still confuse how to apply "millis" to solve my problem

but i'm still confuse how to apply "millis" to solve my problem

You've got a watch (millis()) and a pad of paper (the variables you define in the sketch). Now, define how YOU would approach the problem.

Once you know how to perform the actions that you want the Arduino to perform, making the Arduino do it is trivial.

snowbrigades:
yap i knew i shouldn't use "delay".. and i've seen "blink-without-delay" too
but i'm still confuse how to apply "millis" to solve my problem

You have three states, right?

  1. waiting for trigger
  2. triggered, but waiting for a certain point in time
  3. triggered, and after the certain point in time

In states 1 and 2, you want your LED to be off.
In state 3, you want your LED to be on.

You could for example, use one variable for what state you're in, and switch on that state in loop().
In state 1, check the trigger, and if it's on, switch to state 2.
Then you could use another variable for when the light should turn on. Set that variable as you switch from state 1 to state 2, and check the time in the loop function for state 2 for whether it's time to switch to state 3.
Turn the LED on when switching to state 3.

PaulS:
You've got a watch (millis()) and a pad of paper (the variables you define in the sketch). Now, define how YOU would approach the problem.

Once you know how to perform the actions that you want the Arduino to perform, making the Arduino do it is trivial.

jwatte:
You have three states, right?

  1. waiting for trigger
  2. triggered, but waiting for a certain point in time
  3. triggered, and after the certain point in time

In states 1 and 2, you want your LED to be off.
In state 3, you want your LED to be on.

You could for example, use one variable for what state you're in, and switch on that state in loop().
In state 1, check the trigger, and if it's on, switch to state 2.
Then you could use another variable for when the light should turn on. Set that variable as you switch from state 1 to state 2, and check the time in the loop function for state 2 for whether it's time to switch to state 3.
Turn the LED on when switching to state 3.

sorry,,i didn't follow my thread for a while

actually i've made a sketch for this project
but i'm not using millis() function,,coz still confuse how to apply millis on my project :stuck_out_tongue:
i took another way

here is the code :

int pirPin = 1;
int ledPin =12;
boolean hold = false;
long timer;

void setup(){
pinMode (pirPin, INPUT);
pinMode (ledPin, OUTPUT);
Serial.begin(9600);
}
void loop(){
int pirState = digitalRead(pirPin);
if (pirState == HIGH){
digitalWrite(ledPin, HIGH);
hold = true;
timer = 0;
}
else if (hold == true){
timer = timer + 1;
Serial.println(timer);
if (timer > 3000){
digitalWrite(ledPin, LOW);
timer = 0;
hold = false;
     }
    
}
else {
digitalWrite(ledPin, LOW);
 }
}

i'm using counter code in variable timer (timer = timer + 1)
i set 3000 counter for timer
3000 counter doesn't mean 3000ms..when i observed, 3000 counter = 18 sec

when i set additional input & output, the system works parallel well
but the counter orientation is different...when i observed, 3000 counter = 20 sec
why ?

and also i have to put Serial.println on my code
if it did'nt, the timer won't counting

timer = timer + 1 3000 times is easy for arduino. It is doing it faster than you think.

the serial.print is slowing it down enough to provide enough high time to the led. the duty cycle without serial.print is too small to see the led light most probably.

pYro_65:
timer = timer + 1 3000 times is easy for arduino. It is doing it faster than you think.

the serial.print is slowing it down enough to provide enough high time to the led. the duty cycle without serial.print is too small to see the led light most probably.

so if i don't want to use serial.print on my code, how should i do ?
should i put higher value for counter timer variable ?

Using millis seems better.

the millis example below should trigger every two seconds.

int old_time  = millis();
int ledPin      =12;

bool b_Switch = false;

void setup(){

  pinMode (ledPin, OUTPUT);
}

void loop(){

  if( ( millis() - old_time ) >= 2000 ){ //If time elapsed is 2 seconds or more

    if( b_Switch ){

      digitalWrite(ledPin, LOW);

    }else{

      digitalWrite(ledPin, HIGH);
    }
    old_time = millis();
    b_Switch = !b_Switch;
  }
}

snowbrigades:
... but i'm still confuse how to apply "millis" to solve my problem