I have written a code that detects an incoming pulse (square) of 7.875 kHz to trigger the output High for the entire duration of pulse, and trigger the output low if the pulse is off or not detected.
CODE
// Define pins
volatile int pulse1 = 2; //incoming pulse 1 on pin 2
const int ledPin = 13; //output to trigger LED
void setup() {
pinMode(pulse1, INPUT);
pinMode(ledPin, OUTPUT);
// Timer Setup
TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // same for TCCR1B
TCNT1 = 65282; // initialize counter value
TCCR1B |= (1<<CS10); // set prescaler to 8 bit
TIMSK1 |= (1 << TOIE1); // enable timer overflow interrupt
}
ISR(TIMER1_OVF_Vect){
TCNT1 = 65282;
attachInterrupt(digitalPinToInterrupt(pulse1), ISR_UV2, FALLING);
}
void loop() {
attachInterrupt(digitalPinToInterrupt(pulse1), ISR_UV1, RISING);
}
// Interrupt routines
void ISR_UV1(){
digitalWrite(ledPin, HIGH); // Send high signal
}
void ISR_UV2(){
digitalWrite(ledPin, LOW); // Send low signal
}
Is it the correct approach, or am I doing it wrong. One last thing that is bothering me is, every time I connect a wire (not connected to anything) to the input pin 2, it sends a high signal, and keeps the signal high until the Arduino is reset. The pulse is a square wave 50% duty cycle generated by a device. I dont need to measure it or count it. I am just using it as a trigger to send a HIGH signal when it occurs. So it stays on (occurs) for 25ms & turns off during which its freq. is 7.875MHz & then it is repeated by the device according to the device parameters. I am aware it is a very short pulse but for the purpose of project nothing else can be used as the trigger
Any help will be appreciated.