loading

Interrupts are important we use them all the time in our daily life. Much of our electronics use them as do how the way our brain functions. Imagine being at a restaurant which is on fire and your " fight or flight" response doesn't kick in until after you have finished and paid for your meal!

Watch the video for a very graphic example of how interrupts can and should be used then come back to this Instructable to see just how it is done and when it should be done.

About Interrupts...

Step 1: About Interrupts

Interrupts are everywhere in our daily lives. When two adults are talking and the child interrupts them - they respond to the child and then continue their conversation where they left off. Or, you are on your way to make a coffee and the phone rings, interrupting you. You answer the phone and then when done, you go to make your coffee. That is how interrupts work. The two biggest interrupts to modern life are the Pokemon Go game and Selfies.

Within the Arduino we set an interrupt to listen for an event such as a button being pushed or a smoke sensor detecting smoke. Then the interrupt is run, calling a function we have written especially for that event.

Interrupts should be used as much as possible for the essential aspects of your project. The ejection seat, the avoidance detector, the fire alarm, and the emergency braking system should all run on interrupts. No matter what your project is doing emergencies should always take precedence.

On to the Instructable...

Step 2: The Instructable Without Using Interrupts

We set up 8 sensors only one of which requires human input - a digital button. In our Instructable we used the following sensors: a photoresistor, a temperature sensor, a digital button, four shock sensors, and another temperature sensor.

All the sensors run in series and require no human intervention to run. The digital button represents an emergency action such as a kill switch.

In the bottom right of the image you see the hardware setup. As we are focusing on interrupts I will not show the details of connecting up the sensors as that is not the important feature of this Instructable.

On to the serial code...

Step 3: The Serial Code.

In truth even using interrupts the code we use runs sequentially, that is it never runs in parallel we would need two or more processors for that to happen.

void loop() {
GetSensor1Data();

GetSensor2Data();

GetSensor3Data();

GetSensor4Data();

GetSensor5Data();

GetSensor6Data();

GetSensor7Data();

GetSensor8Data(); }

Our main loop calls the 8 functions, each of which gathers and displays the individual sensor's data. There are no delays inserted so the loop runs at its highest speed. So we are multi-tasking but it is not pre-emptive multi-tasking. That means each function is not given the same amount of time to use the hardware. In this example each function takes as long as it needs.

If GetSensor1Data() contained a never ending loop then none of the other functions would ever run. That means We would never get the Kill notification from sensor 3. Nevertheless, even as it is without any loops in the sensor functions we will only get the Kill notification if we press the digital button whilst the program is actually within the GetSensor3Data function. It will not be picked up at any other time. Clearly this is a massive design flaw.

The psuedo-Parallel code...

Step 4: The Pre-emptive Code

The Arduino Uno has two interrupt pins. These pins are designed specifically for the purpose of monitoring changes on the pin and allowing us to be notified of the change immediately. Other devices have more or less interrupt pins and there are libraries available that will allow you to use any pin as an interrupt. However, we are just sticking with the default Uno pins (pins 2 and 3 which are assigned interrupt 0 and 1) for this Instructable.

attachInterrupt(0,GetSensor3Data,RISING);

The above command which we can place either before or in the setup() function tells the Arduino to call the GetSensor3Data() function when interrupt 0 (pin 2) moves from LOW to HIGH.

Now when we run the code no matter when we press the digital button it immediately gets processed. We can now cut the electricity immediately and not have to wait until the movie has finished, or evacuate the school and not wait until the lunch bell rings.

Because we are now attaching GetSensor3Data to an interrupt we no longer need to call it in the main loop - which in turn speeds up the operation of the main loop. Not by much but a little.

So you can see how important interrupts are and how they should be used.

Word of warning...

Step 5: In Conclusion

Please remember you should not be attaching everything to an interrupt and critical functions should be duplicated on a second backup board.

Interrupts are essential, we use them everyday in our life so start using them in your Arduino sketches. You won't regret it!

Thanks!

About This Instructable

706views

36favorites

License:

More by CommunityWorkshops:Arduino: MPU 6050 Getting It to Work! Why using Arduino Interrupts is vital Arduino: Stepper Motor Example Sketch Fixed 
Add instructable to: