Skip to content

Commit f1dc35a

Browse files
committed
Language reference structure
1 parent 11398bd commit f1dc35a

File tree

11 files changed

+594
-2
lines changed

11 files changed

+594
-2
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
---
2+
title: digitalRead()
3+
categories: "Functions"
4+
subCategories: "Digital I/O"
5+
---
6+
7+
//
8+
:ext-relative: .html
9+
10+
= digitalRead()
11+
12+
// OVERVIEW SECTION STARTS
13+
[#overview]
14+
--
15+
16+
[float]
17+
=== Description
18+
Reads the value from a specified digital pin, either `HIGH` or `LOW`.
19+
[%hardbreaks]
20+
21+
[float]
22+
=== Syntax
23+
`digitalRead(pin)`
24+
25+
[float]
26+
=== Parameters
27+
`pin`: the Arduino pin number you want to read
28+
29+
[float]
30+
=== Returns
31+
`HIGH` or `LOW`
32+
33+
--
34+
// OVERVIEW SECTION ENDS
35+
36+
// HOW TO USE SECTION STARTS
37+
[#howtouse]
38+
--
39+
40+
[float]
41+
=== Example Code
42+
// Describe what the example code is all about and add relevant code ►►►►► THIS SECTION IS MANDATORY ◄◄◄◄◄
43+
Sets pin 13 to the same value as pin 7, declared as an input.
44+
45+
## [source,arduino]
46+
47+
int ledPin = 13; // LED connected to digital pin 13
48+
int inPin = 7; // pushbutton connected to digital pin 7
49+
int val = 0; // variable to store the read value
50+
51+
void setup() {
52+
pinMode(ledPin, OUTPUT); // sets the digital pin 13 as output
53+
pinMode(inPin, INPUT); // sets the digital pin 7 as input
54+
}
55+
56+
void loop() {
57+
val = digitalRead(inPin); // read the input pin
58+
digitalWrite(ledPin, val); // sets the LED to the button's value
59+
}
60+
61+
---
62+
63+
[%hardbreaks]
64+
65+
[float]
66+
=== Notes and Warnings
67+
If the pin isn't connected to anything, `digitalRead()` can return either `HIGH` or `LOW` (and this can change randomly).
68+
69+
The analog input pins can be used as digital pins, referred to as A0, A1, etc. The exception is the Arduino Nano, Pro Mini, and Mini's A6 and A7 pins, which can only be used as analog inputs.
70+
71+
--
72+
// HOW TO USE SECTION ENDS
73+
74+
// SEE ALSO SECTION
75+
[#see_also]
76+
--
77+
78+
[float]
79+
=== See also
80+
81+
[role="example"]
82+
83+
- #EXAMPLE# http://arduino.cc/en/Tutorial/DigitalPins[Description of the digital pins^]
84+
85+
--
86+
// SEE ALSO SECTION ENDS
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
---
2+
title: digitalWrite()
3+
categories: "Functions"
4+
subCategories: "Digital I/O"
5+
---
6+
7+
//
8+
:ext-relative: .html
9+
10+
= digitalWrite()
11+
12+
// OVERVIEW SECTION STARTS
13+
[#overview]
14+
--
15+
16+
[float]
17+
=== Description
18+
Write a `HIGH` or a `LOW` value to a digital pin.
19+
20+
If the pin has been configured as an `OUTPUT` with `pinMode()`, its voltage will be set to the corresponding value: 5V (or 3.3V on 3.3V boards) for `HIGH`, 0V (ground) for `LOW`.
21+
[%hardbreaks]
22+
23+
If the pin is configured as an `INPUT`, `digitalWrite()` will enable (`HIGH`) or disable (`LOW`) the internal pullup on the input pin. It is recommended to set the `pinMode()` to `INPUT_PULLUP` to enable the internal pull-up resistor. See the http://arduino.cc/en/Tutorial/DigitalPins[Digital Pins^] tutorial for more information.
24+
[%hardbreaks]
25+
26+
If you do not set the `pinMode()` to `OUTPUT`, and connect an LED to a pin, when calling `digitalWrite(HIGH)`, the LED may appear dim. Without explicitly setting `pinMode()`, `digitalWrite()` will have enabled the internal pull-up resistor, which acts like a large current-limiting resistor.
27+
[%hardbreaks]
28+
29+
[float]
30+
=== Syntax
31+
`digitalWrite(pin, value)`
32+
33+
[float]
34+
=== Parameters
35+
`pin`: the Arduino pin number. +
36+
`value`: `HIGH` or `LOW`.
37+
38+
[float]
39+
=== Returns
40+
Nothing
41+
42+
--
43+
// OVERVIEW SECTION ENDS
44+
45+
// HOW TO USE SECTION STARTS
46+
[#howtouse]
47+
--
48+
49+
[float]
50+
=== Example Code
51+
// Describe what the example code is all about and add relevant code ►►►►► THIS SECTION IS MANDATORY ◄◄◄◄◄
52+
The code makes the digital pin 13 an `OUTPUT` and toggles it by alternating between `HIGH` and `LOW` at one second pace.
53+
54+
## [source,arduino]
55+
56+
void setup() {
57+
pinMode(13, OUTPUT); // sets the digital pin 13 as output
58+
}
59+
60+
void loop() {
61+
digitalWrite(13, HIGH); // sets the digital pin 13 on
62+
delay(1000); // waits for a second
63+
digitalWrite(13, LOW); // sets the digital pin 13 off
64+
delay(1000); // waits for a second
65+
}
66+
67+
---
68+
69+
[%hardbreaks]
70+
71+
[float]
72+
=== Notes and Warnings
73+
The analog input pins can be used as digital pins, referred to as A0, A1, etc. The exception is the Arduino Nano, Pro Mini, and Mini's A6 and A7 pins, which can only be used as analog inputs.
74+
75+
--
76+
// HOW TO USE SECTION ENDS
77+
78+
// SEE ALSO SECTION
79+
[#see_also]
80+
--
81+
82+
[float]
83+
=== See also
84+
85+
[role="example"]
86+
87+
- #EXAMPLE# http://arduino.cc/en/Tutorial/DigitalPins[Description of the digital pins^]
88+
89+
--
90+
// SEE ALSO SECTION ENDS
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
---
2+
title: abs()
3+
categories: "Functions"
4+
subCategories: "Math"
5+
---
6+
7+
8+
9+
10+
11+
= abs(x)
12+
13+
14+
// OVERVIEW SECTION STARTS
15+
[#overview]
16+
--
17+
18+
[float]
19+
=== Description
20+
Calculates the absolute value of a number.
21+
[%hardbreaks]
22+
23+
24+
[float]
25+
=== Syntax
26+
`abs(x)`
27+
28+
29+
[float]
30+
=== Parameters
31+
`x`: the number
32+
33+
34+
[float]
35+
=== Returns
36+
`x`: if x is greater than or equal to 0. +
37+
`-x`: if x is less than 0.
38+
39+
--
40+
// OVERVIEW SECTION ENDS
41+
42+
43+
44+
45+
// HOW TO USE SECTION STARTS
46+
[#howtouse]
47+
--
48+
[float]
49+
=== Example Code
50+
// Describe what the example code is all about and add relevant code ►►►►► THIS SECTION IS MANDATORY ◄◄◄◄◄
51+
Prints the absolute value of variable `x` to the Serial Monitor.
52+
53+
[source,arduino]
54+
----
55+
void setup() {
56+
Serial.begin(9600);
57+
while (!Serial) {
58+
; // wait for serial port to connect. Needed for native USB port only
59+
}
60+
int x = 42;
61+
Serial.print("The absolute value of ");
62+
Serial.print(x);
63+
Serial.print(" is ");
64+
Serial.println(abs(x));
65+
x = -42;
66+
Serial.print("The absolute value of ");
67+
Serial.print(x);
68+
Serial.print(" is ");
69+
Serial.println(abs(x));
70+
}
71+
72+
void loop() {
73+
}
74+
----
75+
[%hardbreaks]
76+
77+
[float]
78+
=== Notes and Warnings
79+
Because of the way the abs() function is implemented, avoid using other functions inside the brackets, it may lead to incorrect results.
80+
[source,arduino]
81+
----
82+
abs(a++); // avoid this - yields incorrect results
83+
84+
// use this instead:
85+
abs(a);
86+
a++; // keep other math outside the function
87+
----
88+
[%hardbreaks]
89+
90+
91+
--
92+
// HOW TO USE SECTION ENDS
93+
94+
95+
// SEE ALSO SECTION
96+
[#see_also]
97+
--
98+
99+
[float]
100+
=== See also
101+
102+
[role="language"]
103+
* #LANGUAGE# link:../constrain[constrain()]
104+
* #LANGUAGE# link:../map[map()]
105+
* #LANGUAGE# link:../max[max()]
106+
* #LANGUAGE# link:../min[min()]
107+
* #LANGUAGE# link:../pow[pow()]
108+
* #LANGUAGE# link:../sq[sq()]
109+
* #LANGUAGE# link:../sqrt[sqrt()]
110+
111+
--
112+
// SEE ALSO SECTION ENDS
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
---
2+
title: constrain()
3+
categories: "Functions"
4+
subCategories: "Math"
5+
---
6+
7+
= constrain(x, a, b)
8+
9+
// OVERVIEW SECTION STARTS
10+
[#overview]
11+
--
12+
13+
[float]
14+
=== Description
15+
Constrains a number to be within a range.
16+
[%hardbreaks]
17+
18+
[float]
19+
=== Syntax
20+
`constrain(x, a, b)`
21+
22+
[float]
23+
=== Parameters
24+
`x`: the number to constrain Allowed data types: all data types. +
25+
`a`: the lower end of the range. Allowed data types: all data types. +
26+
`b`: the upper end of the range. Allowed data types: all data types.
27+
28+
[float]
29+
=== Returns
30+
x: if x is between a and b. +
31+
a: if x is less than a. +
32+
b: if x is greater than b.
33+
34+
--
35+
// OVERVIEW SECTION ENDS
36+
37+
// HOW TO USE SECTION STARTS
38+
[#howtouse]
39+
--
40+
41+
[float]
42+
=== Example Code
43+
// Describe what the example code is all about and add relevant code ►►►►► THIS SECTION IS MANDATORY ◄◄◄◄◄
44+
The code limits the sensor values to between 10 to 150.
45+
46+
## [source,arduino]
47+
48+
## sensVal = constrain(sensVal, 10, 150); // limits range of sensor values to between 10 and 150
49+
50+
[float]
51+
=== Notes and Warnings
52+
Because of the way the `constrain()` function is implemented, avoid using other functions inside the brackets, it may lead to incorrect results.
53+
54+
This code will yield incorrect results:
55+
[source,arduino]
56+
57+
---
58+
59+
## int constrainedInput = constrain(Serial.parseInt(), minimumValue, maximumValue); // avoid this
60+
61+
Use this instead:
62+
[source,arduino]
63+
64+
---
65+
66+
int input = Serial.parseInt(); // keep other operations outside the constrain function
67+
int constrainedInput = constrain(input, minimumValue, maximumValue);
68+
69+
---
70+
71+
[%hardbreaks]
72+
73+
--
74+
// HOW TO USE SECTION ENDS
75+
76+
// SEE ALSO SECTION
77+
[#see_also]
78+
--
79+
80+
[float]
81+
=== See also
82+
83+
[role="language"]
84+
85+
- #LANGUAGE# link:../abs[abs()]
86+
- #LANGUAGE# link:../map[map()]
87+
- #LANGUAGE# link:../max[max()]
88+
- #LANGUAGE# link:../min[min()]
89+
- #LANGUAGE# link:../pow[pow()]
90+
- #LANGUAGE# link:../sq[sq()]
91+
- #LANGUAGE# link:../sqrt[sqrt()]
92+
93+
--
94+
// SEE ALSO SECTION ENDS
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
title: Functions
3+
---

0 commit comments

Comments
 (0)