Arduino MEGA 2560 INTerrupt issue.

Hi, please find the attached file and help me where is an error.

The working of code:

  1. The code execute when INTERRUPT Pin detects the RISING edge of trigger signal.
  2. It goes to ISR called "aa" here, DISABLE The INTERRUPT (cli) and acquire 320 samples and store in the buffer called dataArray[320]
    3)After that it Prints 320 data on serial window and wait for 5 Seconds then ENABLE the Interrupt, and wait for the another Interrupt signal.

The problem i am facing is.

  1. it prints 320 buffer actual data, and 320 buffer data shows 0 only Total 620. means the ISR active for 2 times. or For loop Execute 2 times. i am not able to find out what happenings.

2)Actually it must print 320 Buffer data on serial window. i am giving trigger signal from another UNO that is for 1 mS pulse.

  1. why it prints total 620 data when buffer size is only 320????

  2. whats wrong with the code??

Thanks in advance.

issue.txt (13.7 KB)

ISRs should be short portions of code that run as quickly as possible, which is not the case here. They should be used only when a portion of code absolutely must be executed NOW

Your ISR has 320 delay(10)'s in it amounting to 3.2 seconds. Even if that was a good idea, why not use a for loop instead of 320 copies of the same code ? In addition, delay() and Serial printing do not work inside an ISR or is they do they do not work properly because they depend on interrupts, which are automatically disables when in an ISR so you don't need to do it yourself.

Why use an interrupt in the first place ? Detect the pin going HIGH and run a for loop to collect the data. No need for an ISR in the first place.

That is a crazy way to use an interrupt. An ISR should be designed to complete very quickly - something like

void aa() {
  newInterrupt = true;
}

an the rest of the code should take place outside the ISR.

And you MUST learn about arrays. You have about 650 lines of code in your function and that could probably all be done in 5 lines with much less chance of typing errors.

Then it would be wise to learn how to do timing without using delay(). Have a look at how millis() is used to manage timing without blocking in Several things at a time

...R

i am looking for a solutions.

not suggestions.

The problem i am facing is.

  1. it prints 320 buffer actual data, and 320 buffer data shows 0 only Total 620. means the ISR active for 2 times. or For loop Execute 2 times. i am not able to find out what happenings.

2)Actually it must print 320 Buffer data on serial window. i am giving trigger signal from another UNO that is for 1 mS pulse.

  1. why it prints total 620 data when buffer size is only 320????

  2. whats wrong with the code??

EJLED:
4) whats wrong with the code??

It's absolute crap.

Dear whandall,

if its absolute crap. then please right this code in best quality. atleast i would learn from u. and would be know atleast how to not write the crap.

EJLED:
i am looking for a solutions.

not suggestions.

If you want someone to write code for you please ask in the Gigs and Collaborations section of the Forum and be prepared to pay.

The rest of the Forum is for helping people write their own programs - hence the suggestions.

...R

EJLED:
if its absolute crap. then please right this code in best quality.

Nope.

Learn about interrupts Gammon Forum : Electronics : Microprocessors : Interrupts and how to use them.

But, as already stated, your problem does not need interrupts.
Using interrupts for keys makes it harder to debounce the keys,
but you forgot that you have to handle debouncing anyway.

The IDE has examples for reading a key and detecting a state change on a pin.