very good with everyone first, my English is not good, I still have a basic level.
My question is that I try to detect the frequency of the network, using a zero crossing detector. which looking online I found a code. what ise is to increase conditions with IF ELSE to put a maximum value and minimum. in the maximum value it works correctly but in the minimum value the led remains on. That error will be true. please help me I will thank you forever.
the code is copyrighted I just try to increase some things
// period of pulse accumulation and serial output, milliseconds
const int MainPeriod = 100;
long previousMillis = 0; // will store last time of the cycle end
volatile unsigned long prevMicros=0;
volatile unsigned long duration=0; // accumulates pulse width
volatile unsigned int pulCnt=0;
// interrupt handler
void contadorcero() {
unsigned long curMicros = micros();
duration += curMicros - prevMicros;
prevMicros = curMicros;
pulCnt++; }
void setup()
{
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
Serial.begin(19200);
attachInterrupt(0, contadorcero, RISING);
}
void loop()
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= MainPeriod)
{
previousMillis = currentMillis;
// frecuency
float freq = 1e6 / float(duration) * (float)pulCnt;
Serial.print("Frec:");
Serial.print(freq);
duration = 0;
pulCnt = 0;
delay(500);
// frecuency
// the frequency is read with decimals (60 hz * 2 = 120.00 hz)
if (freq >= 120.05 ) // maximum value established
{
digitalWrite(5, HIGH);
}
else if (freq <= 119.97 )//minimum value established
{
digitalWrite(6, HIGH);
}
else {
digitalWrite(5, LOW);
digitalWrite(6, LOW);
}
}
}