help needed in programmming two outputs to control a led

ok so i am new to programming but have a lot of exp in hobby electronics after doing some practice in arduino (led cube,led sequences,rgb led,motor control,ir sensors etc)

i finally started doing a serious project , in this project i hav 2 (IR)sensors which give ground on there output pin when triggered, i want to make a program which will check the status of both sensors,
when the first sensor is triggered giving a negative pulse to a pin and after a short delay the second sensor is triggered giving a negative pulse to another pin, a led should light up. similarly when the second sensor is triggered before the first ,the led should turn off .for this i used the following code {extract from the main code}

if(digitalRead(sensorA)==LOW)
{
delay(10);
  if(digitalRead(sensorB)==LOW)
   {
   count=count+1;
   }
}
 if(digitalRead(sensorB)==LOW)
{
delay(10);
  if(digitalRead(sensorA)==LOW)
   {
   count=count-1;
   }
}
if(count<0)
{
digitalWrite(output,LOW);}

if(count>0)
{digitalWrite(output,HIGH);}

but the value of count remains zero whatever i do,i hav tried various alterations to this code but nothing works

the name of my project is smart light switch( senses no of ppl in room and controls room lighting) and i am using a breadboard arduino and println function just hangs up my pc

this is my first post so sorry if i made a mistake just tell me and i will correct it

Post ALL your code

but the value of count remains zero whatever i do

that's what happens when you increment a variable, then almost immediately decrement it.

const int ledIR =10;
const int ledA= 13;
const int ledB= 3;
const int sensorA=12;
const int sensorB=9;
const int output=11;
int count=0;

void setup() {
 
pinMode(ledIR,OUTPUT);
pinMode(ledA,OUTPUT);
pinMode(sensorA,INPUT);
pinMode(ledB,OUTPUT);
pinMode(sensorB,INPUT);
pinMode(output,OUTPUT);
 pinMode(4,OUTPUT);
 tone(10,38000);
}

void loop() {

if(digitalRead(sensorA)==LOW)
{
delay(10);
  if(digitalRead(sensorB)==LOW)
   {
   count=count+1;
   }
}
 if(digitalRead(sensorB)==LOW)
{
delay(10);
  if(digitalRead(sensorA)==LOW)
   {
   count=count-1;
   }
}
if(count<0)
{
digitalWrite(output,LOW);}

if(count>0)
{digitalWrite(output,HIGH);}

if(count==0)
{digitalWrite(4,HIGH);}                             // for checking if count is still zero

digitalWrite(ledA,digitalRead(sensorA));  //for checking status of sensors
digitalWrite(ledB,digitalRead(sensorB));

tnx for the quick reply and why does it decrement ??

when the first sensor is triggered giving a negative pulse to a pin and after a short delay the second sensor is triggered giving a negative pulse to another pin, a led should light up.

After you get done destroying your Arduino giving it negative pulses, and you get a new one, you might (make that will) need to understand about detecting transitions (the current state is not the same as the previous state).

PaulS:
After you get done destroying your Arduino giving it negative pulses, and you get a new one, you might (make that will) need to understand about detecting transitions (the current state is not the same as the previous state).

by negative pulses i mean that when i put my hand between the led and sensor ,the sensor pulls the output to ground

i want to sense directions of people moving in and out of a room by putting these two sensors on the door of the room so that i can turn off the lights when the room is empty

dont worry about me killing my arduino i hav subjected it to more worse forms of torture..
and what do u mean by detecting transitions ??

When you read the state of a pin, you can tell whether it is HIGH or LOW. By itself this may be useful, or it may not. In terms of counting people going into the room, or out of the room, it is not.

You need to compare the current state of the pin to the previous state. If they are the same, either nobody is moving past the sensor OR the person is still moving past the sensor.

Clearly, you don't want to count the person more than once just because they are slow. So, you need to detect when the current state is not the same as the previous state. This requires that you keep track of the previous state. Only when the current state is not the same as the previous state should you then decide whether or not to increment the counter.

Something like this:

byte currStateA;
byte prevStateA = LOW;

void loop()
{
   currStateA = digitalRead(sensorPinA);
   if(currStateA != prevStateA)
   {
      // A transition occurred. Either the sensor is now blocked and wasn't before
      // or it is not blocked but it was before.

      if(currStateA == HIGH)
      {
         sensorATriggered = true;
         sensorATime = millis();
      }
   }
   prevStateA = currStateA;
}

You'd have similar stuff for sensor B. After determining that A and B have been triggered, oyu can then use the difference in time to determine if the person entered the room or left it.

This process only works if the people entering and leaving the room want it to work. That means that two people can't pass in the doorway. One shouldn't trigger the sensors on the way in and drawl under on the way out, or vice-verse.

PaulS:
You'd have similar stuff for sensor B. After determining that A and B have been triggered, oyu can then use the difference in time to determine if the person entered the room or left it.

This process only works if the people entering and leaving the room want it to work. That means that two people can't pass in the doorway. One shouldn't trigger the sensors on the way in and drawl under on the way out, or vice-verse.

tnx alot for your reply i understand what your saying !!
ill try it and post the results soon XD

i tried your code and it worked to some extent but weird things were happening, the output led was randomly lighting up whenever i fiddled with the sensors , it wasn't working the way its supposed to

when i pass from sensor b to sensor a 2 times the led doesn't light up but it lights up the third time

and it lights up when i put my hand in front of sensor a the first time after reset =( =(

this is the code i used:

const int ledIR =10;
const int ledA= 13;
const int ledB= 3;
const int sensorA=12;
const int sensorB=9;
const int output=11;
int count=0;

boolean sensorATriggered=false ; 
unsigned long sensorATime ;
byte currStateA;
byte prevStateA = LOW;

boolean sensorBTriggered=false; 
unsigned long sensorBTime;
byte currStateB;
byte prevStateB = LOW;

void setup() {
  // put your setup code here, to run once:
pinMode(ledIR,OUTPUT);
pinMode(ledA,OUTPUT);
pinMode(sensorA,INPUT);
pinMode(ledB,OUTPUT);
pinMode(sensorB,INPUT);
pinMode(output,OUTPUT);
 pinMode(4,OUTPUT);
 tone(ledIR,38000);
digitalWrite(4,LOW);
}

void loop() {
 
 currStateA = digitalRead(sensorA);
   if(currStateA != prevStateA)
   {
        if(currStateA == HIGH)
      {
         sensorATriggered = true;
         sensorATime = millis();
      }
   }
   prevStateA = currStateA;  

  currStateB = digitalRead(sensorB);
   if(currStateB != prevStateB)
   {
      if(currStateB == HIGH)
      {
         sensorBTriggered = true;
         sensorBTime = millis();
      }
   }
   prevStateB = currStateB;
 
   if(sensorBTriggered==true && sensorATriggered==true)
 {
     if(sensorATime>sensorBTime)
    {count=count+1;
     sensorATriggered=false;
     sensorBTriggered=false;
    }
     else if(sensorATime<sensorBTime)
    {count=count-1;
     sensorATriggered=false;
     sensorBTriggered=false;
    }
 
 }
    
if(count>0)
{
  digitalWrite(output,HIGH);
}
 else if (count<0)
{
 digitalWrite(output,LOW);
}
  
  digitalWrite(ledA,digitalRead(sensorA)); 
  digitalWrite(ledB,digitalRead(sensorB));
  

}
pinMode(ledIR,OUTPUT);
pinMode(ledA,OUTPUT);
pinMode(sensorA,INPUT);
pinMode(ledB,OUTPUT);
pinMode(sensorB,INPUT);
pinMode(output,OUTPUT);
 pinMode(4,OUTPUT);
 tone(ledIR,38000);
digitalWrite(4,LOW);

I don't want to seem picky, but please explain why there is a mix of names and numbers here. If names are a good idea, then everything should be based on names. If they are not, then nothing should.

There is no Serial.begin() statement in setup(). I'm sure there is a good reason why not. I'm on the edge of my seat waiting to hear the reason that there is no debugging code.

   if(sensorBTriggered==true && sensorATriggered==true)
 {

The ==true bit is not needed. true == true evaluates to true. false==true evaluates to false.

White space, on the other hand, makes the code easier to read.

    {count=count+1;

Two comments about this. No coding style calls for code on the same line as the { when the code comes after the {. No style calls for code to be jammed up tight against the {. See the note above about white space.

Second, the language used to program the Arduino is? Nope, not C=C+1. Learn to embrace the compound operators like ++, +=, -=, etc.

How ARE the sensors wired? What does HIGH mean? What does LOW mean? The initial values for the prevStateX variables needs to be whatever the not-interrupted/nobody's-there value is.

Before you try to use the sensor inputs to do anything such as light LEDs, make sure you are capable of reading both sensor inputs correctly and that they are giving you the inputs you are expecting.

(Otherwise known as 'garbage in, garbage out'.)

PaulS:

   if(sensorBTriggered==true && sensorATriggered==true)

{



The ==true bit is not needed. true == true evaluates to true. false==true evaluates to false.

i put in all your corrections, but i sorry couldn't understand this part !
and the problem is still the same after corrections
i hav attached a pic to further explain my setup
and whenever i put in the print ln function my arduino software on pc just hangs up and it becomes very hard to flash anything

PeterH:
Before you try to use the sensor inputs to do anything such as light LEDs, make sure you are capable of reading both sensor inputs correctly and that they are giving you the inputs you are expecting.

(Otherwise known as 'garbage in, garbage out'.)

yeah i re checked the sensors , they r giving 14mV at idle and 4.8V when triggered

but i sorry couldn't understand this part !

if(sensorBTriggered==true && sensorATriggered==true)
 {

should be:

if(sensorBTriggered && sensorATriggered)
 {

and whenever i put in the print ln function my arduino software on pc just hangs up and it becomes very hard to flash anything

Where are you putting it? What are you printing?

okkay i get it :%

and im putting the following code into the main code

void setup() 
{
Serial.begin(9600);
}
void loop() 
{
Serial.println(count);
delay(50);
}

i think its a prob with dku5 , the thing i m using to flash the arduino

the following code is working flawlessly:
but it dosent do what i need it to , one sensor increments count and the other decrements it

const int ledIR =10;
const int ledA= 13;
const int ledB= 3;
const int sensorA=12;
const int sensorB=9;
const int output=11;

int count=0;


void setup() {

pinMode(ledIR,OUTPUT);
pinMode(ledA,OUTPUT);
pinMode(sensorA,INPUT);
pinMode(ledB,OUTPUT);
pinMode(sensorB,INPUT);
pinMode(output,OUTPUT);

tone(ledIR,38000);
}

void loop() {

  if(digitalRead(sensorA)==HIGH)
  {
    count++;
  }
  if(digitalRead(sensorB)==HIGH)
  {
    count--;
  }
  
  if(count>0)
{
  digitalWrite(output,HIGH);
}
  if (count<0)
{
  digitalWrite(output,LOW);
}
  
 digitalWrite(ledA,digitalRead(sensorA)); //for testing senssors
 digitalWrite(ledB,digitalRead(sensorB));
  
}

the following code is working flawlessly:
but it dosent do what i need it to

That's an interesting definition of flawless.

  if(digitalRead(sensorA)==HIGH)
  {
    count++;
  }
  if(digitalRead(sensorB)==HIGH)
  {
    count--;
  }

Good to see that you paid attention to my post about transitions, and why doing something just because the pin is high was going to cause problems.

  if(count>0)
{
  digitalWrite(output,HIGH);
}
  if (count<0)
{
  digitalWrite(output,LOW);
}

And do nothing is count == 0. Good to ignore that case.

const int ledIR =10;
const int ledA= 13;
const int ledB= 3;
const int sensorA=12;
const int sensorB=9;
const int output=11;

int count=0;

byte currStateA;
byte prevStateA = LOW;

byte currStateB;
byte prevStateB = LOW;

void setup() {

pinMode(ledIR,OUTPUT);
pinMode(ledA,OUTPUT);
pinMode(sensorA,INPUT);
pinMode(ledB,OUTPUT);
pinMode(sensorB,INPUT);
pinMode(output,OUTPUT);

tone(ledIR,38000);
}

void loop() {

  currStateA = digitalRead(sensorA);
   if(currStateA != prevStateA)
   {
        if(currStateA == HIGH)
      {
         count++;
      }
   }
   prevStateA = currStateA;
   
   currStateB = digitalRead(sensorB);
   if(currStateB != prevStateB)
   {
      if(currStateB == HIGH)
      {
         count--;
      }
   }
   prevStateB = currStateB;
   

if (count>0)
{
  digitalWrite(output,HIGH);
}
  if (count<=0)
{
  digitalWrite(output,LOW);
}
  
 digitalWrite(ledA,digitalRead(sensorA)); //for testing senssors
 digitalWrite(ledB,digitalRead(sensorB));
  
}

better now ???
but this isnt working =(

correction:

if (count>0)
{
  digitalWrite(output,HIGH);
}
  if (count<0)
{
  digitalWrite(output,LOW);
}
  if (count==0)
{
  digitalWrite(output,LOW); 
}

still not working

Correction:

if (count>0)
{
  digitalWrite(output,HIGH);
} else {
  digitalWrite(output,LOW);
}

still not working

Still not seeing your code.

AWOL:
Still not seeing your code.

here it is (if thats what your asking)

const int ledIR =10;
const int ledA= 13;
const int ledB= 3;
const int sensorA=12;
const int sensorB=9;
const int output=11;

int count=0;

byte currStateA;
byte prevStateA = LOW;

byte currStateB;
byte prevStateB = LOW;

void setup() {

pinMode(ledIR,OUTPUT);
pinMode(ledA,OUTPUT);
pinMode(sensorA,INPUT);
pinMode(ledB,OUTPUT);
pinMode(sensorB,INPUT);
pinMode(output,OUTPUT);

tone(ledIR,38000);
}

void loop() {

  currStateA = digitalRead(sensorA);
   if(currStateA != prevStateA)
   {
        if(currStateA == HIGH)
      {
         count++;
      }
   }
   prevStateA = currStateA;
   
   currStateB = digitalRead(sensorB);
   if(currStateB != prevStateB)
   {
      if(currStateB == HIGH)
      {
         count--;
      }
   }
   prevStateB = currStateB;
   

if (count>0)
{
  digitalWrite(output,HIGH);
} else {
  digitalWrite(output,LOW);
}
  
 digitalWrite(ledA,digitalRead(sensorA)); //for testing senssors
 digitalWrite(ledB,digitalRead(sensorB));
  
}

still not working

The code does something. You expect it to do something. That the code is "still not working" means only that the two somethings are not the same thing.

PM me, and I'll give you an address. You can send me all the stuff, and I'll figure out the first part (what the code does). I'll make some guesses as to the second part (what you expect it to do). I'll make the code work, then, I'll send all the stuff back to you.

Or, you can tell us what the code does, what you expect it to do, and how those differ.

You are presuming that the problem is in the code, while I'm guessing that the problem is in your sensors/wiring.