Professional Documents
Culture Documents
IOT Research Finals
IOT Research Finals
IDE
Arduino IDE
• The Arduino Integrated Development Environment - or Arduino Software (IDE) -
contains a text editor for writing code, a message area, a text console, a toolbar with
buttons for common functions and a series of menus. It connects to the Arduino
hardware to upload programs and communicate with them.
• Decision making structures require
that the programmer specify one or
more conditions to be evaluated or
tested by the program. It should be
along with a statement or
statements to be executed if the
condition is determined to be true,
and optionally, other statements
to be executed if the condition
is determined to be false.
Form 1
A. If Statement if (expression)
statement;
It takes an Form 2
expression in if (expression) {
statement or block of }
Example:
or block of
statements gets Void setup () {
executed otherwise
these statements are }
skipped.
Void loop () {
Block of statements;
Block of statements;
An if statement can }
be followed by an
optional else Example:
Void setup () {
Void loop () {
switch (variable) {
case label:
// statements
break;
C. Switch Statement }
statements, switch...case // statements
Example:
expression1 ? expression2 : expression3
D. Conditional
operator
max = ( a > b ) ? a : b;
var++;
}
Syntax
B. do...while do {
// statement block
The do… while loop works
in the same manner as } while (condition);
• GND (3): Short for ‘Ground’. There are several GND pins on the Arduino, any of
which can be used to ground your circuit.
• 5V (4) & 3.3V (5): As you might guess, the 5V pin supplies 5 volts of power, and
the 3.3V pin supplies 3.3 volts of power. Most of the simple components used with
the Arduino run happily off of 5 or 3.3 volts.
• Analog (6): The area of pins under the ‘Analog In’ label (A0 through A5 on the
UNO) are Analog In pins. These pins can read the signal from an analog sensor
(like a temperature sensor ) and convert it into a digital value that we can read.
• Digital (7): Across from the analog pins are the digital pins (0 through 13 on the
UNO). These pins can be used for both digital input (like telling if a button is
pushed) and digital output (like powering an LED).
• PWM (8): You may have noticed the tilde (~) next to some of the digital pins (3, 5,
6, 9, 10, and 11 on the UNO). These pins act as normal digital pins, but can also
be used for something called Pulse-Width Modulation (PWM). We have a tutorial
on PWM, but for now, think of these pins as being able to simulate analog output
(like fading an LED in and out).
• AREF (9): Stands for Analog Reference. Most of the time you can leave this pin
alone. It is sometimes used to set an external reference voltage (between 0 and 5
Volts) as the upper limit for the analog input pins.
(10) Reset Button
Just like the original Nintendo, the Arduino has a reset button (10). Pushing it will temporarily connect the reset
pin to ground and restart any code that is loaded on the Arduino. This can be very useful if your code doesn’t
repeat, but you want to test it multiple times. Unlike the original Nintendo however, blowing on the Arduino
doesn't usually fix any problems.
(12) TX RX LEDs
TX is short for transmit, RX is short for receive. These markings appear quite a bit in electronics to indicate the
pins responsible for serial communication. In our case, there are two places on the Arduino UNO where TX and RX
appear -- once by digital pins 0 and 1, and a second time next to the TX and RX indicator LEDs (12). These LEDs
will give us some nice visual indications whenever our Arduino is receiving or transmitting data (like when we’re
loading a new program onto the board).
(13) Main IC
The black thing with all the metal legs is an IC, or Integrated Circuit (13). Think of it as the brains of our Arduino.
The main IC on the Arduino is slightly different from board type to board type, but is usually from the ATmega line
of IC’s from the ATMEL company. This can be important, as you may need to know the IC type (along with your
board type) before loading up a new program from the Arduino software. This information can usually be found in
writing on the top side of the IC. If you want to know more about the difference between various IC's, reading the
datasheets is often a good idea.
• Declare an array of chars (with one extra char) and the compiler will add the required null character, as in Str2
• Initialize with a string constant in quotation marks; the compiler will size the array to fit the string constant and
a terminating null character, Str4
• Initialize the array with an explicit size and string constant, Str5
• Initialize the array, leaving extra space for a larger string, Str6
Null termination
Generally, strings are terminated with a null character (ASCII code 0). This allows functions (like Serial.print()) to tell where the end
of a string is. Otherwise, they would continue reading subsequent bytes of memory that aren’t actually part of the string.
This means that your string needs to have space for one more character than the text you want it to contain. That is why Str2 and Str5 need
to be eight characters, even though "arduino" is only seven - the last position is automatically filled with a null character. Str4 will be
automatically sized to eight characters, one for the extra null. In Str3, we’ve explicitly included the null character (written '\0') ourselves.
Note that it’s possible to have a string without a final null character (e.g. if you had specified the length of Str2 as seven instead of eight).
This will break most functions that use strings, so you shouldn’t do it intentionally. If you notice something behaving strangely
(operating on characters not in the string), however, this could be the problem.
Arrays of strings
It is often convenient, when working with large amounts of text, such as a project with an LCD display, to setup an array of strings.
Because strings themselves are arrays, this is actually an example of a two-dimensional array.
In the code below, the asterisk after the datatype char “char*” indicates that this is an array of “pointers”. All array names are actually
pointers, so this is required to make an array of arrays. Pointers are one of the more esoteric parts of C++ for beginners to understand, but
it isn’t necessary to understand pointers in detail to use them effectively here.
Strings are always defined inside double quotes ("Abc") and characters are always defined inside single quotes('A').
Strings
Syntax
char Str1[15];
char Str2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'};
char Str3[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o', '\0'};
char Str4[] = "arduino";
char Str5[8] = "arduino";
char Str6[15] = "arduino";
Example Code
char *myStrings[] = {"This is string 1", "This is string 2", "This is string 3",
"This is string 4", "This is string 5", "This is string 6"
};
void setup() {
Serial.begin(9600);
}
void loop() {
for (int i = 0; i < 6; i++) {
Serial.println(myStrings[i]);
delay(500);
}
}
Arrays
Creating (Declaring) an Array
All of the methods below are valid ways to create (declare) an array.
int myInts[6];
Accessing an Array
Arrays are zero indexed, that is, referring to the array initialization above, the first element of the array is at index 0, hence
It also means that in an array with ten elements, index nine is the last element. Hence:
// myArray[9] contains 11
For this reason you should be careful in accessing arrays. Accessing past the end of an array (using an index number greater than your declared array size - 1) is
reading from memory that is in use for other purposes. Reading from these locations is probably not going to do much except yield invalid data. Writing to
random memory locations is definitely a bad idea and can often lead to unhappy results such as crashes or program malfunction. This can also be a difficult
bug to track down.
Unlike BASIC or JAVA, the C++ compiler does no checking to see if array access is within legal bounds of the array size that you have declared.
Arrays
mySensVals[0] = 10;
x = mySensVals[4];
Arrays are often manipulated inside for loops, where the loop counter is used as the index for each array element. For
example, to print the elements of an array over the serial port, you could do something like this:
Serial.println(myPins[i]);