Arduino Flashcards
void setup()
This is where you place the initialization code—the instructions that set up the board before the main loop of the sketch starts.
void loop()
This contains the main code of your sketch. It contains a set of instructions that get repeated over and over until the board is switched off.
; (semicolon)
Every instruction (line of code) is terminated by a semicolon. This syntax lets you format the code freely. You could even put two instructions on the same line, as long as you separate them with a semicolon. (However, this would make the code harder to read.) Example: delay(100);
{} (curly braces)
This is used to mark blocks of code. For example, when you write code for the loop() function, you have to use curly braces before and after the code. Example: void loop() { Serial.println("ciao"); }
comments
These are portions of text ignored by the Arduino processor, but are extremely useful to remind yourself (or others) of what a piece of code does.
There are two styles of comments in Arduino:
// single-line: this text is ignored until the end of the line
/* multiple-line:
you can write
a whole poem in here
*/
constants
Arduino includes a set of predefined keywords with special values.
HIGH and LOW are used, for example, when you want to turn on or off an Arduino pin. INPUT and OUTPUT are used to set a specific pin to be either and input or an output
true and false indicate exactly what their names suggest: the truth or falsehood of a condition or expression.
variables
Variables are named areas of the Arduino’s memory where you can store data that you can use and manipulate in your sketch. As the name suggests, they can be changed as many times as you like.
Because Arduino is a very simple processor, when you declare a variable you have to specify its type. This means telling the processor the size of
the value you want to store.
boolean
Can have one of two values: true or false.
char
Holds a single character, such as A. Like any computer, Arduino stores it as a number, even though you see text. When chars are used to store numbers, they can hold values from –128 to 127.
ASCII
ASCII is a set of 127 characters that was used for, among other things, transmitting text between serial terminals and time-shared computer systems such as mainframes and minicomputers.
UNICODE
UNICODE is a much larger set of values used by modern computer operating systems to represent characters in a wide range of languages.
byte
Holds a number between 0 and 255. As with chars, bytes use only one byte of memory
int
Uses 2 bytes of memory to represent a number between –32,768 and 32,767; it’s the most common data type used in Arduino.
unsigned int
Like int, uses 2 bytes but the unsigned prefix means that it can’t store negative numbers, so its range goes from 0 to 65,535.
long
This is twice the size of an int and holds numbers from –2,147,483,648 to 2,147,483,647.
unsigned long
Unsigned version of long; it goes from 0 to 4,294,967,295.
float
This quite big and can hold floating-point values, a fancy way of saying that you can use it to store numbers with a decimal point in it. It will eat up 4 bytes of your precious RAM and the functions that can handle them use up a lot of code memory as well. So use floats sparingly.
double
Double-precision floating-point number, with a maximum value of 1.7976931348623157 x 10308. Wow, that’s huge!
string
A set of ASCII characters that are used to store textual information (you might use a string to send a message via a serial port, or to display on an LCD display). For storage, they use one byte for each character in the string, plus a null character to tell Arduino that it’s the end of the string.
The following are equivalent:
char string1[] = “Arduino”; // 7 chars + 1 null char
char string2[8] = “Arduino”; // Same as above
array
A list of variables that can be accessed via an index. They are used to build tables of values that can easily be accessed. For example, if you want to store different levels of brightness to be used when fading an LED,
you could create six variables called light01, light02, and so on. Better yet, you could use a simple array like:
int light[6] = {0, 20, 50, 75, 100};
The word “array” is not actually used in the variable declaration: the symbols [] and {} do the job.
if . . . else
This structure makes decisions in your program. if must be followed by a question specified as an expression contained in parentheses. If the expression is true, whatever follows will be executed. If it’s false, the block of code following else will be executed. It’s possible to use just if without providing an else clause. Example: if (val == 1) { digitalWrite(LED,HIGH); }
for
Lets you repeat a block of code a specified number of times. Example: for (int i = 0; i < 10; i++) { Serial.print("ciao"); }
switch case
The if statement is like a fork in the road for your program. switch case is like a massive roundabout. It lets your program take a variety of directions depending on the value of a variable. It’s quite useful to keep your code tidy as it replaces long lists of if statements. Example: switch (sensorValue) { case 23: digitalWrite(13,HIGH); break; case 46: digitalWrite(12,HIGH); break; default: // if nothing matches this is executed digitalWrite(12,LOW); digitalWrite(13,LOW); }
while
Similar to if, this executes a block of code while a certain condition is true. Example: // blink LED while sensor is below 512 sensorValue = analogRead(1); while (sensorValue < 512) { digitalWrite(13,HIGH); delay(100); digitalWrite(13,HIGH); delay(100); sensorValue = analogRead(1); }
do . . . while
Just like while, except that the code is run just before the the condition
is evaluated. This structure is used when you want the code inside your
block to run at least once before you check the condition.
Example:
do {
digitalWrite(13,HIGH);
delay(100);
digitalWrite(13,HIGH);
delay(100);
sensorValue = analogRead(1);
} while (sensorValue < 512);
break
This term lets you leave a loop and continue the execution of the code that appears after the loop. It’s also used to separate the different sections of a switch case statement. Example: // blink LED while sensor is below 512 do { // Leaves the loop if a button is pressed if (digitalRead(7) == HIGH) break; digitalWrite(13,HIGH); delay(100); digitalWrite(13,LOW); delay(100); sensorValue = analogRead(1); } while (sensorValue < 512);
continue
When used inside a loop, continue lets you skip the rest of the code inside it and force the condition to be tested again. Example: for (light = 0; light < 255; light++) { // skip intensities between 140 and 200 if ((x > 140) && (x < 200)) continue; analogWrite(PWMpin, light); delay(10); }