counting problem

hello, everyone.
I face a probelm in programming my ardunio using IR sensor to do the following:

If IR sensor goes LOW, start counting until the it goes HIGH in which a varible called duration take the counting value, and then check for four states. While in each state there is a certain command.

The problem is I need counting goes to 0 after duration take its value, but it did not work becuase whenever I add count = 0; in the code the counting did not work.

Here is my code:

int LED1 = 4;
int LED2 = 5;
int LED3 = 6;
int LEDc = 7;
int IR = 2;

boolean currentstate; // Current state of IR input scan
unsigned long duration;
int count = 0;

void setup() {
  pinMode(IR,INPUT);
  Serial.begin(9600);
  pinMode(LED1,OUTPUT);
  pinMode(LED2,OUTPUT);
  pinMode(LED3,OUTPUT);
  pinMode(LEDc,OUTPUT);
  digitalWrite(LED1,HIGH);
  digitalWrite(LED2,HIGH);
  digitalWrite(LED3,HIGH);
  digitalWrite(LEDc,HIGH);
  delay(2000);
  digitalWrite(LED1,LOW);
  digitalWrite(LED2,LOW);
  digitalWrite(LED3,LOW);
  digitalWrite(LEDc,LOW);
}

void loop() {
  count=0;
  currentstate = digitalRead(IR);

  if( currentstate == LOW ) 
  { 
  digitalWrite(LEDc,HIGH);
  count++;
  Serial.print("Time: ");
  Serial.println(count);    //prints time since program started
  delay(1000);   // wait a second so as not to send massive amounts of data
 }
if (currentstate == HIGH){
    digitalWrite(LEDc,LOW);
    duration = count;
    if (duration <=4 && duration >0){
      digitalWrite(LED1,HIGH);
      digitalWrite(LED2,LOW);
      digitalWrite(LED3,LOW);}
      
    else if (duration >4 && duration <=7){
      digitalWrite(LED2,HIGH);
      digitalWrite(LED1,LOW);
      digitalWrite(LED3,LOW);}
      
    else if (duration >7){
      digitalWrite(LED3,HIGH);
      digitalWrite(LED2,LOW);
      digitalWrite(LED1,LOW);}

     else if (duration ==0){
      digitalWrite(LED3,LOW);
      digitalWrite(LED2,LOW);
      digitalWrite(LED1,LOW);}
    }
    Serial.print("duration: ");
    Serial.println(duration);    //prints time since program started
    delay(1000);   // wait a second so as not to send massive amounts of data
}

Barahim:
hello, everyone.
I face a probelm in programming my ardunio using IR sensor to do the following:

If IR sensor goes LOW, start counting until the it goes HIGH in which a varible called duration take the counting value, and then check for four states. While in each state there is a certain command.

The problem is I need counting goes to 0 after duration take its value, but it did not work becuase whenever I add count = 0; in the code the counting did not work.

Here is my code:

int LED1 = 4;

int LED2 = 5;
int LED3 = 6;
int LEDc = 7;
int IR = 2;

boolean currentstate; // Current state of IR input scan
unsigned long duration;
int count = 0;

void setup() {
  pinMode(IR,INPUT);
  Serial.begin(9600);
  pinMode(LED1,OUTPUT);
  pinMode(LED2,OUTPUT);
  pinMode(LED3,OUTPUT);
  pinMode(LEDc,OUTPUT);
  digitalWrite(LED1,HIGH);
  digitalWrite(LED2,HIGH);
  digitalWrite(LED3,HIGH);
  digitalWrite(LEDc,HIGH);
  delay(2000);
  digitalWrite(LED1,LOW);
  digitalWrite(LED2,LOW);
  digitalWrite(LED3,LOW);
  digitalWrite(LEDc,LOW);
}

void loop() {
  count=0;
  currentstate = digitalRead(IR);

if( currentstate == LOW )
  {
  digitalWrite(LEDc,HIGH);
  count++;
  Serial.print("Time: ");
  Serial.println(count);    //prints time since program started
  delay(1000);  // wait a second so as not to send massive amounts of data
}
if (currentstate == HIGH){
    digitalWrite(LEDc,LOW);
    duration = count;
    if (duration <=4 && duration >0){
      digitalWrite(LED1,HIGH);
      digitalWrite(LED2,LOW);
      digitalWrite(LED3,LOW);}
     
    else if (duration >4 && duration <=7){
      digitalWrite(LED2,HIGH);
      digitalWrite(LED1,LOW);
      digitalWrite(LED3,LOW);}
     
    else if (duration >7){
      digitalWrite(LED3,HIGH);
      digitalWrite(LED2,LOW);
      digitalWrite(LED1,LOW);}

else if (duration ==0){
      digitalWrite(LED3,LOW);
      digitalWrite(LED2,LOW);
      digitalWrite(LED1,LOW);}
    }
    Serial.print("duration: ");
    Serial.println(duration);    //prints time since program started
    delay(1000);  // wait a second so as not to send massive amounts of data
}

Is that why you unconditionally set count to zero each time through the loop?

Paul

I need the check being continuously while there is a state of the counting equal to zero. In the same time, I need counting value returns to zero each time the loop starts.

Basically, the idea is simple, there three LEDs:

Green when the counting is less than 5 and more than 0
yellow when the counting is less than 8 and more than 4
red when the counting is more than 7

Then you REALLY need to add Serial.println() showing what is happening rather than just guessing.

Paul

You got this part wrong...

    delay(1000);   // wait a second so as not to send massive amounts of data

The comment should read "Prevent the Arduino from doing anything useful for a million microseconds."

Use millis() for timing, don't invent your own counter.