Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interrupts.

Similar presentations


Presentation on theme: "Interrupts."— Presentation transcript:

1 Interrupts

2 Overview Used to “interrupt” your loop to run a small piece of code
An external source triggers the interrupt After complete, the code returns to where it left off

3 Overview An external signal triggers the interrupt
There are 4 types of signals to watch for in a digital pin Rising – voltage goes from 0V  5V Falling – voltage goes from 5V  0V Changing – voltage goes either way LOW – as long as the pin is the low the interrupt will run

4 Buttons as Triggers D2 is normally connected to ground through the unpressed button D2 is LOW

5 Buttons as Triggers When the button is pressed, the 5V side of the button connects to D2 D2 is HIGH The resistor prevents a short circuit between 5V and ground when the button is pressed

6 Which pins? Depending on your microcontroller, each use specific pins to trigger the interrupt For example: on the Uno board, Interrupt 0 is actually triggered using D2

7 Interrupt Service Routine ISR
The piece of code you are running is called an ISR It is a separate void placed after the void loop() Code should be short There can be no inputs or outputs: digitalRead() and digitalWrite() No millis() or delay() No Serial.print();

8 Code attachInterrupt(interrupt number, ISR function, mode);
Setup the interrupt routine in the void setup() attachInterrupt(interrupt number, ISR function, mode); interrupt number – NOT PIN NUMBER. E.G. – on Arduino Uno, #0 is pin D2 ISR function – the name of the sub routine that should run mode – defines when the interrupt should be triggered

9 Code - Example

10 Button Bounce When a button is pressed and the two pieces of metal inside are “smashed” together, they actually bounce off of one another before settling into permanent contact This is called button bounce, and can cause a computer to interpret the one single press as multiple presses

11 Button Bounce

12 Button bounce as viewed on an oscilloscope

13 Software Solution Don’t update the variable in the ISR unless ¼ of a second has passed So… The ISR will still run on each bounce But the variable will not be updated on each run of the ISR

14 Software Solution


Download ppt "Interrupts."

Similar presentations


Ads by Google