Introduction: Interrupts Lab
To begin this lab you will need:
9 Leds
9 220 ohm resistors
1 10K ohm resistor
15 copper wires
Step 1: Add the Parts
In this step you will adding all of the needed components. The power cord should run from the 5V pin to the power rail. The ground wire will run from GND to the ground rail (-). The LEDS should be added next, followed by the button. Once these are added, add the copper wires and resistors to properly connect the components.
Step 2: Test the Source Code
With everything wired up, you can now test provided source code. Below I have attached source code that performs an interrupt function. The loop method runs through an array of pins and assigns a specific state. During this time, if a button is pressed an interrupt will occur. The interrupt calls for up to three random LEDs to have their state changed
Comments
6 years ago
I'm a newbie with respect to Arduino programming, but from my reading, there are couple of changes to your code you should consider:
1) I believe any variable that changes value inside an interrupt service routine should be volatile, so randNum, randNum2 and randNum3 should be defined as volatile.
2) There are issues with longer interrupt service routines including lost time on the millis() call. A better practice would be to just set a flag in the service routine and then call Blink() in the loop() function based on the flag. With this structure, only the flag would need to be volatile.