0% found this document useful (0 votes)
7 views

Arduino 89

The document discusses the digitalWrite() function which writes a HIGH or LOW value to a digital pin, the pinMode() function which configures a pin as an input or output, and the if/else control structure which allows conditional execution of code blocks based on boolean expressions.

Uploaded by

Rod Supervlogs
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Arduino 89

The document discusses the digitalWrite() function which writes a HIGH or LOW value to a digital pin, the pinMode() function which configures a pin as an input or output, and the if/else control structure which allows conditional execution of code blocks based on boolean expressions.

Uploaded by

Rod Supervlogs
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

.

Reference > Language > Functions > Digital io > Digitalwrite


digitalWrite()
[Digital I/O]
Description
Write a HIGH or a LOW value to a digital pin.

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.

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 Digital Pins tutorial for more information.

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.

Syntax
digitalWrite(pin, value)

Parameters
pin: the Arduino pin number.
value: HIGH or LOW.

Returns
Nothing

Example Code
The code makes the digital pin 13 an OUTPUT and toggles it by alternating between HIGH and
LOW at one second pace.

void setup() {
pinMode(13, OUTPUT); // sets the digital pin 13 as output
}

void loop() {
digitalWrite(13, HIGH); // sets the digital pin 13 on
delay(1000); // waits for a second
digitalWrite(13, LOW); // sets the digital pin 13 off
delay(1000); // waits for a second
}
Notes and Warnings
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.

Reference > Language > Functions > Digital io > Pinmode


pinMode()
[Digital I/O]
Description
Configures the specified pin to behave either as an input or an output. See the Digital Pins page for
details on the functionality of the pins.

It is possible to enable the internal pullup resistors with the mode INPUT_PULLUP. Additionally,
the INPUT mode explicitly disables the internal pullups.

Syntax
pinMode(pin, mode)

Parameters
pin: the Arduino pin number to set the mode of.
mode: INPUT, OUTPUT, or INPUT_PULLUP. See the Digital Pins page for a more complete
description of the functionality.

Returns
Nothing

Example Code
The code makes the digital pin 13 OUTPUT and Toggles it HIGH and LOW

void setup() {
pinMode(13, OUTPUT); // sets the digital pin 13 as output
}

void loop() {
digitalWrite(13, HIGH); // sets the digital pin 13 on
delay(1000); // waits for a second
digitalWrite(13, LOW); // sets the digital pin 13 off
delay(1000); // waits for a second
}
Notes and Warnings
The analog input pins can be used as digital pins, referred to as A0, A1, etc.

Reference > Language > Functions > Digital io > Pinmode


pinMode()
[Digital I/O]
Description
Configures the specified pin to behave either as an input or an output. See the Digital Pins page for
details on the functionality of the pins.

It is possible to enable the internal pullup resistors with the mode INPUT_PULLUP. Additionally,
the INPUT mode explicitly disables the internal pullups.

Syntax
pinMode(pin, mode)

Parameters
pin: the Arduino pin number to set the mode of.
mode: INPUT, OUTPUT, or INPUT_PULLUP. See the Digital Pins page for a more complete
description of the functionality.

Returns
Nothing

Example Code
The code makes the digital pin 13 OUTPUT and Toggles it HIGH and LOW

void setup() {
pinMode(13, OUTPUT); // sets the digital pin 13 as output
}

void loop() {
digitalWrite(13, HIGH); // sets the digital pin 13 on
delay(1000); // waits for a second
digitalWrite(13, LOW); // sets the digital pin 13 off
delay(1000); // waits for a second
}
Notes and Warnings
The analog input pins can be used as digital pins, referred to as A0, A1, etc.

Reference > Language > Structure > Control structure > If


if
[Control Structure]
Description
The if statement checks for a condition and executes the following statement or set of statements if
the condition is 'true'.
Syntax
if (condition) {
//statement(s)
}
Parameters
condition: a boolean expression (i.e., can be true or false).

Example Code
The brackets may be omitted after an if statement. If this is done, the next line (defined by the
semicolon) becomes the only conditional statement.

if (x > 120) digitalWrite(LEDpin, HIGH);

if (x > 120)
digitalWrite(LEDpin, HIGH);

if (x > 120) {digitalWrite(LEDpin, HIGH);}

if (x > 120) {
digitalWrite(LEDpin1, HIGH);
digitalWrite(LEDpin2, HIGH);
}
// all are correct
Notes and Warnings
The statements being evaluated inside the parentheses require the use of one or more operators
shown below.

Comparison Operators:

x == y (x is equal to y)
x != y (x is not equal to y)
x < y (x is less than y)
x > y (x is greater than y)
x <= y (x is less than or equal to y)
x >= y (x is greater than or equal to y)
Beware of accidentally using the single equal sign (e.g. if (x = 10) ). The single equal sign is the
assignment operator, and sets x to 10 (puts the value 10 into the variable x). Instead use the double
equal sign (e.g. if (x == 10) ), which is the comparison operator, and tests whether x is equal to 10
or not. The latter statement is only true if x equals 10, but the former statement will always be true.
This is because C++ evaluates the statement if (x=10) as follows: 10 is assigned to x (remember that
the single equal sign is the (assignment operator)), so x now contains 10. Then the 'if' conditional
evaluates 10, which always evaluates to TRUE, since any non-zero number evaluates to TRUE.
Consequently, if (x = 10) will always evaluate to TRUE, which is not the desired result when using
an 'if' statement. Additionally, the variable x will be set to 10, which is also not a desired action.

Reference > Language > Structure > Control structure > Else
else
[Control Structure]
Description
The if…else allows greater control over the flow of code than the basic if statement, by allowing
multiple tests to be grouped. An else clause (if at all exists) will be executed if the condition in the if
statement results in false. The else can proceed another if test, so that multiple, mutually exclusive
tests can be run at the same time.

Each test will proceed to the next one until a true test is encountered. When a true test is found, its
associated block of code is run, and the program then skips to the line following the entire if/else
construction. If no test proves to be true, the default else block is executed, if one is present, and
sets the default behavior.

Note that an else if block may be used with or without a terminating else block and vice versa. An
unlimited number of such else if branches are allowed.

Syntax
if (condition1) {
// do Thing A
}
else if (condition2) {
// do Thing B
}
else {
// do Thing C
}
Example Code
Below is an extract from a code for temperature sensor system

if (temperature >= 70) {


// Danger! Shut down the system.
}
else if (temperature >= 60) { // 60 <= temperature < 70
// Warning! User attention required.
}
else { // temperature < 60
// Safe! Continue usual tasks.
}

You might also like