Skip to content

Commit c2cf592

Browse files
committed
Merge pull request #6 from arduino/Language_content
Language content. Ok but PROGMEM needs to be modified according to arduino/Arduino#1536
2 parents a52359c + 97dd879 commit c2cf592

File tree

8 files changed

+730
-8
lines changed

8 files changed

+730
-8
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
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
:source-highlighter: pygments
2+
:pygments-style: arduino
3+
:ext-relative: adoc
4+
5+
6+
= Constants
7+
8+
9+
// OVERVIEW SECTION STARTS
10+
[#overview]
11+
--
12+
13+
[float]
14+
=== Description
15+
Constants are predefined expressions in the Arduino language. They are used to make the programs easier to read. We classify constants in groups:
16+
17+
[float]
18+
== Defining Logical Levels: true and false (Boolean Constants)
19+
There are two constants used to represent truth and falsity in the Arduino language: `true`, and `false`.
20+
21+
[float]
22+
=== false
23+
`false` is the easier of the two to define. false is defined as 0 (zero).
24+
[%hardbreaks]
25+
26+
[float]
27+
=== true
28+
`true` is often said to be defined as 1, which is correct, but true has a wider definition. Any integer which is non-zero is true, in a Boolean sense. So -1, 2 and -200 are all defined as true, too, in a Boolean sense.
29+
[%hardbreaks]
30+
31+
Note that the `true` and `false` constants are typed in lowercase unlike `HIGH`, `LOW`, `INPUT`, and `OUTPUT`.
32+
[%hardbreaks]
33+
34+
[float]
35+
== Defining Pin Levels: HIGH and LOW
36+
When reading or writing to a digital pin there are only two possible values a pin can take/be-set-to: `HIGH` and `LOW`.
37+
38+
[float]
39+
=== HIGH
40+
The meaning of `HIGH` (in reference to a pin) is somewhat different depending on whether a pin is set to an `INPUT` or `OUTPUT`. When a pin is configured as an `INPUT` with link:../../Functions/Digital%20IO/pinMode{ext-relative}[pinMode()], and read with link:../../Functions/Digital%20IO/digitalRead{ext-relative}[digitalRead()], the Arduino (ATmega) will report `HIGH` if:
41+
42+
- a voltage greater than 3 volts is present at the pin (5V boards)
43+
- a voltage greater than 2 volts is present at the pin (3.3V boards)
44+
[%hardbreaks]
45+
46+
A pin may also be configured as an INPUT with `pinMode()`, and subsequently made HIGH with link:../../Functions/Digital%20IO/digitalWrite{ext-relative}[digitalWrite()]. This will enable the internal 20K pullup resistors, which will _pull up_ the input pin to a `HIGH` reading unless it is pulled `LOW` by external circuitry. This is how `INPUT_PULLUP` works and is described below in more detail.
47+
[%hardbreaks]
48+
49+
When a pin is configured to OUTPUT with `pinMode()`, and set to `HIGH` with `digitalWrite()`, the pin is at:
50+
51+
- 5 volts (5V boards)
52+
- 3.3 volts (3.3V boards)
53+
54+
In this state it can source current, e.g. light an LED that is connected through a series resistor to ground.
55+
[%hardbreaks]
56+
57+
[float]
58+
=== LOW
59+
The meaning of `LOW` also has a different meaning depending on whether a pin is set to `INPUT` or `OUTPUT`. When a pin is configured as an `INPUT` with `pinMode()`, and read with `digitalRead()`, the Arduino (ATmega) will report LOW if:
60+
61+
- a voltage less than 3 volts is present at the pin (5V boards)
62+
- a voltage less than 2 volts is present at the pin (3.3V boards)
63+
64+
When a pin is configured to `OUTPUT` with `pinMode()`, and set to `LOW` with `digitalWrite()`, the pin is at 0 volts (both 5V and 3.3V boards). In this state it can sink current, e.g. light an LED that is connected through a series resistor to +5 volts (or +3.3 volts).
65+
[%hardbreaks]
66+
67+
[float]
68+
== Defining Digital Pins modes: INPUT, INPUT_PULLUP, and OUTPUT
69+
Digital pins can be used as `INPUT`, `INPUT_PULLUP`, or `OUTPUT`. Changing a pin with `pinMode()` changes the electrical behavior of the pin.
70+
71+
[float]
72+
=== Pins Configured as INPUT
73+
Arduino (ATmega) pins configured as `INPUT` with `pinMode()` are said to be in a _high-impedance_ state. Pins configured as `INPUT` make extremely small demands on the circuit that they are sampling, equivalent to a series resistor of 100 Megohms in front of the pin. This makes them useful for reading a sensor.
74+
[%hardbreaks]
75+
76+
If you have your pin configured as an `INPUT`, and are reading a switch, when the switch is in the open state the input pin will be "floating", resulting in unpredictable results. In order to assure a proper reading when the switch is open, a pull-up or pull-down resistor must be used. The purpose of this resistor is to pull the pin to a known state when the switch is open. A 10 K ohm resistor is usually chosen, as it is a low enough value to reliably prevent a floating input, and at the same time a high enough value to not not draw too much current when the switch is closed. See the http://arduino.cc/en/Tutorial/DigitalReadSerial[Digital Read Serial^] tutorial for more information.
77+
[%hardbreaks]
78+
79+
If a pull-down resistor is used, the input pin will be `LOW` when the switch is open and `HIGH` when the switch is closed.
80+
[%hardbreaks]
81+
82+
If a pull-up resistor is used, the input pin will be `HIGH` when the switch is open and `LOW` when the switch is closed.
83+
[%hardbreaks]
84+
85+
[float]
86+
=== Pins Configured as INPUT_PULLUP
87+
The ATmega microcontroller on the Arduino has internal pull-up resistors (resistors that connect to power internally) that you can access. If you prefer to use these instead of external pull-up resistors, you can use the `INPUT_PULLUP` argument in `pinMode()`.
88+
[%hardbreaks]
89+
90+
See the http://arduino.cc/en/Tutorial/InputPullupSerial[Input Pullup Serial^] tutorial for an example of this in use.
91+
[%hardbreaks]
92+
93+
Pins configured as inputs with either `INPUT` or `INPUT_PULLUP` can be damaged or destroyed if they are connected to voltages below ground (negative voltages) or above the positive power rail (5V or 3V).
94+
[%hardbreaks]
95+
96+
[float]
97+
=== Pins Configured as OUTPUT
98+
Pins configured as `OUTPUT` with `pinMode()` are said to be in a _low-impedance_ state. This means that they can provide a substantial amount of current to other circuits. ATmega pins can source (provide current) or sink (absorb current) up to 40 mA (milliamps) of current to other devices/circuits. This makes them useful for powering LEDs because LEDs typically use less than 40 mA. Loads greater than 40 mA (e.g. motors) will require a transistor or other interface circuitry.
99+
[%hardbreaks]
100+
101+
Pins configured as outputs can be damaged or destroyed if they are connected to either the ground or positive power rails.
102+
[%hardbreaks]
103+
104+
[float]
105+
== Defining built-ins: LED_BUILTIN
106+
Most Arduino boards have a pin connected to an on-board LED in series with a resistor. The constant `LED_BUILTIN` is the number of the pin to which the on-board LED is connected. Most boards have this LED connected to digital pin 13.
107+
108+
--
109+
// OVERVIEW SECTION ENDS
110+
111+
112+
113+
// HOW TO USE SECTION STARTS
114+
[#howtouse]
115+
--
116+
117+
118+
[float]
119+
=== See also
120+
121+
[role="language"]
122+
* #LANGUAGE# link:integerConstants{ext-relative}[Integer Constants]
123+
* #LANGUAGE# link:floatingPointConstants{ext-relative}[Floating Point Constants]
124+
125+
--
126+
// HOW TO USE SECTION ENDS
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
:source-highlighter: pygments
2+
:pygments-style: arduino
3+
:ext-relative: adoc
4+
5+
6+
= Floating Point Constants
7+
8+
9+
// OVERVIEW SECTION STARTS
10+
[#overview]
11+
--
12+
13+
[float]
14+
=== Description
15+
Similar to integer constants, floating point constants are used to make code more readable. Floating point constants are swapped at compile time for the value to which the expression evaluates.
16+
[%hardbreaks]
17+
18+
--
19+
// OVERVIEW SECTION ENDS
20+
21+
22+
23+
// HOW TO USE SECTION STARTS
24+
[#howtouse]
25+
--
26+
27+
[float]
28+
=== Example Code
29+
30+
[source,arduino]
31+
----
32+
n = 0.005; // 0.005 is a floating point constant
33+
----
34+
[%hardbreaks]
35+
36+
[float]
37+
=== Notes and Warnings
38+
Floating point constants can also be expressed in a variety of scientific notation. 'E' and 'e' are both accepted as valid exponent indicators.
39+
[%hardbreaks]
40+
41+
|===
42+
|floating-point constant |evaluates to: |also evaluates to:
43+
44+
|10.0
45+
|10
46+
|
47+
48+
|2.34E5
49+
|2.34 * 10^5
50+
|234000
51+
52+
|67e-12
53+
|67.0 * 10^-12
54+
|0.000000000067
55+
56+
|===
57+
[%hardbreaks]
58+
59+
60+
61+
[float]
62+
=== See also
63+
64+
[role="language"]
65+
* #LANGUAGE# link:constants{ext-relative}[Constants]
66+
* #LANGUAGE# link:integerConstants{ext-relative}[Integer Constants]
67+
68+
--
69+
// HOW TO USE SECTION ENDS

0 commit comments

Comments
 (0)