Supplementary Signposting Document to Accompany The Introductory Exercises
Pre-face
This document is aimed at students with little or no C-programming experience, but maybe used as a signposted reference for anyone. For students with little or no C-programming experience, this document can be used in parallel to the
Students with some C-programming skills may wish to skip this document, and go on to the only use the
Introduction
In previous years it has become evident that some groups of students, taking the Mechatronics modules, do not have any background experience on programming in C/C++ or experience with using Arduino or other microcontroller based development systems. As a result, these students have struggled with some of the basics of the Mechatronics laboratory activities, due to not having learned any C-programming skills before starting this module.
To provide you with comprehensive instruction into C-programming is both outside the scope of this module and would take far more time than is available for the teaching/contact time allocated to this module. There are countless very good on-line resources for self-paced learning of programming languages. Therefore, instead of writing yet another tutorial, this document will provide you with some signposts to resources for basic C programming topics that we think are necessary to facilitate the laboratory activities within this module.
Note
This document is not aimed at providing you with a comprehensive list of topics required for this module, and it may be necessary for you to discover further resources to improve your programming knowledge and skills.
In this iteration of this document we will signpost some of the tutorial resources from
It will be up to you to self-guide your own learning to improve your skills and understanding, required for the labs and your project activities, and if necessary discover further resources. (If you do find topics/resources that you think would be of benefit, please make us aware of these, and if appropriate, we may provide further sign posting to these materials.)
The remaining subsections of the introduction highlight some resources you may find useful whilst going through the remainder of this document, and while you are completing the mechatronics module laboratory exercises and project.
External tutorial websites linked from this document
The tutorials provided on
Each
Another useful resource is
Note
In the next section of this document we have selected relevant sections from the
Arduino Language Reference
The
Note
Relevant sections of the
Arduino Build-in Examples within the Arduino IDE
There is a
Some of the links sections within the
Introduction to Programming an Arduino in C.
The remainder of this document is aimed at providing you with a structured set of signposted links to help build your understanding of the basics of C-programming. This document will signpost you to a number of tutorials on the
The Arduino programming libraries are a series of C/C++ functions that can be called from the code you write. The native programming languages for Arduino are either C or C++. Throughout the remainder of this module, we will only be considering programming Arduino in C.
(For those interested, there is a brief overview and history of the C-programming language on the
Hello World Example
A typical place to start with any programming language is the “Hello World” example - (See the
The Standard Output in Arduino
In Arduino, there is no standard output, because it is an embedded operating system operating without a console terminal. Generally, we direct the standard output to the
The following programming syntax, (linked from the Arduino language reference), are necessary for correct implementation of your code:
Code comments are a readable explanation or annotation of the code that you have written. You should provide comments throughout your code to help explain what you have written and why you have written it in that way. Code comments are an invaluable tool to help you debug code that is not working in the manner intended, and should be updated when code is changed. Links to how to implement code commenting in Arduino:
Simple Macros are a very useful tool when writing clear and effective embedded code, not only helping with the readability of your code, but also help when trying to debug your work. The
Whilst programming, you may wish to link an external library into your Arduino code. The
Variables and Numbers
One of the key considerations when programming computer systems and embedded systems, is the handling and storage of different types of numbers. The
(Also see the Using Variables section of the
Arrays are a data structure that is essentially a list/collection of values of the same data type, arranged in an indexed manner - See
(Another, and more comprehensive, reference for Arrays in C, is provided in
Functions
Functions are blocks of code that perform a predefined set of commands, and are either used to ‘tidy-up’ your code to make it more readable, or to perform repeated activities. As you write your Arduino code, you will be using some of the built-in and 3rd party library functions, and you may also wish to define your own.
Functions are discussed in the
Blink Example - Introductory Exercise
The
Files > Examples > 01.Basics > Blink
The
The Blink example code is a simple test program, often used to ensure the Arduino compiler is working correctly, and the hardware can communicate with the IDE correctly. Look at the Arduino code example, there are three sections to the code:
- Global namespace - area at the top of the code used for declaration of global variables and location for compiler directives, such as #define and #include instructions
setup() function - This function runs once at the start of the program and is generally used to initialise variables, hardware and libraries, and to define I/O pin modes.loop() function - This function is generally the main body of your Arduino program, and loop continuously, allowing your system hardware to be read and controlled each loop iteration.
You will notice the use of the
At this point is worth signposting:
- The while loop is a programming structure that will continuously loop until a logical termination is satisfied
- The for loop is a programming structure that will loop for a pre-specified number of iterations.
Learn-C provides tutorials for the
Decision making
One of the basic tasks we use computer systems for is decision making, based on logical conditions and inputs. The Learn-C
At this point it is worth mentioning the switch case structure in C. This is a more advanced topic but you may find it a useful programming structure for your projects:
The switch case structure can be used in the place of a long if structure that uses a definite value of a control variable to select one or more blocks of code to be executed. Switch case structures and widely used when programming state machine type behaviour.
Serial Monitor
As previously mentioned, there is no standard output for Arduino hardware. When using Arduino hardware, a serial terminal is often used to read user generated messages from the Arduino hardware, or to stream variable values to a readable interface. The Serial Monitor, in the Arduino IDE, provides a convenient serial terminal to view text based messages generated from your Arduino code.
The
The string data type
Before using the serial monitor it is worth going through the Learn-C Tutorial on
Push button and LED Example - Introductory Exercise
The
Final Comment
The signposting of background C programming resources provided in the previous sections of this document should be sufficient for you to implement the remaining laboratory work in this Mechatronics module. You may wish to find further resources, either on-line or in print, to help you practice your C programming skills, but you will need to discover these yourself.
If you do find further topics/resources that you think would be beneficial in enriching this document, please make us aware of these topics/resources. If appropriate, we may use some of these resources to improve future iterations of this document