question about arduino functions

Hi all Of those functions, is there any restriction that some of them must be called in setup()? For example: attachInterrupt(0, FunctPtrs, 0);

What I experienced is when I called this in setup(). Everything is fine but when i called it dynamically somewhere in loop() It causes crash.

Any idea?



crash? where? how do you detect that?

Post your code.


In general you can call anything anywhere you like, however some things are better called once in setup() and other things better called a 1000 times a second in loop().

attachInterrupt() would normally be used in setup(), if it was in loop() it would be conditional based on something and not called every time.

+ What the other guys said.


the code is very large. basically what I did is in loop(), I read in the string, break it into parameter tokens for interrupt, function index (will be used for predefined function pointer array) and the mode.

Based on the values of those token I called attachInterrupt(interrupt, FunctPtrs[index], mode);

I found that when I put a set of parameters into com port, the arduino read them off the com port correctly, then it can call attachInterrupt() one by one and post back a string information to let other end know the attachInterrupt() was called. I find that the first call is OK. When the second call the arduino did not post prompt back correctly and it hangs over there.

But when I call attachInterrupt() in setup with same values, I can call many times as I like. Of cause the parameters are predefined instead read from COM port. I checked the parameters read off COM port and print them back, all are correctly read and the values are correct.


static void (*FunctPtrs[6])();

FunctPtrs[0] = rpm0; FunctPtrs[1] = rpm1; FunctPtrs[2] = rpm2; FunctPtrs[3] = rpm3; FunctPtrs[4] = rpm4; FunctPtrs[5] = rpm5;

void rpm0(){...} void rpm1(){...} void rpm2(){...} void rpm3(){...} void rpm4(){...} void rpm5(){...}

How are you indexing into the array? You aren't doing something silly like sending '3' and using that character as an index are you?

This is the sort of thing that easily happens and why we always need to see the code. You can attach it as a file if it's too large for the forum.

Al least post the part that does this work.


chang: Of those functions, is there any restriction that some of them must be called in setup()?

No. There is no such restriction.