Arduino_promini_program_continuously_crashing_with_External_INTERRUPT1_enable

Hi,
My question is how come atmega328P can print the statement which is in setup() more than one time ?

Here i will attach two codes in which i have enable both external interrupts.When there is signal come at external interrupt 1 it should increment my variable which is in ISR and print the value of that variable. But what is happening ,when ever signal come on the pin it prints the message again and again which i have written in setup().How? and WHY?

I know that use of Serial.println() in ISR is not appropriate but i am just using for debugging purpose it will not be present in my final design.And i have also tried with move that Serial.println() in loop() but still facing same issue.

There is also one other program(working code) in which i have use same logic to print the same variable it works fine in that code.Then how come the same logic is not acceptable for my nonworking code?

Did i miss something which i dont even aware of even after working on arduino since almost 1year.

Plz guide me....

working_code.ino (2.26 KB)

non_working_code.ino (521 Bytes)

You missed the how-to-use-this-forum part where it says to use code tags to show your code.

I won't click on your .ino files because that will open my Arduino IDE that will want to save them in projects folder, and I don't want to get through the chore of deleting other's people folders.,ino files from my project directory just because you didn't care to read the forum how-to and follow the advice there that states: " use code tags "

I know that use of Serial.println() in ISR is not appropriate but i am just using for debugging purpose

It is more than inappropriate. Until recent versions of the IDE it simply didn't work.

What version of the IDE are you using ?

Hi,
My question is how come atmega328P can print the statement which is in setup() more than one time ?

Here i will attach two codes in which i have enable both external interrupts.When there is signal come at external interrupt 1 it should increment my variable which is in ISR and print the value of that variable. But what is happening ,when ever signal come on the pin it prints the message again and again which i have written in setup().How? and WHY?

I know that use of Serial.println() in ISR is not appropriate but i am just using for debugging purpose it will not be present in my final design.And i have also tried with move that Serial.println() in loop() but still facing same issue.

There is also one other program(working code) in which i have use same logic to print the same variable it works fine in that code.Then how come the same logic is not acceptable for my nonworking code?

Did i miss something which i dont even aware of even after working on arduino since almost 1year.

Plz guide me....
.............................................working code......................................................................

volatile unsigned int pulse = 0;

volatile unsigned int timer_tick = 0;

double accumulated_water = 0.0;

//--------------------------------------------------------------------------------------------
void external_interrupt_setup(void)
{
 EICRA = 0x0A;                               //falling edge on INT0(bit 0th and 1st) and the falling edge on INT1(bit 2nd and 3rd) generates interrupt...
 EIMSK |= 0x03;                              //INT0(0th bit) and INT1(1st bit) interrupt mask enable...
}
void timer1config(void)
{
 //cli();
 TCCR1A = 0;                                 //clear timer/counter control register A.
 TCCR1B = 0;                                 //clear timer/counter control register B.
 TCNT1 = 0;                                  //clear count register.
 OCR1A = 15624;                              //To generate Timer compare interrupt every 1 sec when CPU clock frequency is 1000000Hz(1MHz)
 TCCR1B = 0x0B;                              //turn on CTC mode(clear timer on compare) as well as prescaler(64)[1000000/64 = 15625].[CS10(bit 0) and CS12(bit 2) for prescaler,WGM12(bit 3) for mode selection]
 TIMSK1 = 0x02;                              //enable timer compare interrupt(using OCIE1A(1st bit)).
 //sei();
}
void setup()
{
 Serial.begin(9600);
 external_interrupt_setup();
 timer1config();
 Serial.println(F("HELLO WORLD"));
}

void loop()
{

 if(pulse >= 428)
 {
   accumulated_water++;
   pulse = pulse-428;
 }
 
 if (timer_tick == 60)
 {
   accumulated_water += (float)pulse / 428.0;
   Serial.print(F("WATER = "));
   Serial.print(accumulated_water);
   Serial.println(F("Lit/Min."));
   timer_tick = 0;
   pulse = 0;
 }
}
ISR(TIMER1_COMPA_vect)
{
 timer_tick++;

}


ISR(INT1_vect)
{
 pulse++;
 Serial.println(pulse);
}

...............................................working code END.................................................................

.................................................nonworking code.................................................................

volatile unsigned int pulse = 1; //using for calculating number of pulses...
void external_interrupt_setup(void)
{
 EICRA = 0x0A;                               //falling edge on INT0(bit 0th and 1st) and the falling edge on INT1(bit 2nd and 3rd) generates interrupt...
 EIMSK |= 0x03;
}
void setup()
{
 Serial.begin(9600);
 external_interrupt_setup();
 Serial.println(F("HELLO_WORLD"));
 Serial.println(pulse);
}
void loop()
{

}
ISR(INT0_vect)
{
 pulse++;  
  if(pulse++%10 == 0)
   Serial.println(pulse);
}

..................................................non working code END......................................................

i am using 1.6.5 IDE.

My question is how come atmega328P can print the statement which is in setup() more than one time ?

Clearly that happens because the Arduino is resetting, causing setup() to run again.

I know that use of Serial.println() in ISR is not appropriate but i am just using for debugging purpose it will not be present in my final design.

But it IS present in the code that is causing problems. Get rid of it, and see if the problem goes away.

i am using 1.6.5 IDE.

That version blocks if the outgoing serial buffer gets full, waiting for room. Room is made in the serial buffer by shifting data out, but that happens only when an interrupt happens, but interrupts don't happen when an interrupt service routine is running. So, if the buffer gets full, the Arduino will seem to hang.

Thank you so much for your reply. I will try the way u suggested.

I know that use of Serial.println() in ISR is not appropriate ...

More like, it will hang or crash.

So your question is, "I am doing something I am told not to do. Why does my program crash?"