160x Filetype PDF File size 0.13 MB Source: mycourses.aalto.fi
Arduino Coding Basics For more information about structures and variables, visit the reference website. Structure of Arduino code At a basic level, Arduino code is made up of three parts: //1. Pre-setup //Include additional libraries here //Define global variables before setup() //2. Setup void setup() { //Statements here are run once when the Arduino is turned on //or reset. } //3. Loop void loop() { //After setup, statements inside the loop function will be run //over and over, until the Arduino is turned off. } Good to know: - It’s required to have both setup() and loop() functions in the code. - Defining variables inside a function limits their use to that function only, called the scope of the variable. - On the other hand, defining variables outside of functions lets the code access them anywhere. This is called a global variable. - It’s also possible to include additional features and functions into your program with the use of external libraries. Libraries are included with #include. Variables A variable is a place to store a piece of data. It has a data type, name and a value. For example, these statements (called declarations): int pin1 = 13; int pin2; //Variables don’t need to be assigned a value create variables whose types are int and names are pin1 and pin2. Pin1 is assigned the value 13, but pin2 isn’t assigned any value, and should not be used for anything before it is given one. The variables can be used later in the program, at which point its value will be looked up and used. For example this statement: pinMode(pin1, OUTPUT); //Equals: pinMode(13, OUTPUT); You can easily change the value of a variable using an assignment, for example: pin1 = 12; //Pin1’s value is changed from 13 to 12. pin2 = 11; //Pin2’s value is set to 11. Why use variables? - Only need to specify a value once, but can be used multiple times. - In case one wants to change the value, only one spot in the code needs to be changed. - Descriptive names for variables makes the code more readable. Data types The data type of a variable determines what kind of data it can store. Using the wrong type when declaring a variable will have unexpected consequences and may crash the program. The most common types in Arduino programming are: char: Stores a character, e.g ‘e’ or ‘9’. Characters are stored as integers, and their integer values are listed in ASCII tables. int: Stores an integer between -32768 and 32767. Going past these bounds will overflow the value to the other bound and keep counting from there. e.g 32767+1 == -32768 38 38 float: Stores a decimal value between -3.4*10 to 3.4*10 . Should only be used if decimal values are required, because calculation with floats is slower and not as precise. bool: Stores a truth value (true or false). If used in math operations, false = 0 and true = 1 Good to know: - Dividing a value assigned as int will not round up. E.g 5/3 = 1, not 2 - Assigning a decimal number to an int variable will truncate, not round. E.g 2.7 = 2 - For numbers larger than 16-bit, use long instead of int. - The unsigned keyword lets a variable only store positive integers up to double the normal maximum. E.g unsigned int goes from 0 to 65535. - When doing math with floats, all numbers need to have decimals for the calculations to be correct. - floats will not store large decimal differences correctly, e.g 12345.6789 might be stored as 12345.679 Arrays Arrays allow for storage of multiple values of the same data type, easily accessible through the index number of the value. Indexing starts from 0. There are multiple ways to create an array, presented below: //Initialize empty array with space for 6 ints. int myInts[6]; //Initialize array with elements {2, 4, 8, 3, 6}. Size is //automatically set accordingly. int myPins[] = {2, 4, 8, 3, 6}; //Initialize array with elements and sets size. In this case, //there is room for one more int. int mySensVals[6] = {2, 4, -8, 3, 2}; //Works the same for char, except size must be 1 more than number //of characters, to make room for the required null character at //the end. char message[6] = "hello"; Values from an array are accessed via their index number. E.g mySensVals[0] == 2 and mySensVals[2] == -8. Assigning values to an index is done with array[index] = value; Good to know: - Trying to access indexes outside an array will lead to crashes or other unwanted behaviour. - Parsing through an array is easily done with a for-loop (explained below). - The size of an array can be gotten with sizeof(array). Keep in mind size and length are not the same. - The length of an array can be gotten by dividing its size with the size of its contained data type. E.g sizeof(array)/sizeof(int).
no reviews yet
Please Login to review.