# Arduino Mini Pro External Interrupt

#### imranahmed

Please let me know can we write long code in External interrupt function "modeChange"
attachInterrupt(digitalPinToInterrupt(interruptPinforEnter), modeChange, FALLING);

Code:
void modeChange() {
mode++;
if(mode == 9)
{
mode = INIT_MODE;
}
}
I wrote more " if " conditions below " if " condition mode==9 , but whole function could not work when I removed additional conditions I added then it worked.
I want to write another " if " condition.

Code:
void modeChange() {
mode++;
if(mode == 9)
{
mode = INIT_MODE;
}

if(autoLight == 1)
{
lastTimeOut = millis();
lcd.backlight();
lcd.display();
autoLight = 0;
}

}

#### andre_teprom

##### Super Moderator
Staff member
You are not doing the right way, no function should be placed inside interrupt handler.
Put the above snippet ( "if(autoLight..." ) within the loop() function.

#### imranahmed

You are not doing the right way, no function should be placed inside interrupt handler.
Put the above snippet ( "if(autoLight..." ) within the loop() function.
Code:
/Interrupts using Arduino
//Circuit Digest

#include<LiquidCrystal.h>                        // Including lcd display library
LiquidCrystal lcd (7,8,9,10,11,12);              // Define LCD display pins RS,E,D4,D5,D6,D7

volatile int output = LOW;
int i = 0;

void setup()

{
lcd.begin(16,2);                              //  setting LCD as 16x2 type
lcd.setCursor(0,0);
lcd.print("CIRCUIT DIGEST");
lcd.setCursor(0,1);
lcd.print("ArduinoInterrupt");
delay(3000);
lcd.clear();
pinMode(13,OUTPUT);

attachInterrupt(digitalPinToInterrupt(2),buttonPressed1,FALLING);  //  function for creating external interrupts at pin2 on Falling (HIGH to LOW)
attachInterrupt(digitalPinToInterrupt(3),buttonPressed2,FALLING);  //  function for creating external interrupts at pin3 on Flling (HIGH to LOW)

}

void loop()
{
lcd.clear();
lcd.print("COUNTER:");
lcd.print(i);
++i;
delay(1000);
digitalWrite(13,output);     //Turns LED ON or OFF depending upon output value
}

void buttonPressed1()           //ISR function excutes when push button at pinD2 is pressed
{
output = LOW;                //Change Output value to LOW
lcd.setCursor(0,1);
lcd.print("Interrupt 1");
}
void buttonPressed2()           //ISR function excutes when push button at pinD3 is pressed
{
output = HIGH;               //Change Output value to HIGH
lcd.setCursor(0,1);
lcd.print("Interrupt2");
}
Please see above code copy from CircuitDigest website, I followed same thing as they did.
ISR function is outside loop.

#### andre_teprom

##### Super Moderator
Staff member
Please see above code copy from CircuitDigest website, I followed same thing as they did.
Review above reply, I didn't mean to add the whole code within loop(), but rather the snippet if(autoLight == 1) { ... }
Anyway, don't rely on everything you see on the Web. BTW, are you providing any kind of hardware debounce for the button, e.g RC ?

#### imranahmed

Review above reply, I didn't mean to add the whole code within loop(), but rather the snippet if(autoLight == 1) { ... }
Anyway, don't rely on everything you see on the Web. BTW, are you providing any kind of hardware debounce for the button, e.g RC ?
for debounce I used
Code:
if (millis() - lastFire < 300) { // Debounce
return;
}
lastFire = millis();
no hardware used.
How can I placed snippet if(autoLight == 1) { ... } within the loop because I want to execute this snippet when button is pressed at pin 2 external interrupt. Only mode++ is executing but when I add another code in modeChange function it wont work.

variables used in ISR that are not local to ISR should be declared as "volatile"

Regards, Dana.

imranahmed

### imranahmed

points: 2

#### imranahmed

variables used in ISR that are not local to ISR should be declared as "volatile"

Regards, Dana.
Thanks bro it is working. Volatile is very important today I know it. Again Thanks.

#### imranahmed

Please let me know I am using Arduino UNO 16MHz and increment a count variable when external interrupt occurred. Incoming external interrupt signal from Function Generator of 1Hz and display count value on Virtual Terminal (baud rate 9600). When I gone to increase frequency gradually 1Hz to up when reaches 800Hz or above the values of count variable skipping, why is it skipping? Please let me know?

#### KlausST

##### Super Moderator
Staff member
Hi,

How many byte do you transfer via UART?
How long does it take to transfer them?

Klaus

#### imranahmed

Code:
volatile unsigned long count = 0;
volatile unsigned long prev_count = 0;
const byte int_0_pin = 2;
void setup() {
Serial.begin(9600);
pinMode(int_0_pin, INPUT_PULLUP);
interrupts();
attachInterrupt(digitalPinToInterrupt(int_0_pin), countInt0,CHANGE);
}
void loop() {
if(count > prev_count){
Serial.println(count);
prev_count = count;
}
}
void countInt0(){
count++;
}
I made a sketch above, count variable and println--> CR+LF , total 3 bytes.
Function Generator frequency is set to 1Hz at INT0 pin2 of Arduino UNO.

#### KlausST

##### Super Moderator
Staff member
Hi,

"count" is declared as "long". How many bytes is a "long"?

Klaus

### imranahmed

points: 2

#### imranahmed

Hi,

"count" is declared as "long". How many bytes is a "long"?

Klaus
long is 8 bytes

#### KlausST

##### Super Moderator
Staff member
Hi,

so with each count tick you send 8 bytes + CR + LF. Is this correct?
How long does it take for the UART to transmit 8 bytes + CR + LF?

Klaus

#### imranahmed

long is 8 bytes
Sorry for Arduino it is 4 bytes
--- Updated ---

Hi,

so with each count tick you send 8 bytes + CR + LF. Is this correct?
How long does it take for the UART to transmit 8 bytes + CR + LF?

Klaus
I set baud rate 9600 means 9600 bits per second it sends 4 bytes of count 1 byte of CR and 1 byte of LF.
count --> 4 bytes x 8 = 32bits
CR--> 1 bytes x 8 = 8bits
LF--> 1 bytes x 8 = 8bits
---------
Total bits send = 48 bits

9600 bits takes 104us , 1 bit takes 1.08x10^-8 seconds multiply by 48 it will 0.00000052 seconds.
Is this right?

#### Attachments

• 74.3 KB Views: 1
Last edited:

Suggestions -

1) Make interrupt priority high in interrupt controller
2) Use pointers wherever possible
3) Any variables not local to ISR routine should be declared "volatile"
4) When ever possible in ISR do NOT call f()'s. That creates a lot of
stack push. Ideally an ISR should set a flag, exit, and process the ISR
outside the ISR, in main.

Regards, Dana.

#### KlausST

##### Super Moderator
Staff member
Total bits send = 48 bits

9600 bits takes 104us , 1 bit takes 1.08x10^-8 seconds multiply by 48 it will 0.00000052 seconds.
Is this right?
Where does 1.08 x 10^-8 come from?
Neither 1.08 is clear, nor 10^-8.

1 us = 1 microsecond = 1 x 10^-6 s. = 0.000001s
104us = 104 x 10^-6 s = 1.04 x 10^-4 s = 0.000104s
48 bit means 48 x 1.04 x 10^-4 s = 49.92 x 10^-4 s = 0.004992s = 4.99ms
You had at least 3 mistakes in this simple math.
You really need to learn this, because you will need this all the time when you want to write programs.

This also means it fits about 200 times in one second.
So not 800Hz is the limit, but 200Hz

Klaus

### imranahmed

points: 2

#### imranahmed

Where does 1.08 x 10^-8 come from?
Neither 1.08 is clear, nor 10^-8.

1 us = 1 microsecond = 1 x 10^-6 s. = 0.000001s
104us = 104 x 10^-6 s = 1.04 x 10^-4 s = 0.000104s
48 bit means 48 x 1.04 x 10^-4 s = 49.92 x 10^-4 s = 0.004992s = 4.99ms
You had at least 3 mistakes in this simple math.
You really need to learn this, because you will need this all the time when you want to write programs.

This also means it fits about 200 times in one second.
So not 800Hz is the limit, but 200Hz

Klaus
Ohhh I see 104us is for 1 bit transfer time for 9600 baud rate.
Thank you for correction.