wind speed reed switch, program code help

So i am setting up an anemometer for a school project, the anemometer uses magnetic reed switches for the wind direction and wind speed, i split up the 2 and worked on a program of each with the plan to combine the 2 at the end. i have finished the wind direction and it works with no problems, but i am having trouble with wend speed.

So far the wind speed program works like this: it adds 1 to a variable every time a magnet goes by (2 magnets in it), the counting of every time a magnet goes by is fine (mostly fine) but the problem comes with calculating the wind speed. the idea i was doing was to count up to 3 seconds then do the calculations and display the result in the serial monitor. then when it goes past 3 seconds it restarts and counts up to 3 seconds again while also setting rotations back down to 0.

So i went through the code and i found the timer to be the problem (sorry for my attempt of a timer in the code below), i have tried using the TimerOne library and millis() but i cant get that to work, either because it doesn't work or i don't know how to use the code. so here is the code below:

const int pulse = 12;                 // Pin connected to reed switch
int rotations = 0;                    // integer for rotations
float windSpeed = 0.00;               // float for wind speed
int countup;                          // counter integer

void setup() 
{
  Serial.begin(9600);
  pinMode(pulse, INPUT_PULLUP);
  countup = 0;
}

void loop()
{
  countup++;
  
  if (countup == 300)                 //when counter gets to 300 it calculats
  {
  windSpeed = rotations * 0.014;      //covert rotations to the windspeed in knots
  }
  else if (countup == 305)            //when counter gets tp 305, it prints the answer and resets
  {
  if (windSpeed > 0)                  //removes unwanted "0.00" values from serial monitor
    {  
    Serial.println(windSpeed);
    }
  delay (5);
  countup = 0;                        //reset timer and rotation
  rotations = 0;
  
  }

  
  int proximity = digitalRead(pulse);   // Read the state of the switch
  if (proximity == LOW)                 // If the pin reads low, the switch is closed.
  {
    rotations++;                        // add to rotations
    delay (10);                         //Wait 50  miliseconds
  }
  
}

i connected the wire to the D12 pin on an arduino Nano and i used a 4k7 resistor for a pullup/pulldown to ground and the other end of the reed switch connected to 3v3 on the nano

the idea i was doing was to count up to 3 seconds

That sounds like a reasonable idea but your code does not do that. You add 1 to countup each time through loop() and do calculations when it reaches 300 which it will do very quickly as loop() repeats several thousand times per second.

Use millis() for timing by saving the start time from millis() then check each time through loop() whether the current time from millis() minus the start time is greater than 3000 milliseconds. If not then keep going round loop counting pulses until the 3 second period ends. When it does do the calculation, save the start time and do it all over again. Do not put blocking code such as delay() in loop() as it needs to run freely.

Have a look at Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

As to detecting pulses, you need to detect when the input is LOW rather than when it becomes LOW. See the StateChangeDetection example in the IDE. Note that you may also need to implement debouncing to avoid false counts occuring. Again, there is an example in the IDE.

UKHeliBob:
you need to detect when the input is LOW rather than when it becomes LOW.

I think that UKHeliBob meant to write "you need to detect when the input becomes LOW rather than when it is LOW."

Take a look at my code here.

vaj4088:
I think that UKHeliBob meant to write "you need to detect when the input becomes LOW rather than when it is LOW."

Ypu are right.

WHOOPSIE on my part