Skip to content

Commit e2018bd

Browse files
author
Akshay Sharma
committed
Add Function attachinterupt and map
1 parent 0945872 commit e2018bd

File tree

2 files changed

+150
-5
lines changed

2 files changed

+150
-5
lines changed
Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,127 @@
1-
// still working on this file
1+
:source-highlighter: pygments
2+
:pygments-style: arduino
3+
:ext-relative: adoc
4+
5+
6+
= attachInterrupt()
7+
8+
9+
// OVERVIEW SECTION STARTS
10+
[#overview]
11+
--
12+
13+
[float]
14+
=== Description
15+
Specifies a named Interrupt Service Routine (ISR) to call when an interrupt occurs. Replaces any previous function that was attached to the interrupt. Most Arduino boards have two external interrupts: numbers 0 (on digital pin 2) and 1 (on digital pin 3). The table below shows the available interrupt pins on various boards.
16+
17+
Board int.0 int.1 int.2 int.3 int.4 int.5
18+
19+
Uno, Ethernet 2 3
20+
21+
Mega2560 2 3 21 20 19 18
22+
23+
Leonardo 3 2 0 1 7
24+
25+
Due (see below)
26+
27+
The Arduino Due board has powerful interrupt capabilities that allows you to attach an interrupt function on all available pins. You can directly specify the pin number in `attachInterrupt()`.
28+
[%hardbreaks]
29+
30+
=== Notes and Warnings
31+
Inside the attached function, `delay()` won't work and the value returned by `millis()` will not increment. Serial data received while in the function may be lost. You should declare as volatile any variables that you modify within the attached function. See the section on ISRs below for more information.
32+
[%hardbreaks]
33+
34+
[float]
35+
== Using Interrupts
36+
37+
Interrupts are useful for making things happen automatically in microcontroller programs, and can help solve timing problems. Good tasks for using an interrupt may include reading a rotary encoder, or monitoring user input.
38+
39+
If you wanted to insure that a program always caught the pulses from a rotary encoder, so that it never misses a pulse, it would make it very tricky to write a program to do anything else, because the program would need to constantly poll the sensor lines for the encoder, in order to catch pulses when they occurred. Other sensors have a similar interface dynamic too, such as trying to read a sound sensor that is trying to catch a click, or an infrared slot sensor (photo-interrupter) trying to catch a coin drop. In all of these situations, using an interrupt can free the microcontroller to get some other work done while not missing the input.
40+
41+
[float]
42+
== About Interrupt Service Routines
43+
44+
ISRs are special kinds of functions that have some unique limitations most other functions do not have. An ISR cannot have any parameters, and they shouldn't return anything.
45+
46+
Generally, an ISR should be as short and fast as possible. If your sketch uses multiple ISRs, only one can run at a time, other interrupts will be ignored (turned off) until the current one is finished. as delay() and millis() both rely on interrupts, they will not work while an ISR is running. `delayMicroseconds()`, which does not rely on interrupts, will work as expected.
47+
48+
Typically global variables are used to pass data between an ISR and the main program. To make sure variables used in an ISR are updated correctly, declare them as volatile.
49+
50+
For more information on interrupts, see http://gammon.com.au/interrupts[Nick Gammon's notes].
51+
52+
[float]
53+
=== Syntax
54+
`attachInterrupt(interrupt, ISR, mode)` +
55+
`attachInterrupt(pin, ISR, mode)` _(Arduino Due only)_
56+
57+
58+
[float]
59+
=== Parameters
60+
`interrupt`: the number of the interrupt (`int`)
61+
`pin`: the pin number _(Arduino Due only)_
62+
`ISR`: the ISR to call when the interrupt occurs; this function must take no parameters and return nothing. This function is sometimes referred to as an interrupt service routine.
63+
`mode`: defines when the interrupt should be triggered. Four contstants are predefined as valid values:
64+
65+
* *LOW* to trigger the interrupt whenever the pin is low, +
66+
* *CHANGE* to trigger the interrupt whenever the pin changes value +
67+
* *RISING* to trigger when the pin goes from low to high, +
68+
* *FALLING* for when the pin goes from high to low. +
69+
The Due board allows also:
70+
* *HIGH* to trigger the interrupt whenever the pin is high. _(Arduino Due only)_
71+
72+
[float]
73+
=== Returns
74+
Nothing
75+
76+
--
77+
// OVERVIEW SECTION ENDS
78+
79+
80+
81+
82+
// HOW TO USE SECTION STARTS
83+
[#howtouse]
84+
--
85+
86+
[float]
87+
=== Example Code
88+
// Describe what the example code is all about and add relevant code ►►►►► THIS SECTION IS MANDATORY ◄◄◄◄◄
89+
90+
91+
[source,arduino]
92+
----
93+
int pin = 13;
94+
volatile int state = LOW;
95+
96+
void setup()
97+
{
98+
pinMode(pin, OUTPUT);
99+
attachInterrupt(0, blink, CHANGE);
100+
}
101+
102+
void loop()
103+
{
104+
digitalWrite(pin, state);
105+
}
106+
107+
void blink()
108+
{
109+
state = !state;
110+
}
111+
----
112+
[%hardbreaks]
113+
114+
[float]
115+
116+
117+
[float]
118+
=== See also
119+
// Link relevant content by category, such as other Reference terms (please add the tag #LANGUAGE#),
120+
// definitions (please add the tag #DEFINITION#), and examples of Projects and Tutorials
121+
// (please add the tag #EXAMPLE#) ►►►►► THIS SECTION IS MANDATORY ◄◄◄◄◄
122+
[role="language"]
123+
* #LANGUAGE# link:detachInterrupt{ext-relative}[detachInterrupt]
124+
125+
126+
--
127+
// HOW TO USE SECTION ENDS

Language/Functions/Math/map.adoc

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ Re-maps a number from one range to another. That is, a value of *fromLow* would
1616

1717
Does not constrain values to within the range, because out-of-range values are sometimes intended and useful. The `constrain()` function may be used either before or after this function, if limits to the ranges are desired.
1818

19+
Note that the "lower bounds" of either range may be larger or smaller than the "upper bounds" so the `map()` function may be used to reverse a range of numbers, for example
20+
21+
`y = map(x, 1, 50, 50, 1);`
22+
23+
The function also handles negative numbers well, so that this example
24+
25+
`y = map(x, 1, 50, 50, -100);`
26+
27+
is also valid and works well.
28+
29+
The `map()` function uses integer math so will not generate fractions, when the math might indicate that it should do so. Fractional remainders are truncated, and are not rounded or averaged.
1930
[%hardbreaks]
2031

2132

@@ -70,7 +81,17 @@ void loop()
7081
[%hardbreaks]
7182

7283
[float]
73-
=== Notes and Warnings
84+
=== Appendix
85+
86+
For the mathematically inclined, here's the whole function
87+
88+
[source,arduino]
89+
----
90+
long map(long x, long in_min, long in_max, long out_min, long out_max)
91+
{
92+
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
93+
}
94+
----
7495
[%hardbreaks]
7596

7697
[float]
@@ -79,9 +100,7 @@ void loop()
79100
// definitions (please add the tag #DEFINITION#), and examples of Projects and Tutorials
80101
// (please add the tag #EXAMPLE#) ►►►►► THIS SECTION IS MANDATORY ◄◄◄◄◄
81102
[role="language"]
82-
* #LANGUAGE#
83-
* #DEFINITION#
84-
* #EXAMPLE#
103+
* #LANGUAGE# link:constrain{ext-relative}[constrain()]
85104

86105
--
87106
// HOW TO USE SECTION ENDS

0 commit comments

Comments
 (0)