Finding the direction of wind with

Hi, i’m doing a program in wich i have to telle in wich direction the wind is blowing using an ir led and two optoschmitt detectors.

I’m currently using interrupts but i can’t figure why my program is not working. I’m using an old computer fan for simulation and no matter in wich direction i place it, my arduino tells me the wind is going from the left to the right then the rigth to the left even if i don’t change the direction of my fan .

My code looks like this

attachInterrupt(digitalPinToInterrupt(2),dg, RISING);
 
  attachInterrupt(digitalPinToInterrupt(3),dd,RISING); 

   

    if (timeg < timed) // Si la valeur de la variable timeg est superieure à la variable timed 
    {
      Serial.println("From right to left"); // Write "from right to left"
      
    }

    if (timeg > timed) 
    {
     Serial.println("From left to right"); // write "from left to right "
     
    }
    

 }

 void dg() // fonction dg
 {
 timeg = 0; 
  
timeg = millis(); //associate the millis value to timeg variable
  
 }

 void dd() //fonction dd
 {

 timed = 0;
 
  timed = millis(); //associate the millis value to timed variable
 }

Please post you complete program including the variable declarations. Are the variables used in the ISRs declared as volatile ?

The two pulses alternate: D, G, D, G, D, G... Your code is looking at the time stamps on the latest two pulses. If it checks when the latest pulse was a D it says Right To Left. If it checks when the latest pulse was a G it says Left To Right. Naturally it will report randomly one or the other.

I think your two detectors have to be set up as a quadrature pair and used as you would a shaft encoder. When one sensor changes you need to check the state of the OTHER sensor at that time to see if the rotation is clockwise or counter-clockwise.