and special thanks to @chegewara for a previous help in a different topic .
The problem was fixed by inverting the Signal , not by any software changes .
It looks like if you hold an INPUT PIN high , you get that behaviour
but if you invert the signal "which I did" , it works with no problems
Have no idea why is that , but the circuit is here and also the code ,so any one interested ..you can try it .
below is the complete circuit for anyone interested
the 2 10K resistors are connected to the 3.3v supply , sorry if that is not very clear (I'm a software guy)
Power for PIC817 and 2N222 is NOT from ESP
2n222 was used to invert the signal and that solved the problem .
as for PCNT , LEDC .. , Those are very good for HIGH Frequencies .. not for 50Hz , it's an overkill for such low frequency
besides I got double that frequency accurately with 2 ESPs
and again the Sketch to read is simple while loops to measure the wavelength
I did put it in a Task , but that is not even necessary , it can be in the main loop and it will give the same results
Code: Select all
int FC_PIN = 34 ;
void Get_Cycle_Length(void* arrow)
{
for(;;)
{
char buf[8] = "";
volatile int64_t start_time, end_time, cycle_duration ;
noInterrupts() ;
while(digitalRead(FC_PIN) != LOW)
// while(analogRead(FC_PIN) > 0)
{
// If we get here while the phase is high .. wait till the phase is LOW
}
while(digitalRead(FC_PIN) == LOW)
// while(analogRead(FC_PIN) == 0)
{
// Wait till the LOW Phase ends so we start the timer
}
start_time = esp_timer_get_time(); // timer in microSeconds
while(digitalRead(FC_PIN) == HIGH)
// while(analogRead(FC_PIN) > 0)
{
// another wait till the HIGH phase ends
}
while(digitalRead(FC_PIN) == LOW)
// while(analogRead(FC_PIN) == 0)
{
// another wait till we reach the high phase again
}
end_time = esp_timer_get_time() ;
cycle_duration = end_time - start_time ;
interrupts();
Serial.printf("Start_Time : %jd \t End_Time : %jd \t", start_time, end_time);
Serial.printf("Duration for 1 Cycle is : %.3f mSec\n", cycle_duration/1000.0);
//start_time = end_time = cycle_duration = 0 ;
delay(950); // read wavelength every 950mSec
}
}
void setup()
{
Serial.begin(115200);
pinMode(FC_PIN,INPUT);
noInterrupts() ;
delay(1000);
xTaskCreate(Get_Cycle_Length, "Cycle_Length", 8192, NULL,4,NULL);
}
void loop()
{
}
https://streamable.com/me5bo8