Skip to content

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 Introductory Exercises document. This document provides structured signposted links to the Learn-C tutorial website, C-Programming.com, the Arduino programming reference, and also links in some of the early exercises from the Introductory Exercises document.

Students with some C-programming skills may wish to skip this document, and go on to the only use the Introductory Exercises document or skip these and go straight to the first of the assessed exercises: Basic: LED Pattern

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 Learn-C, sections from C-Programming.com, and the programming references and examples/tutorials from the Arduino website.

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 Learn-C are aimed at programming for computer systems, (PC, Linux, etc), and not specifically for embedded C-programming, the likes of which we will be doing on the Arduino platform. As a result, there are some slight differences in the structure of the Arduino programs, or ‘Sketches’ as they are known. However, these Learn-C tutorials provide a good introduction to the basics of C programming, and we have supplemented them with signposted information from the Arduino website.

Each Learn-C tutorial provides a short introduction to each topic and has an on-line compiler that allows you to practice parts of each tutorial exercise, and topics discussed. These should provide sufficient background for you to complete the Mechatronics laboratory exercises.

Another useful resource is C-Programming.com, (again aimed at programming for computer systems), which provides a more in depth overview/tutorial for learning C, and you may only wish to consider the lessons from the ”Introduction and Basic C Features” section of the C-Tutorial

Note

In the next section of this document we have selected relevant sections from the C-Programming.com and C-Programming.com websites, and linked them with relevant material from the Arduino language guide. It is suggested that you access these tutorials as instructed in the following sections.

Arduino Language Reference

The Arduino programming reference is a web page, within the Arduino website, which provides a complete reference for Arduino functions, variables and programming structures for the core Arduino language. The language reference may seem a little daunting to a novice programmer, but is a very useful resource when you have some experience with Arduino programming.

Note

Relevant sections of the Arduino programming reference are linked throughout the remainder of this document, therefore, it is not necessary to read it before proceeding.

Arduino Build-in Examples within the Arduino IDE

There is a tutorials section within the Arduino web site, which provides you with a large range of tutorials, from simple getting started guides for the software and hardware, through to the descriptions of the Arduino Software’s build-in examples. There are also links into tutorials on the Arduino Project Hub, and links to other more advanced topics to help expand your capabilities.

Some of the links sections within the tutorials section section may be useful when you come to do your project.

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 Learn-C website, and interweave these with signposted links to relevant sections of the Arduino programming reference, and some of the early exercises from the Introductory Exercises document.

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 Wikipedia site.)

Hello World Example

A typical place to start with any programming language is the “Hello World” example - (See the “Hello World” example from the Learn-C Tutorial), this illustrates the basic structure of a programming language, using a program that writes the words “Hello World” to the “standard output”.

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 serial monitor in the Arduino IDE. This will be discussed in a later section.

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 #define directive replace a constant numeric value throughout your code, with a simple name defined in the global namespace. This can simplify the readability of your code and allow you to quickly update these distributed constants from a single location. The #define can also be used to create macros for small blocks of instructions, but care must be used, because it may be more appropriate to use a function instead.

Whilst programming, you may wish to link an external library into your Arduino code. The #include directive can be used to link external libraries into your work, such as an external library of code to interface a servo device.

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 Variables and numbers - tutorial from Learn-C provides a brief introduction to this, but does not consider the conversion between data types. See the variables section of Arduino Language Reference for a more detailed list of variable types and commands used to convert between them.

(Also see the Using Variables section of the Introduction to C lesson from C-Programming.com)

Arrays are a data structure that is essentially a list/collection of values of the same data type, arranged in an indexed manner - See Arrays - Learn-C Tutorial, and the Array section of the Arduino Language Reference. Multidimensional arrays can also store multidimensional data, by employing more than one set of indexes, e.g. a 2-dimensional array would be arranged similar to a spreadsheet of data and would have 2 sets of indices.

(Another, and more comprehensive, reference for Arrays in C, is provided in lesson-8 of C Tutorial in C-Programming.com.)

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 Learn-C, Functions tutorial and the Functions in C lesson from C-Programming.

The blink example is used in the Introductory Exercises, and can be accessed from the Arduino IDE files menu :

Files > Examples > 01.Basics > Blink

The in-build example tutorial for the Blink program is provided in the Arduino Tutorial section of the website. (Note: Arduino suggest using a 220𝛺 resistor, but in Exercise 1 will instruct you to use a 470𝛺 resistor.)

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 delay() function. This is a simple time delay function used to control the execution of your code.

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 for loop and the while loop structures. Both of these structures are discussed on Loops in C lesson from C-Programming.com. The Arduino Language reference also has sections for the for loop and the while loop structures

Decision making

One of the basic tasks we use computer systems for is decision making, based on logical conditions and inputs. The Learn-C Conditions tutorial provides an introduction to using the if statement on C. The Arduino language reference for the if statement is linked here, but you should also look at the different logical and comparison operation from the Arduino language reference, located in the structures section. The if statement, logical and relational operators are also discussed in the If Statements in C lesson of C-Programming.com.

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: Arduino language reference and C-Programming.com lesson.

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.

Finite state machines are a useful conceptual design tool when implementing sequenced and decision-based behaviour. You may find them useful when implementing the software for your project, but they will not be further discussed in this material.

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 Instructables.com “HOW-TO Use the ARDUINO SERIAL MONITOR” how-to web page provides an insight into the use of the serial monitor. You will notice that the code provided in this tutorial uses a number of the serial communications functions, which are described in the Arduino language reference. Data sent across the serial interface to the serial monitor.

The string data type

Before using the serial monitor it is worth going through the Learn-C Tutorial on Strings, and being aware of the String as an array of characters section and the String DataType object section of the Arduino language reference.

Push button and LED Example - Introductory Exercise

The Push button and LED example is based on the Button example from the in-built Arduino examples - the tutorial for the Button example is linked, here. The code provided in the laboratory worksheet demonstrates how the serial monitor can be incorporated into an Arduino program, to monitor an internal variable within the code.

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