you are right. Just I have modified the code as following:
const int identify_pin = 7;
const int yellowLED_pin = 12;
const int redLED_pin = 11;
unsigned long duration = 200;
duration = pulseIn(identify_pin, HIGH, 20000);
if(duration <= 1)
If i give the arduino DC power supply 5V, the red LED can work. If i give it a PWM(50% @ 50Hz), the yellow one can work. Unfortunately, i have no AC PSU, so only use PWM to test. The test result can proof the code can solve my problem?
Because our PCB (ATmega328P) needs firstly to identify the PSU type(AC or DC) and then with the data it can do the next corresponding things, i define 20ms (max time value, a period of PSU with 50Hz >= period of PSU 60Hz) for timeout, that is, in one period can find, there is a pulse or no pulse.
Do you have any Suggestion to improve codes? Do I need to consider some what parameters or situation?