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