Interrupt Routine malfunctioning

I had written Interrupt Routine so as (1) Interrupt (INT0-PIN2) should get activated on trailing edge of the repetitive pulse of frequency around 1 KHZ. (2) UPON interrupt pin4 changes it's state (3) Observed that Arduino Nano Missing some pulses.

What can be the solution for the same? please advice. For reference screenshot of oscilloscope had been attached herewith. Yellow Trace-Ch1-is Input signal at pin2 and other one -Ch2-is output signal at pin4.

const byte interruptPin=2; 


volatile byte State=LOW;

void setup() {
  noInterrupts();
  pinMode(4,OUTPUT);
  pinMode(interruptPin,INPUT_PULLUP) ;
  attachInterrupt(digitalPinToInterrupt(interruptPin),A,FALLING);
}
  
void loop() {
  interrupts();
  digitalWrite(4, State);
}

void A(){
 noInterrupts();
 State=!State; 
 
 delayMicroseconds(6);
}

The ONLY time you should be turning interrupts on or off is when you copy volatile data

Mark

Thanks Mark

Can you please elaborate

@OP
I have done some adjustments on your codes. Please, see the adjusted codes posted below.

const byte interruptPin = 2;
volatile byte State = LOW;

void setup() 
{
  noInterrupts();
  pinMode(4, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP) ;
  attachInterrupt(digitalPinToInterrupt(interruptPin), A, FALLING); //Mode missing
}

void loop() 
{
 // interrupts();
  digitalWrite(4, State);
}

void A() 
{
 // noInterrupts();  //no need; interrupt (globally) is automatically disabled
  State = !State;
 // delayMicroseconds(6); //why?
}  //during return from ISR, global interrupt logic is auto enabled

Thanks for your replay

"FALLING" had been already part of programme, but missed while copying code to forum

Secondly I had checked by removing delay; but no improvement.

Another observation is that -- trailing edge of Input Signal takes almost 10 Microseconds to change state from 5VDC (Logic-1) to GND (Logic-0)

Can you please suggest further

Right both of you -

First NEVER do this

void setup() 
{
  noInterrupts();

It is point less!

Second NEVER leave the interrupts turned off

Mark

Right both of you - First NEVER do this

noInterrupts(); And Interrupts(); both the Instructions removed but no improvement

Please advice further

AtulDhadiwal:
I had written Interrupt Routine so as

What is the real purpose of this. The code does not make much sense.

Having delayMicroseconds(6); in an ISR seems crazy.

There is no need for noInterrupts() in an ISR as interrupts are OFF automatically and are reinstated when the ISR completes.

Note that other interrupts will be happening in an Arduino - especially for updating micros() and millis()

...R

Hey Robin!
upon occurrence of Interrupt after every defined time i.e. approximately 1 Milliseconds; need to measure Duty-Cycle of another Input waveform-code for which not yet included since I am still not able to detect every trailing edge of the signal fed to Pin 2 i.e. INT0

please advice me further

Thanks

My guess is that the input is bouncing. Your picture does not show enough detail to tell but it does show some small negative-going spikes in the middle of some of the pulses.

If the input bounces the interrupt will occur twice in a few microseconds and the State will toggle twice, producing no net change of State.

Try this version:

const byte interruptPin = 2;
const byte OutputPin = 4;

boolean State = LOW;

void setup()
{
  pinMode(OutputPin, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP) ;
  attachInterrupt(digitalPinToInterrupt(interruptPin), A, FALLING);
}

void loop()
{}

void A()
{
  State = !State;
  digitalWrite(OutputPin, State);
}

AtulDhadiwal:
please advice me further

You first.

It will be much easier to help if you describe the project you are trying to create - including details of the hardware.

At the moment it is a typical XY Problem

...R