Introduction
A programming language is used to convey the Progammer’s thoughts into instructions for the microprocessor to understand. To translate english-like instructions to machine code, a middile-ground is required, which is done so by computer programming language. They can be understood by humans, and can also easily and accurately convert the language into binary by compiling the code for the microprocessor to understand. Such programming languages include Python and C/C++. It is important that the instructions of the programmer be conveyed accurately into the programming language as the microprocessor ONLY follows the instructions-to-the-letter by the programming language. If your code is wrong/has errors, the microprocessor reproduces these errors. There are 3 main types of error:
- Syntax Error: This error occur when the programmer does not follow the rules of the programming language, such as missing a semi-colon. They are the most common errors and are easily caught by the compiler
- Logical Error: This error is made by the programmer in developing the logic of the program, such as using imperial units instead of metric.
- Semantic Error: This error occur during improper use of program statements, such as using the function 'Break' when there is no loop to break.
Variables and Data types in Arduino
Variables are storage that can be called out during a program. In order to use them, they must first be declared like so: [Data Type] [identifier] = value. The value can be fixed, calculated or from another variable. The value also doesn not have to be specified immediately, it can be assigned a value at a later part of the program. Variables only exist in the region that they are declared. Local variables are declared in a function or a block, and can only be used in there. Global variables are defined outside any function, usually at the top of the program and they exist throughout the entire program.
There are many types of data, and variables and functions have to have their data type specified. Different data types are implemented differently and they require different amount of memory to store data. Some common data types include:
- bool/boolean: Stores 'true' or 'false'. Uses 1 byte
- char: Stores a character. Uses 1 byte
- unsigned char/byte: Stores a number from 0 to 255. Uses 1 byte
- int: Stores a integer from -32766 to +32767. Uses 2 byte
- unsigned int/word/short:Stores a integer from 0 to +65535. Uses 2 byte
- long:Stores a integer from -2,147,483,648 to 2,147,483,647. Uses 4 byte
- float/double:Stores numbers with decimal point, ranging from -3.4028235E+38 to 3.4028235E+38. Uses 4 byte
When you require a value to stay constant through-out the program and you want to keep track of it using a identifier. Declare a constant by typing const before the data type.
Operators
Operators are symbols that are used to perform a specific mathematical or logical operation.
- Arithmetic: Execute a mathematical operation which gives a numerical result. '=' assign a value to an operand, '+' add, '-' subtract, '*' multiply, '/' divide.
- Comparison: Compares 2 operands to give a logical result(true of false). '==' equality, '!=' not equal to, '&rt;' greater than, '<' less than.
- Boolean operators: Compare 2 or more operations to give a logical result(true or false). && AND, '||' OR, '!' logical NOT.
- Bitwise operators: changes the bit pattern of the operator. '&' and. '|' or, '!' not/inverse, '«' shift left, '»' shift right
Control Statements
Control statements provide a means of decision making. A condition is first tested. If the result is true, then the code for Task A is executed. Else, the code for Task B is executed. Control statements are provided using the 'if' and 'else' statement. The condition statement must return a boolean result. 'if' statements can be nested using more 'if', whereas else statements can be nested using 'else if' statement
Loops
Loops provide a means of performing a task or executing a block of statements repeatedly. There are different types of loops that loops function in different ways.
- while(...): The loop starts if the condition in the bracket is true. When the block of code in the while statement is finished, the condition is checked again, and the block of code executes again if the condition is true.
- do ... while(...): The condition is only checked after the block of code is executed once, unlike while(...) loops, where the condition is checked before.
- for(...): Loops a certain number of times by using a initialised variable. In the bracket, a variable is specifed to be used or a new variable can be initialised, the condition for the loop to execute, and the increment of the variable after every cycle.
Functions
User-Defined functions are block of code outside of the default function "Setup" and "Loop". A user-defined function is created to do a series of task which the user can call out for the function to be executed at any point of the program. In other words, it's a smaller program where the user can run it at any part of the program. This creates a program that is modular where the main task is made out of smaller task linked together. It also allows a part of the program to be repeated or reuse without writing it all out again. Functions are created in a certain format, usually below the loop function.
Return data type functionname (argument1, argument2,...){
statement 1;
statement 2;
return variable;
}
- Return data type: The type of data to be returned by the function, such as int, void or double.
- argument1: A variable or parameter from the nested function that can be used by the user-defined function
- functionname: A unique name given to the function, to be used when calling out this function
- return: Return a variable of the data type specified. Used at the end of the function
Arduino Pins
The arduino uno comes with 14 digital pins that can be configured to either an Input or Output pin. To use this pins, there are a few functions required.
- pinMode(pinnumber, type): Used in setup(), specify the pin number as either Input or Output. The pin stays in this specified mode until another pinMode() is applied, reset occurs or powered off.
- digitalRead(pinnumber): Used on input ports, read the value from this pin.
- digitalWrite(pinnumber, state)Used on output ports, output the specified state the the pin number.
Serial Monitor
The Arduino comes with a Serial Communication module that is useful for debugging. It is a way of communicating from the board to you, where you can have a monitor show you the state of the Uno when it is executing a program. To use this, a few functions is required to be used.
- Serial.begin(bps): Used in setup(), setup the serial monitor to a certain bps (typically 9600) that has to be the same as the serial monitor used on the computer.
- Serial.println(variable): Print a line of the specified variable. The exact words can be outputted instead of a variable by adding "".
- Serial.print(variable):Print the variable without creating a new line.