![]() Thus these two lines have the same effect: attachInterrupt (0, switchPressed, CHANGE) // that is, for pin D2ĪttachInterrupt (digitalPinToInterrupt (2), switchPressed, CHANGE) It returns the appropriate interrupt number, or NOT_AN_INTERRUPT (-1).įor example, on the Uno, pin D2 on the board is interrupt 0 (INT0_vect from the table below). To simplify converting interrupt vector numbers to pin numbers you can call the function digitalPinToInterrupt(), passing a pin number. In a more complicated example, the main loop might be doing something useful, like taking temperature readings, and allow the interrupt handler to detect a button being pushed.Ĭonverting pin numbers to interrupt numbers ![]() The change in the pin is detected by a CHANGE interrupt, which causes the Interrupt Service Routine (ISR) to be called. The internal pullup (enabled in setup) forces the pin HIGH normally. To test this, just connect a wire (or switch) between D2 and Ground. This example shows how, even though the main loop is doing nothing, you can turn the LED on pin 13 on or off, if the switch on pin D2 is pressed. PinMode (LED, OUTPUT) // so we can update the LEDĪttachInterrupt (digitalPinToInterrupt (SWITCH), switchPressed, CHANGE) // attach interrupt handler When the timer rings you "interrupt" your TV viewing to do something with the potatoes.Įxample of interrupts const byte LED = 13 ![]() Rather than staring at the clock for 20 minutes you might set a timer, and then go watch TV. For example, if you are cooking dinner you may put the potatoes on to cook for 20 minutes. ![]() Interrupts let you respond to "external" events while doing something else. Variables shared with main code may need to be protected by "critical sections" (see below).Make variables shared with the main code volatile.When writing an Interrupt Service Routine (ISR): ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |