Page 1 of 1

Arduino UNO and attachInterrupt (soft Interrupt)

Posted: Fri Mar 17, 2017 4:03 pm
by Pascal1966
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

Re: Arduino UNO and attachInterrupt (soft Interrupt)

Posted: Sat Mar 18, 2017 6:41 pm
by exeng
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

Re: Arduino UNO and attachInterrupt (soft Interrupt)

Posted: Mon Mar 20, 2017 8:04 am
by DanV
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.

Re: Arduino UNO and attachInterrupt (soft Interrupt)

Posted: Mon Mar 20, 2017 2:45 pm
by Pascal1966
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!