Love UECIDE? Why not consider supporting the development?

[SOLVED] attachInterrupt()

Share your sketches here for others to play with and enjoy.

[SOLVED] attachInterrupt()

Postby jwzumwalt » Tue Mar 01, 2016 11:37 pm

I am trying to get attachInterrupt() to work in Pinguino. The interrupt is supposed to toggle the LED at reqular intervals.

The Arduino format for using attachInterrupt() is shown at

https://www.arduino.cc/en/Reference/AttachInterrupt

a simple example is shown

Code: Select all
int pin = 13;
volatile int state = LOW;

void blink() {
    state = !state;
}

void setup() {
    pinMode(pin, OUTPUT);
    attachInterrupt(pin, blink, CHANGE);
}

void loop() {
    digitalWrite(pin, state);
}



I can't get this to work. Does anyone have a successful use of attachInterrupt()?
Last edited by jwzumwalt on Mon Mar 07, 2016 7:24 pm, edited 1 time in total.
Thanks,
jwzumwalt
(\__/)
(='.'=)
(")_(")
User avatar
jwzumwalt
 
Posts: 19
Joined: Tue Dec 01, 2015 6:00 am
I use UECIDE on: Windows
 

Re: How2 interrupt attachInterrupt()

Postby matt » Tue Mar 01, 2016 11:51 pm

If you're using CHANGE then you have no chance of it working. The PIC32 doesn't have the concept of CHANGE on interrupt pins, only RISING or FALLING.

If you want CHANGE you could use the ChangeNotification facility instead (which works on many more pins - modern PIC32s have it on all pins). It's not quite as responsive as interrupt pins, but it's still better than manually polling.

There's a library that supports non-PPS chips in the plugin manager, or if you like to do things manually it's here:

https://github.com/MajenkoLibraries/ChangeNotification

Code: Select all
attachInterrupt(CN_3, myFunction, CHANGE);

If it's a PPS chip (MX1xx/2xx, etc) then you'll have to do it all manually I'm afraid - there's no library support yet.

Or... just don't use CHANGE.
Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Universal IDE: http://uecide.org
"I was trying to find out if it was possible to only eat one Jaffa Cake. I had to abandon the experiment because I ran out of Jaffa Cakes".
matt
Site Admin
 
Posts: 1286
Joined: Sun Jul 07, 2013 11:37 am
I use UECIDE on: Linux
 

Re: How2 interrupt attachInterrupt()

Postby jwzumwalt » Wed Mar 02, 2016 6:35 am

Thank you for the help - this is a Olimex PIC32 Micro.

But I still get an error; it does not know what "blink" is.

int pin = 13;
volatile int state = LOW;

void blink() {
state = !state;
}

void setup() {
pinMode(pin, OUTPUT);
attachInterrupt(pin, blink, FALLING);
}

void loop() {
digitalWrite(pin, state);
}

So I guess really the greater question is:
How do I have an external pin event trigger a interrupt routine?
I'm not picky about /RISING/FALLING, I'll settle for anything.
Thanks,
jwzumwalt
(\__/)
(='.'=)
(")_(")
User avatar
jwzumwalt
 
Posts: 19
Joined: Tue Dec 01, 2015 6:00 am
I use UECIDE on: Windows
 

Re: How2 interrupt attachInterrupt()

Postby GastonLagaffe » Wed Mar 02, 2016 9:59 am

Salut,

interrupts are not assigned to a pin with the attachInterrupt routine. You assign the interrupt routine to a given interrupt number. The pin assigned to that interrupt is depending on the chip/board you are using!

You have to identify the pin linked to an interrupt on your board and then assign the routine to that interrupt and conect your external trigger to the pin identified.

If I read the documentation right, pin13 is assigned to INT4 and pin14 to INT1, so you should call:
Code: Select all
attachInterrupt(1,blink,FALLING);


and trigger it via pin 13

Ciao, Mathias
GastonLagaffe
 
Posts: 57
Joined: Thu May 15, 2014 4:22 pm
Location: Germany
I use UECIDE on: Windows
 

Re: How2 interrupt attachInterrupt()

Postby matt » Wed Mar 02, 2016 11:09 am

I see no error there - other than the use of pin 13 as your interrupt.

Two things:

1. you don't use the pin number but the interrupt number, as Gaston has mentioned
2. you're using pin 13 as an output - how can you use it as an interrupt at the same time? At best you'll get some kind of feedback making it oscillate...
Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Universal IDE: http://uecide.org
"I was trying to find out if it was possible to only eat one Jaffa Cake. I had to abandon the experiment because I ran out of Jaffa Cakes".
matt
Site Admin
 
Posts: 1286
Joined: Sun Jul 07, 2013 11:37 am
I use UECIDE on: Linux
 

Re: How2 interrupt attachInterrupt()

Postby jwzumwalt » Thu Mar 03, 2016 5:01 am

Still no success...

I tried 1 thru 14 without success... any ideas?

int pin = 13;
volatile int state = LOW;

void blink() {
state = !state;
}

void setup() {
pinMode(pin, OUTPUT);
attachInterrupt(<1 thru 14>,blink,LOW);
}

void loop() {
digitalWrite(pin, state);
}
Thanks,
jwzumwalt
(\__/)
(='.'=)
(")_(")
User avatar
jwzumwalt
 
Posts: 19
Joined: Tue Dec 01, 2015 6:00 am
I use UECIDE on: Windows
 

Re: How2 interrupt attachInterrupt()

Postby matt » Thu Mar 03, 2016 12:50 pm

Interrupts can only be between 0 and 4 and have to be on the right pin they are assigned to. I will check out which pins they are tomorrow when I get back home.

Sent from my SM-T555 using Tapatalk
Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Universal IDE: http://uecide.org
"I was trying to find out if it was possible to only eat one Jaffa Cake. I had to abandon the experiment because I ran out of Jaffa Cakes".
matt
Site Admin
 
Posts: 1286
Joined: Sun Jul 07, 2013 11:37 am
I use UECIDE on: Linux
 

Re: How2 interrupt attachInterrupt()

Postby jwzumwalt » Mon Mar 07, 2016 1:50 am

I found out that the Olimex micro has interrupts located like this...
Interrupt 0 is connected to the button
Interrupt 1 is connected to CONN1-14
Interrupt 2 is connected to UEXT-6
Interrupt 3 is connected to UEXT-5
Interrupt 4 is connected to CONN1-13
Last edited by jwzumwalt on Mon Mar 07, 2016 7:20 pm, edited 1 time in total.
Thanks,
jwzumwalt
(\__/)
(='.'=)
(")_(")
User avatar
jwzumwalt
 
Posts: 19
Joined: Tue Dec 01, 2015 6:00 am
I use UECIDE on: Windows
 

Re: How2 interrupt attachInterrupt()

Postby GastonLagaffe » Mon Mar 07, 2016 9:39 am

Salut,

the following code changes the LED state when the button is pressed, the second led is blinking to show it works:
Code: Select all
volatile int state = 1;

void blink() {
   state = !state;
}

void setup() {
   pinMode(PIN_LED1, OUTPUT);
   pinMode(PIN_LED2, OUTPUT);
   attachInterrupt(0, blink, FALLING);
}

void loop() {
   digitalWrite(PIN_LED2, state);
  digitalWrite(PIN_LED1,!digitalRead(PIN_LED1));
  delay(500);
}


Ciao, Mathias
GastonLagaffe
 
Posts: 57
Joined: Thu May 15, 2014 4:22 pm
Location: Germany
I use UECIDE on: Windows
 

Re: How2 interrupt attachInterrupt()

Postby jwzumwalt » Mon Mar 07, 2016 7:18 pm

The the code you supplied worked!

I usually use Pinguino.cc IDE, but the attachInterrupt() currently (March 2016) does not work in Pinguino.cc.

I did discover that UECIDE uses slightly different designation than Pinguino.cc

For example the green led on the Olimex sheet says it is attached to IDE pin 32 and this works with Pinguino.cc. However UECIDE does not recognize pin 32 for some reason.

Thanks.
Thanks,
jwzumwalt
(\__/)
(='.'=)
(")_(")
User avatar
jwzumwalt
 
Posts: 19
Joined: Tue Dec 01, 2015 6:00 am
I use UECIDE on: Windows
 
 

Return to Sketches

Who is online

Users browsing this forum: No registered users and 1 guest

cron