Could someone explain the ArduinoMega's Interupts

Could someone explain the ArduinoMega’s Interupts , how many they have and how they work?

Thanks!

This should start you off:

http://arduino.cc/en/Reference/AttachInterrupt

Good luck!

Thanks, I will try this.

From NKC,

External Interrupts: 2 (interrupt 0), 3 (interrupt 1), 18 (interrupt 5), 19 (interrupt 4), 20 (interrupt 3), and 21 (interrupt 2). These pins can be configured to trigger an interrupt on a low value, a rising or falling edge, or a change in value. See the attachInterrupt() function for details.

You have 6 interrupt pins vs the regular Arduino with 2 interrupt pins. The MEGA actually has 8 interrupt lines, but two of them are either not wired or used for something else…

I wanted to give priorities to certain subroutines. Like, if Hungry, go charge the battery. If this then that. And “Stop what you are doing for a while. This is more important” So, I can do six of these priorities. Great!

Thanks

:slight_smile:

I think you are missing the point of interrupts. What you want is just simple software and is down to the way you write your main code.

An interrupt should stop normal execution for just a very short time, and then get back to what it was doing, not change the overall program. It is used to monitor infrequent or erratic inputs or to do a regularly poled task.

What I want to do in a robot is to let it roam (lowest priority), but if it’s battery gets low, it should imediately charge the battery before going back to what it was doing. And many tasks will be assign with priority 1,2,3,4,5,6, etc. I have never used interupts before, but this is the way I figure they would work. Is this correct?

but this is the way I figure they would work. Is this correct?

No what you are wanting is called a state machine. Interrupts don’t work like that.

An interrupt is a hardware call to a subroutine that should be exited as soon as possible. Like when data arrives at the serial port, this triggers an interrupt and the interrupt service routine gets the data from the hardware register and stores it in a software buffer and then returns to the main program and you barley saw it go.

A state machine is where you have a variable (state variable) in your case it would have a different value for each behavior you wanted it to have. The main loop would just look at the state variable and run the appropriate bit of code. Then at the end of the loop it would look at stuff and see if the state variable needed to be changed (most times it wouldn’t) and if it did change it. The priority is set in this last bit of code.

thanks for the info. I will look further into State situation. Never really heard of this before now.