Don't be intimidated by code. They are mere sets of instructions you tell your computer what to do. I'll go through the basics with you and it should be pretty easy. It's written with letters, number, and mathematical symbols but in actuality when the code is executed, it is compiled meaning it is translated to a bunch of bits (ones and zeros), the only language the computer understands and something us humans find really hard to understand. Although this is a general programming tutorial, I'll use Arduino code as examples. A side note, some languages don't compile but are interpreted like javascript and some aren't really programming languages like HTML which is a makeup language.
The BasicsVariablesVariables are containers that hold data like a number or a string of text. It has a physical address in memory and is persistent so if you need to call it later in the code, it would contain the same value. To define a variable, it's as easy as:
Code:
int a = 1;
Some languages like the Arduino language requires you to give the variable a datatype or tell the computer what type of data this variable will store. 'int' means that it is an integer with some range like -32,768 to 32,767. There are other datatypes like 'string', 'boolean', 'float' etc..
A variable can contain compound, dynamic values as well like:
Code:
int x = 5;
float myVal = 32/x;
ArraysAn array is a list of data unlike a variable where it can only store one data. Like variables, it can be any type of data like numbers or strings etc.., although it's rarely needed where you need to mix data types. Most language won't allow this anyway. Each piece of data in the array is defined by an index number to represent its position in the list. The first element is
[0], the second is
[1], and so on. The methods to create arrays vary with the different languages. The Arduino looks like this:
Code:
int myInts[6]; // myInts expects 6 integer values
int myPins[] = {2, 4, 8, 3, 6};
int mySensVals[6] = {2, 4, -8, 3, 2};
char message[6] = "hello";
All codes above are valid.
Any line of code that has ( // ) will not be read by the compiler. It's called commenting and used to make notes. To comment out a big chunk with multiple lines of text use:
Code:
/* some comments
more comments
and something extra
*/
To access the values of the array:
Given:
Code:
int myArray[10]={1,3,2,4,3,2,7,8,9,11};
// myArray[0] contains 1
// myArray[9] contains 11
// myArray[10] is invalid and contains random information (other memory address) and usually will throw an error.
To assign a value to an array individually:
Code:
mySensVals[0] = 10;
To retrieve a value from an array:
Code:
x = mySensVals[4];
For LoopsThe
for statement is used to repeat a block of code. The structure has three parts: init, test, and update. Each part is separated by a semicolon ( ; ). The loop continues until the test part (condition) is false.
Code:
for (init; test; update) {
statements
}
init
statement executed once at the beginning of the looptest
if the test evaluates to true, the statements executeupdate
executes at the end of each iterationstatements
collection of codeLet's say I want to print "hello 0, hello 1, hello 2, hello 3, hello 4", I would write this:
Code:
for(int i=0; i < 5; i++) {
serial.println("hello " + i);
}
The variable i initially is 0 and is increased by one with every iteration until i is no longer less than 5. The variable i initially can be any value and the update part can increase by however many like i=i+2 instead of i++.
Ok, so this is interesting and all but when would you use this besides printing "hello"? One example would be to iterate through an array to get or set its values.
Code:
int myPins[] = {1,3,5,6,8};
for(int i=0; i<5;i++) {
serial.println(myPins[i]);
}
The code above prints out the list of pins stored in myPins array to serial.
There are other types of loops to iterate through code like
while loops and
do... while loops. The structure is different from each other but the basic idea is the same.
ConditionalsConditional allows the program to make decisions about which code to execute. This is the work-horse in programming. We touched on this a bit in the
FOR loop.
IF If the
test evaluates to be
true, then the statements enclosed within the block are executed otherwise nothing is executed.
Code:
if (test) {
statements
}
Code:
for (int i=0; i < 5; i++) {
serial.println("my value is " + i);
if ( i == 3) {
serial.println(" and 3 is special.");
}
}
The above code loops through and print
i and when
i is equal to ( == ) 3, it prints out an additional text.
Note that the equality operator ( == ) is used in a conditional and ( = ) is used to assign values to a variable like int a = 3.
Here are all of the relational operators you can use in conditionals:
Code:
!= (inequality)
< (less than)
<= (less than or equal to)
== (equality)
> (greater than)
>= (greater than or equal to)
ELSE Else extends the if condition to allow the program to choose between two or more block of code. It specifies a block of code to execute when the expression in
if is
false.
Code:
for (int i=0; i < 5; i++) {
if (i == 3) {
serial.println("3 is special!");
} else {
serial.println("nothing interesting");
}
}
The above code prints 'nothing interesting' until i = 3;
You can extends
if further (more than two blocks of code) with
else if.
Code:
if (expression) {
statements
} else if (expression) {
statements
} else {
statements
}
Code:
for(int i=0; i < 6; i++) {
if(i < 2) {
serial.println(i + ": I'm less than 2 ");
} else if (i < 4) {
serial.println(i + ": I'm 2 and greater but less than 4 ");
} else {
serial.println(i+": I'm 4 and greater.");
}
}
The output from the code above would look like:
Code:
0: I'm less than 2
1: I'm less than 2
2: I'm 2 and greater but less than 4
3: I'm 2 and greater but less than 4
4: I'm 4 and greater.
5: I'm 4 and greater.
A similar statement similar to
if else if is
switch/case. The structure is different but the idea is the same.