Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

Arduino Mini Pro External Interrupt

imranahmed

Advanced Member level 3
Joined
Dec 4, 2011
Messages
721
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
5,852
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
Joined
Nov 7, 2006
Messages
9,185
Helped
1,141
Reputation
2,301
Reaction score
1,116
Trophy points
1,403
Location
Brazil
Activity points
53,371
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

Advanced Member level 3
Joined
Dec 4, 2011
Messages
721
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
5,852
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
Joined
Nov 7, 2006
Messages
9,185
Helped
1,141
Reputation
2,301
Reaction score
1,116
Trophy points
1,403
Location
Brazil
Activity points
53,371
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

Advanced Member level 3
Joined
Dec 4, 2011
Messages
721
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
5,852
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.
 

danadakk

Advanced Member level 1
Joined
Mar 26, 2018
Messages
493
Helped
100
Reputation
199
Reaction score
115
Trophy points
43
Activity points
2,456
variables used in ISR that are not local to ISR should be declared as "volatile"



Regards, Dana.
 

imranahmed

Advanced Member level 3
Joined
Dec 4, 2011
Messages
721
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
5,852
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

Advanced Member level 3
Joined
Dec 4, 2011
Messages
721
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
5,852
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
Joined
Apr 17, 2014
Messages
18,415
Helped
4,112
Reputation
8,224
Reaction score
4,046
Trophy points
113
Activity points
121,223
Hi,

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

Klaus
 

imranahmed

Advanced Member level 3
Joined
Dec 4, 2011
Messages
721
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
5,852
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
Joined
Apr 17, 2014
Messages
18,415
Helped
4,112
Reputation
8,224
Reaction score
4,046
Trophy points
113
Activity points
121,223
Hi,

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

Klaus
 

    imranahmed

    points: 2
    Helpful Answer Positive Rating

KlausST

Super Moderator
Staff member
Joined
Apr 17, 2014
Messages
18,415
Helped
4,112
Reputation
8,224
Reaction score
4,046
Trophy points
113
Activity points
121,223
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

Advanced Member level 3
Joined
Dec 4, 2011
Messages
721
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
5,852
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

Last edited:

danadakk

Advanced Member level 1
Joined
Mar 26, 2018
Messages
493
Helped
100
Reputation
199
Reaction score
115
Trophy points
43
Activity points
2,456
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
Joined
Apr 17, 2014
Messages
18,415
Helped
4,112
Reputation
8,224
Reaction score
4,046
Trophy points
113
Activity points
121,223
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
    Helpful Answer Positive Rating

imranahmed

Advanced Member level 3
Joined
Dec 4, 2011
Messages
721
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Location
Karachi,Pakistan
Activity points
5,852
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.
 

Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top