AMC Reference Sheet
AMC Reference Sheet
KEY TERMINOLOGY
Listed below are the key terms covered in this mini-course with their respective
definitions.
● Duty Cycle: The fraction of one period in which a signal is active. Measured as
a percentage of the period.
● Frequency: How often an event repeats per unit time (e.g. the number of times
a signal’s waveform repeats itself per second).
● LED: Light Emitting Diode. A semiconductor that emits light when current flows
through it. The on-board LED of the Arduino is connected to Pin13 and is used
in the “Blinky” example.
● Pin: A type of electrical contact used to connect circuit components. The digital
pins on the Arduino can be configured to either read or write signals by setting
them to be input pins or output pins. The Arduino Uno has 14 digital pins as well
as 6 analog input pins.
● Period: The amount of time for one cycle of something to complete (e.g. time
in seconds it takes for a signal’s waveform to repeat itself). The reciprocal of
the frequency.
● Power: The rate per unit time that electrical energy is transferred in a circuit.
Measured in watts.
● PWM: Pulse Width Modulation. A method used to simulate analog signals with
digital signal outputs. The width of the signal's pulse is changed, or modulated,
such that the average output voltage is the same as the desired analog
© Copyright 2021 Gentiam Consulting LLC - All Rights Reserved
voltage. PWM has two key characteristics that determine its behaviour: duty
cycle and frequency.
● Serial Communication: The process of sending data via digital signals one bit
at a time.
o Analog Signal: A continuous signal that can hold any value between
ground and its supply voltage. In this way an electrical signal represents
data/information analogously to another time-varying signal.
Variables can have different types depending on their intended purpose. There are
three key data types you will need to know before starting the course.
Integer
Integers store only discrete integer values ranging from -32,768 to 32,767 (e.g. -1, 8,
378, -47, etc.).
int myNumber = 7;
The above code declares an integer (int) named myNumber and sets its stored value
to 7.
https://www.arduino.cc/reference/en/language/variables/data-types/int/
https://www.arduino.cc/reference/en/language/variables/data-types/float/
https://www.arduino.cc/reference/en/language/variables/data-types/bool/
void loop() {
// This is a local variable and can only be accessed within thisloop()
// function
int myLocalVariable = 6;
}
Boolean and comparison operators are used to create the Boolean conditions for a
conditional statement. Logical operations follow order of operations. Parentheses can
be used to specify that one logical operation should be performed before another.
Boolean operators use Boolean operands (i.e. ‘true’ or ‘false’) and they are listed in
the following table:
Comparison operators use numerical operands and are listed in the following table:
“if” Statements
The “if” statement checks if a certain Boolean condition is “true”. If the statement is
“true”, then the block of code following the “if” statement will be executed. If it is false
instead, the code will not be executed. The Boolean condition is contained with
parentheses and the code to be executed is contained by curly braces. An example
“if” statement is shown below:
if (x > 72) {
x = 7;
y = 3;
}
The above code will assign x the value of 7 and y the value of 3 if x was greater than
72. Otherwise, the code within the curly braces will not execute.
https://www.arduino.cc/reference/en/language/structure/control-structure/if/
“else” Statements
The else statement cannot exist on its own and can only be used after an if
statement. Else statements do not contain a Boolean condition and will always
execute their code block if the previous if condition did not evaluate to true. For
example, see the following code segment.
int x = 3;
if (x >= 72) {
x = 7;
}
else {
x = 42;
}
The above code will assign x the value of 42. Since the initial value of x is 3, which is
not greater than 72, the code following the if statement will not execute, but the code
following the else statement will.
For more on else statements, please see the official Arduino documentation:
https://www.arduino.cc/reference/en/language/structure/control-structure/else/
int x = 20;
if(x >= 72) {
x = 7;
}
else if (x == 20){
x = 24;
}
else if(x >=20){
x = 0;
}
The above code will assign x the value of 24. Since the initial value of x is 20, which is
not greater than 72, the code following the “if” statement will not execute, and the next
“else if” condition will be checked. Since x is equal to 20, the code following the “else if”
block will be executed and x will take the value of 24. The last “else if” statement is
skipped, since one of the conditions in the chain proved to be true.
For more on “else if” statements, please see the official Arduino documentation:
https://www.arduino.cc/reference/en/language/structure/control-structure/else/
“for” Loops
A “for” loop repeats a block of code a specific number of times and will typically use
an increment counter to determine when it stops looping. There are three elements of
a for loop statement: the initialization, the condition, and the increment.
int x = 0;
for (int i = 0; i< 100; i++) {
x = x + 2;
}
The initialization is where i is declared and initialized to 1. This is executed only at the
beginning. The condition is the ‘i < 100’. This means that the “for” loop will continue
© Copyright 2021 Gentiam Consulting LLC - All Rights Reserved
executing its code block as long as i is less than 100. The i++ is the increment, resulting
in i incrementing by 1 every time the code block is executed. The increment is
executed at the end of every iteration of the loop. That means that this “for” loop will
execute its code exactly 100 times before continuing on to the rest of the program.
The final value of x will be 200.
For more on for loops in Arduino, please see the official Arduino documentation:
https://www.arduino.cc/reference/en/language/structure/control-structure/for/
The functions used in this course are outlined below for your own reference.
pinMode()
Description: Configures the specified pin to behave either as an input or an output.
Parameters:
Return: None.
Example:
void setup() {
// sets the digital pin 13 as output
pinMode(13, OUTPUT);
}
digitalWrite()
Description: Writes a HIGH or a LOW value to a digital pin.
Parameters:
Return: None.
Example:
void loop() {
// sets digital pin 13 to logic high
digitalWrite(13, HIGH);
}
delay()
© Copyright 2021 Gentiam Consulting LLC - All Rights Reserved
Description: Stalls the program for the amount of time as specified in the input
parameter (milliseconds).
Syntax: delay(ms)
Parameters:
Return: None.
Example: The following example code alternates the output on pin 13 from high to low
every 1 second.
void loop() {
digitalWrite(13, HIGH);
delay(1000); // wait for 1 second
digitalWrite(13, LOW);
delay(1000); // wait for 1 second
}
digitalRead()
Description: Reads a HIGH or LOW value from a digital pin.
Syntax: digitalRead(pin)
Parameters:
Example:
void loop() {
// Reads pin 13 and stores result into val
int val = digitalWrite(13);
}
analogRead()
Description: Reads the integer value from an analog pin.
Parameters:
Example:
void loop() {
// Reads pin A0 and stores result into val
int val = analogRead(A0);
}
analogWrite()
Description: Writes an analog value (PWM wave) to a digital pin.
Parameters:
● pin: the Arduino pin to write, must be a PWM pin market with a ‘~’.
● value: the duty cycle (value between 0-255)
Return: None.
Example:
void loop() {
// Writes a 50% duty cycle to pin 11
analogWrite(11, 255*0.5);
}