Arduino uses the C/C++ programming language, but remove all the complexity in C. We will discuss some of the basic foundation of C.
A variable is a named location in a memory where a program can manipulate data. Here is the declaration syntax in C.
int i = 5;
where i is the variable, int is the type of the variable, "=" is the equal operator and 5 is the value of the variable.
The value of the variable may get change in the program. A variable in C should have a data type like int or boolean.
See the table below for different C data types.
C Data Types
Strings in C is created with the following syntax:
char* text = "Sample Text";
The char* is an equivalent to String type in other language (like Java)
There are times that it needs to perform calculation in the code and assign to a variable. The following code contains the example of arithmetic operation.
Sample Arithmetic Operation
When a conditional statement is use, it means that it needs to decide what the next instruction to be executed based on the logical expression. See below for an example:
Sample if-else statement
In a given sample, if the logical expression is true, that is, if value is greater than 2, the digitalWrite(13, HIGH) will be executed. But if the condition returns false, the statement in the "else" block will be executed, which is digitalWrite(13, LOW)
We will discuss more and other coding in C as we explore doing more projects in Arduino. See you on the next article.