Re: How to properly use an if statment

hi all friends…
i still new in arduino …my first project is to detect object using IR sensor…
if sensor detect object LED with be light and if object clear in < 5 sec nothing happen…but if sensor detect object and not clear in 5 sec relay will be on. i use 2 sensor for this project, my sketch function not properly relay still on although object already clear less that 5 sec. can any one help me?

this my sketch:

int sensa = 2; //Obstacle Sensor A
int sensb = 3; //Obstacle Sensor B
int led = 13; //Obstacle Sensor B
int relay = 4; // relay 5v
int sensora;
int sensorb;
// the setup routine runs once when you press reset:
void setup() {
//Everything written here will run only once
//Initializing pins as inputs and outputs
pinMode(led, OUTPUT);
pinMode(sensa,INPUT);
pinMode(sensb,INPUT);
pinMode(relay,OUTPUT);

}

// The loop routine runs over and over again forever:
void loop() {
sensora = digitalRead(sensa);
if(sensora==LOW) //If obstacle is detected carrier
{
digitalWrite(led,HIGH); // LED ON
delay(5000);
digitalWrite(relay,HIGH);
delay(5000);

}
else
{
digitalWrite(led,LOW); // LED OFF
digitalWrite(relay,LOW);
}
sensorb = digitalRead(sensb);
if(sensorb==LOW)
{
digitalWrite(led,HIGH);
delay(5000);
digitalWrite(relay,HIGH);
delay(5000);
}
else
{
digitalWrite(led,LOW);
digitalWrite(relay,LOW);
}
}

In your program once your detector sees an obstacle the LED is turned on, then 5 seconds later the relay is turned on whether or not the obstacle is still in view and it stays on for another 5 seconds.

What you need to do is to check whether the obstruction is still there 5 seconds after the initial check and, if it is, turn on the relay.

yes my program now once sensor detected led on and 5 sec later relay on.. what i need is..once sensor detect object led ON but if object clear (or move ) less that 5 sec (sensor not detect ) relay not turn ON. if object not clear in 5 sec ( or what timing we set ) relay turn ON.

i try edit this program to many time but still not work..pls help me

I gave you an idea in my previous post.

read the sensor if an obstacle is detected turn on the LED start timing wait five seconds read the sensor again if an obstacle is detected turn on the relay end if else turn of the LED end else end if

You need to check for an obstacle 5 seconds after the first check

You should also: 1) Read the first two posts at the top of this forum 2) Always reformat your code in the IDE before you post using Ctrl-T 3) Post your sample code in between the code tags, using the '#' character just above your post, like

int sensa = 2; //Obstacle Sensor A
int sensb = 3; //Obstacle Sensor B
int led = 13; //Obstacle Sensor B
int relay = 4; // relay 5v
int sensora;
int sensorb;
// the setup routine runs once when you press reset:
void setup() {
  //Everything written here will run only once              
  //Initializing pins as inputs and outputs
  pinMode(led, OUTPUT);
  pinMode(sensa,INPUT);
  pinMode(sensb,INPUT);
  pinMode(relay,OUTPUT);   
}

// The loop routine runs over and over again forever:
void loop() {
  sensora = digitalRead(sensa);
  if(sensora==LOW) //If obstacle is detected carrier
  {
    digitalWrite(led,HIGH);  // LED ON 
    delay(5000);
    digitalWrite(relay,HIGH);
    delay(5000); 
  }
  else
  {
    digitalWrite(led,LOW); // LED OFF
    digitalWrite(relay,LOW);
  }
  sensorb = digitalRead(sensb);
  if(sensorb==LOW)
  {
    digitalWrite(led,HIGH);
    delay(5000);
    digitalWrite(relay,HIGH);
    delay(5000);
  }
  else
  {
    digitalWrite(led,LOW);
    digitalWrite(relay,LOW);
  }
}

yes..good idea exactly i want function like this... how to write code for this function?

You already have all the commands that you need in your existing code. There are better ways of waiting for 5 seconds than using delay() but for now just use delay().

ok..i get it.. all work fine..thanks for your support and help.. thanks you very much.. :)