Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Table of Contents

Note : If you are an absolute beginner to programming, it is a good idea to use the code+blocks programming option in Tinkercad, and copy over the generated C++ code to Arduino IDE.

Comments

  • Comments are for you – the programmer or any other human who might read your code

  • Comments are not run on the Arduino board

C++

Blocks


Code Block
languagecpp
themeConfluence
/*
  
   multi-line
 
  
  comment
*/
]] ></ac:plain-text-body></ac:structured-macro><ac:structured-macro ac:name="code" ac:schema-version="1" ac:macro-id="34cc66ab-2eaa-4ad8-8606-d2180728f957"><ac:parameter ac:name="language">cpp</ac:parameter><ac:parameter ac:name="theme">Confluence</ac:parameter><ac:plain-text-body><![CDATA[

Code Block
languagecpp
// single line comment

]] ></ac:plain-text-body></ac:structured-macro></div></td><td><div class="content-wrapper"><p><br /></p></div><p><ac:image ac:thumbnail="true" ac:width="200"><ri:attachment ri:filename="image2020-1-19_21-20-38.png" /></ac:image></p><p><ac:image ac:thumbnail="true" ac:width="260"><ri:attachment ri:filename="image2020-1-26_16-49-10.png" /></ac:image></p><p>Note: title block comment is used in the beginning, single-line comment is embedded into the code</p></td></tr></tbody></table><h3>Statements</h3><ul><li>Statements are compiled into an &lsquo;executable&rsquo; of 1s and 0s, which are run on the Arduino.</li><li>Statements end with a ;</li></ul><table class="wrapped"><colgroup><col /><col /></colgroup><tbody><tr><th>C++</th><th>Blocks</th></tr><tr><td><div class="content-wrapper"><ac:structured-macro ac:name="code" ac:schema-version="1" ac:macro-id="ba1c0489-b530-4751-8845-c174493b4c8b"><ac:parameter ac:name="language">cpp</ac:parameter><ac:parameter ac:name="theme">Confluence</ac:parameter><ac:plain-text-body><![CDATA[



Image Added

Image Added

Statements

  • Statements are compiled into an ‘executable’ of 1s and 0s, which are run on the Arduino.

  • Statements end with a ;

C++

Blocks

Code Block
languagecpp
c = a+b;

 // c is assigned the sum of a, b

]] ></ac:plain-text-body></ac:structured-macro></div></td><td><div class="content-wrapper"><p><ac:image ac:thumbnail="true" ac:width="230"><ri:attachment ri:filename="image2020-1-19_21-22-0.png" /></ac:image></p></div></td></tr></tbody></table><h3>Variables</h3><ul><li>Variables are containers to store intermediate data</li><li>In C language, a variable needs to have a type, which specifies the nature and the size of data it holds</li><li>Some of the standard data types are</li><li><strong>int</strong> : 16-bit integers in the range -32,768 to 32,767</li><li><strong>unsigned int </strong>: 16-bit positive integers (incl 0), 0 to 65535</li><li><strong>char</strong> : 8-bit integers from -128 to 127. Characters such as &lsquo;0&rsquo;, &lsquo;A&rsquo;, &lsquo;a&rsquo; etc. are represented using ASCII (48, 65, 97 respectively - read up more!). BYTE is the same as unsigned char</li><li><strong>float</strong> : for floating point numbers such as 2.25, -5.875 (32-bits)</li><li>Types can also be user defined (through <strong>class</strong>es)</li><li>Variables can be declared with or without initialization</li></ul><table class="wrapped"><colgroup><col /><col /></colgroup><tbody><tr><th>C++</th><th>Blocks</th></tr><tr><td><div class="content-wrapper"><ac:structured-macro ac:name="code" ac:schema-version="1" ac:macro-id="dcfd7877-17e7-4e2a-98a4-097c26790ec6"><ac:parameter ac:name="language">cpp</ac:parameter><ac:parameter ac:name="theme">Confluence</ac:parameter><ac:plain-text-body><![CDATA[

Image Added

Variables

  • Variables are containers to store intermediate data

  • In C language, a variable needs to have a type, which specifies the nature and the size of data it holds

  • Some of the standard data types are

  • int : 16-bit integers in the range -32,768 to 32,767

  • unsigned int : 16-bit positive integers (incl 0), 0 to 65535

  • char : 8-bit integers from -128 to 127. Characters such as ‘0’, ‘A’, ‘a’, etc. are represented using ASCII (48, 65, 97 respectively - read up more!). BYTE is the same as unsigned char

  • float : for floating point numbers such as 2.25, -5.875 (32-bits)

  • Types can also be user-defined (through classes)

  • Variables can be declared with or without initialization

C++

Blocks


Code Block
languagecpp
int a; // declaration
]]

></ac:plain-text-body></ac:structured-macro><ac:structured-macro ac:name="code" ac:schema-version="1" ac:macro-id="1d2486f8-a79d-4755-aaa6-ac3cf977f737"><ac:parameter ac:name="language">cpp</ac:parameter><ac:parameter ac:name="theme">Confluence</ac:parameter><ac:plain-text-body><![CDATA[
int a = 0; // declaration and initialization
]] ></ac:plain-text-body></ac:structured-macro><p><br /></p></div><p><br /></p></td><td><div class="content-wrapper"><p><ac:image ac:height="150"><ri:attachment ri:filename="image2020-1-19_16-50-26.png" /></ac:image></p><p>Note: Creating a variable in Blocks declares it and initializes it to 0.</p></div></td></tr></tbody></table><p>A variable appearing on the left-hand side of an assignment statement assigns a value to it.</p><table class="wrapped"><colgroup><col /><col /></colgroup><tbody><tr><th>C++</th><th>Blocks</th></tr><tr><td><div class="content-wrapper"><ac:structured-macro ac:name="code" ac:schema-version="1" ac:macro-id="87b7b374-93fe-48c9-aac0-24808412811c"><ac:parameter ac:name="language">cpp</ac:parameter><ac:parameter ac:name="theme">Confluence</ac:parameter><ac:plain-text-body><![CDATA[


Image Added

Note: Creating a variable in Blocks declares it and initializes it to 0

A variable appearing on the left-hand side of an assignment statement assigns a value to it.

C++

Blocks

Code Block
languagecpp
a = 10; // assigning 10 to a

]] ></ac:plain-text-body></ac:structured-macro></div></td><td><div class="content-wrapper"><p><ac:image ac:thumbnail="true" ac:width="150"><ri:attachment ri:filename="image2020-1-19_17-31-32.png" /></ac:image></p></div></td></tr></tbody></table><p>A variable value is used when it appears in a comparison or on the right-hand side of an expression/assignment.</p><table class="wrapped relative-table" style="width: 62.7424%;"><colgroup><col style="width: 64.9724%;" /><col style="width: 34.9171%;" /></colgroup><tbody><tr><th>C++</th><th>Blocks</th></tr><tr><td><div class="content-wrapper"><ac:structured-macro ac:name="code" ac:schema-version="1" ac:macro-id="96f4bcc6-c2a8-4bd7-beb9-c9062cb6add5"><ac:parameter ac:name="language">cpp</ac:parameter><ac:parameter ac:name="theme">Confluence</ac:parameter><ac:plain-text-body><![CDATA[

Image Added

A variable value is used when it appears in a comparison or on the right-hand side of an expression/assignment.

C++

Blocks

Code Block
languagecpp
(a == 1) // checking if a is equal to 1

]] ></ac:plain-text-body></ac:structured-macro></div></td><td><div class="content-wrapper"><p><ac:image ac:thumbnail="true" ac:width="170"><ri:attachment ri:filename="image2020-1-19_17-34-54.png" /></ac:image></p></div></td></tr><tr><td colspan="2"><p>The comparison above returns a 'true' or

Image Added

The comparison above returns a 'true' or 'false',

which

is

usually

used

for

selection

or

iteration.

Read

up

more

about

comparison

operators!

</p><p>Important : In

Important : In C++,

'=='

is

a

comparison

operator,

'='

is

the

assignment

operator.

Using

one

instead

of

the

other

will

not

cause

a

syntax

error,

but

will

cause

incorrect

program

functionality.</p></td></tr></tbody></table><table class="wrapped"><colgroup><col /><col /></colgroup><tbody><tr><th>C++</th><th>Blocks</th></tr><tr><td><div class="content-wrapper"><ac:structured-macro ac:name="code" ac:schema-version="1" ac:macro-id="3eb24a2a-41d1-4a3f-bac9-4395251b987c"><ac:parameter ac:name="language">cpp</ac:parameter><ac:parameter ac:name="theme">Confluence</ac:parameter><ac:plain-text-body><![CDATA[

functionality.

C++

Blocks

Code Block
languagecpp
b = a + 10; // variable b is assigned the value of variable a plus 10.

]] ></ac:plain-text-body></ac:structured-macro></div></td><td><div class="content-wrapper"><p><ac:image ac:thumbnail="true" ac:width="230"><ri:attachment ri:filename="image2020-1-19_17-53-5.png" /></ac:image></p></div></td></tr></tbody></table><h3>Selection</h3><ul><li>A program that executes all the statements in a sequence is probably not very interesting.</li><li>Sometimes, a part of a program must be executed based on a certain condition (selection).</li><li>Given below is an example of an <a href="https://www.arduino.cc/reference/en/language/structure/control-structure/if/">if-else</a> statement. The&nbsp;{} is optional if there is only one line being executed conditionally for if or else.</li></ul><table class="wrapped"><colgroup><col /><col /></colgroup><tbody><tr><th>C++</th><th>Blocks</th></tr><tr><td><div class="content-wrapper"><ac:structured-macro ac:name="code" ac:schema-version="1" ac:macro-id="eaee27e1-6c70-4546-b82a-5af2bb32fef7"><ac:parameter ac:name="language">cpp</ac:parameter><ac:parameter ac:name="theme">Confluence</ac:parameter><ac:plain-text-body><![CDATA[if(a>0){

Image Added

Selection

  • A program that executes all the statements in a sequence is probably not very interesting.

  • Sometimes, a part of a program must be executed based on a certain condition (selection).

  • Given below is an example of an if-else statement. The {} is optional if there is only one line being executed conditionally for if or else.

C++

Blocks

Code Block
languagecpp
if(a>0){
    c = a+b;
//executed when a>0
}
 
else{
    
c = a-b;
//executed when a<0 or a==0
}
]] ></ac:plain-text-body></ac:structured-macro></div></td><td><div class="content-wrapper"><p><ac:image ac:thumbnail="true" ac:width="240"><ri:attachment ri:filename="image2020-1-19_18-1-56.png" /></ac:image></p></div></td></tr></tbody></table><p>To Do : Read up about <a href="https://www.arduino.cc/reference/en/language/structure/control-structure/switchcase/">switch-case</a></p><h3>Iteration</h3><ul><li>Sometimes, a part of a program must be executed repeatedly until a certain condition is satisfied (iteration / loop)</li><li>Given below is an example for an <a href="https://www.arduino.cc/reference/en/language/structure/control-structure/while/">while</a>&nbsp;loop.</li></ul><table class="wrapped"><colgroup><col style="width: 439.0px;" /><col style="width: 366.0px;" /></colgroup><tbody><tr><th>C++</th><th>Blocks</th></tr><tr><td><div class="content-wrapper"><ac:structured-macro ac:name="code" ac:schema-version="1" ac:macro-id="08b87112-4760-4b8b-83fe-7f5a1813e9ce"><ac:parameter ac:name="language">cpp</ac:parameter><ac:parameter ac:name="theme">Confluence</ac:parameter><ac:plain-text-body><![CDATA[while(a>0){
Image Added

To Do : Read up about switch-case

Iteration

  • Sometimes, a part of a program must be executed repeatedly until a certain condition is satisfied (iteration / loop)

  • Given below is an example for an while loop.

C++

Blocks

Code Block
languagecpp
while(a>0){
    a = a-b;
/* executed over and over until a<0 or a==0 */
}

]] ></ac:plain-text-body></ac:structured-macro></div></td><td><div class="content-wrapper"><p><ac:image ac:thumbnail="true" ac:width="260"><ri:attachment ri:filename="image2020-1-19_20-48-6.png" /></ac:image></p></div></td></tr></tbody></table><p>To Do : Read up about <a href="https://www.arduino.cc/reference/en/language/structure/control-structure/for/">for loop</a>, <a href="https://www.arduino.cc/reference/en/language/structure/control-structure/dowhile/">do-while</a> loop</p><h3>Functions</h3><ul><li>A function is a segment of code which can be invoked from different parts of the program in a parameterized manner</li><li>A function involves 3 aspects</li><li>Declaration (typically done in header files with extension .h)</li></ul><ac:structured-macro ac:name="code" ac:schema-version="1" ac:macro-id="90c935d2-6b31-40a8-803e-fb1ae498c57b"><ac:parameter ac:name="language">cpp</ac:parameter><ac:parameter ac:name="theme">Confluence</ac:parameter><ac:plain-text-body><![CDATA[

Image Added

To Do : Read up about for loopdo-while loop

Functions

  • A function is a segment of code which can be invoked from different parts of the program in a parameterized manner

  • A function involves 3 aspects

  • Declaration (typically done in header files with extension .h)

Code Block
languagecpp
int add(int,

...

int)

...

  • Definition (typically

...

  • done

...

  • by

...

  • the

...

  • library

...

  • vendor)

...

Code Block
languagecpp
int add(int x, int y){ // x, y are function 'parameters'

...

                        //of the function 'add’

...

  return x+y;
}

...

  • Usage (usually,

...

  • we

...

  • need

...

  • to

...

  • do

...

  • only

...

  • this)

...

Code Block
languagecpp
{
  … //other statements
 

...

    c = add(a,b);
 
/* causes add() to be called with a, b passed as arguments.

...

    The value returned is assigned to c */

...


 

...

  … //other statements
}

...

Note : Blocks do not allow you to declare or define functions explicitly. However, some built-in functions such as digitalWrite() can be called using appropriate blocks, as shown below.

C++

Blocks

Code Block
languagecpp
{
  digitalWrite(11, HIGH);
}
]] ></ac:plain-text-body></ac:structured-macro></div></td><td><div class="content-wrapper"><p><ac:image ac:thumbnail="true" ac:width="200"><ri:attachment ri:filename="image2020-1-19_21-4-33.png" /></ac:image></p></div></td></tr></tbody></table><h5>Setup() and loop() functions</h5><ul><li>Setup and loop are functions called automatically (unlike all other functions needs to be called explicitly)</li><li>setup() is called <em>once</em>, when you release the reset button / plug in your Arduino / upload a new code onto the board</li><li>loop() is called <em>repeatedly</em>; anything inside the loop() gets executed over and over as long as the system remains powered on</li></ul><p><ac:image ac:width="350"><ri:attachment ri:filename="image2019-7-31_16-58-39.png" /></ac:image></p><p>Note : When using blocks, any setup code required is automatically inserted into setup(). The main program logic which implemented using blocks goes into loop().</p><h3>Good Coding Styles / Practices</h3><p>Good code is code which is readable, comprehensible, and extensible. If you can't read/comprehend, you can't debug the code, you can't improve the code, you can't collaborate in a team. Hence the importance of following good coding styles/practices.</p><ul><li><strong>Write detailed comments</strong>. This will be very useful later for you yourself or other people who read your code.</li><li><strong>Use descriptive variable names</strong>&nbsp;for variable etc. rather than short, generic names like i, j, k. This will help your code more readable and comprehensible.</li><li><strong>Indent your code properly</strong>. Indent with tabs, not with spaces. This also makes your code easier to read.</li><li><strong>Use constants/variables</strong> for values that appear at multiple places rather than hard-coding values.</li></ul><ac:structured-macro ac:name="code" ac:schema-version="1" ac:macro-id="a54338b1-33a0-469d-9de8-a822bbc84a62"><ac:parameter ac:name="language">cpp</ac:parameter><ac:parameter ac:name="theme">Confluence</ac:parameter><ac:parameter ac:name="title">Poor Coding Style</ac:parameter><ac:plain-text-body><![CDATA[void setup(){



Image Added

Setup() and loop() functions

  • Setup and loop are functions called automatically (unlike all other functions needs to be called explicitly)

  • setup() is called once, when you release the reset button / plug in your Arduino / upload a new code onto the board

  • loop() is called repeatedly; anything inside the loop() gets executed over and over as long as the system remains powered on

...

Note : When using blocks, any setup code required is automatically inserted into setup(). The main program logic which implemented using blocks goes into loop().

Good Coding Styles / Practices

Good code is code which is readable, comprehensible, and extensible. If you can't read/comprehend, you can't debug the code, you can't improve the code, you can't collaborate in a team. Hence the importance of following good coding styles/practices.

  • Write detailed comments. This will be very useful later for you yourself or other people who read your code.

  • Use descriptive variable names for variable etc. rather than short, generic names like i, j, k. This will help your code more readable and comprehensible.

  • Indent your code properly. Indent with tabs, not with spaces. This also makes your code easier to read.

  • Use constants/variables for values that appear at multiple places rather than hard-coding values.

Poor Coding Style
Code Block
languagecpp
void setup(){
    //setup code
}
void loop()
       {

...

                // Poor position for opening brace.
    

...

int j = 100;     

...

   

...

// Non-descriptive variable name
  analogWrite(5, j);    

...

/*  

...

Indented with spaces, and not aligned with the previous line.
 

...

                           Hard-coded values.

...


                            No comment to indicate what the line does.*/

...


                }       // Poor position for closing brace.

...

Good Coding Style
Code Block
languagecpp
const int leftMotor = 5; // pin 5 used to control left motor of the robot
void setup(){
    

...

//setup code
}
void loop(){
    

...

int leftMotorSpeed = 100;
    

...

analogWrite(leftMotor, leftMotorSpeed); // to write the analog value 100 to pin 5.
}

...