Arduino UNO and attachInterrupt (soft Interrupt)

For the discussion of Arduino related topics.

Moderator: phalanx

Arduino UNO and attachInterrupt (soft Interrupt)

Postby Pascal1966 » Fri Mar 17, 2017 4:03 pm

I am brand new to Arduino AND C programming. I use the IDE from arduino.org, rev 1.8.1
I want to attach interrupts to all my INPUT Pins. They are connected to buttons.
It does not want to compile, and seems to say that this is not available on a UNO. Is that correct?
It would be great if someone could look into the abstract below:
Abstract of my code:

#include <SoftwareSerial.h>
#include <PinChangeInt.h>
#include <stdio.h>

void setup() {
attachInterrupt(9, IntLevel1Down, FALLING); //compiler says not declared in this scope...
attachInterrupt(9, IntLevel1Up, RISING);
attachInterrupt(10, IntLevel2Up, RISING);
attachInterrupt(11, IntLevel3Up, RISING);
attachInterrupt(12, IntLevel4Up, RISING);
attachInterrupt(13, IntLevel5Up, RISING);
attachInterrupt(13, IntLevel5Down, FALLING);
}
void loop(){ whatever it's supposed to do in te loop}

void IntLevel1Up(){ whatever it's supposed to do }
void IntLevel2Up(){whatever it's supposed to do }
void IntLevel3Up(){whatever it's supposed to do }
void IntLevel4Up(){whatever it's supposed to do }
void IntLevel5Up(){whatever it's supposed to do }
void IntLevel1Down(){ whatever it's supposed to do }
void IntLevel5Down(){whatever it's supposed to do }

Cheers! Pascal
Pascal1966
 
Posts: 10
Joined: Fri Mar 17, 2017 3:47 pm

Re: Arduino UNO and attachInterrupt (soft Interrupt)

Postby exeng » Sat Mar 18, 2017 6:41 pm

Are is a summary of the interrupts pins available to the UNO as well as other Arduino boards...
https://www.arduino.cc/en/Reference/AttachInterrupt
User avatar
exeng
 
Posts: 21
Joined: Thu Jan 05, 2017 1:37 pm

Re: Arduino UNO and attachInterrupt (soft Interrupt)

Postby DanV » Mon Mar 20, 2017 8:04 am

You don't need interrupts to detect if a button is pressed.
The UNO runs through the loop() function many hundreds of thousands of times per second.
Just perform pinVal = digitalRead(pin#) to determine on or off state.
DanV
 
Posts: 254
Joined: Sat Apr 25, 2015 9:29 am

Re: Arduino UNO and attachInterrupt (soft Interrupt)

Postby Pascal1966 » Mon Mar 20, 2017 2:45 pm

I found the problems and fixed them:

1)- Some libraries needed:
#include <PinChangeInterrupt.h>
#include <PinChangeInterruptBoards.h>
#include <PinChangeInterruptPins.h>
#include <PinChangeInterruptSettings.h>
#include <PinChangeInt.h>
#include <stdio.h>

Note: I also had some libraries that were not compatible with UNO. I had to remove them. I forgot their names.... but what I found online about this was somewhat misleading or unclear.
Note: UNO has only TWO hardinterrupt pins, but any input pin can also be setup as interrupt through Soft-Interrupt.
Example in my application:
PCintPort::attachInterrupt(9, IntLevel1Up, RISING); // Attach all the interrupts to relevant pins
PCintPort::attachInterrupt(10, IntLevel2Up, RISING);
PCintPort::attachInterrupt(11, IntLevel3Up, RISING);
PCintPort::attachInterrupt(12, IntLevel4Up, RISING);
PCintPort::attachInterrupt(13, IntLevel5Up, RISING);
PCintPort::attachInterrupt(13, IntLevel5Up, FALLING);

2)- I had some syntax errors with things like " } " ;-)

3)- I had multiple detection for each interrupt (electrical and/or mechanical bounces) and my code was not immune to that: fixed.

4)- TRUE, I may not need interrupts for my application as the uP is fast enough, but interrupts provide more info since you detect a ONE TIME event, and can differentiate FALLING from RISING and do different things accordingly.
Also. if you have waiting loops or routines that take time, digitalread could miss an event short enough (my events are detected only 4 times per loop by digitalRead). Interrupt will not.

Cheers!
Pascal1966
 
Posts: 10
Joined: Fri Mar 17, 2017 3:47 pm


Return to Arduino

Who is online

Users browsing this forum: No registered users and 2 guests