Create an Object Tracking System: Using Built-in Arduino Libraries

How to use the native Arduino libraries, how to create a custom library, and generate the framework that is to be used for the duration of this project series.

Table of Contents:

  1. Using Native Libraries
  2. Write a Custom Library
  3. Framework for Project Series

Using Native Libraries

The standard Arduino IDE comes loaded with a variety of example sketches and libraries.  Using native libraries gives the user access to pre-baked functions and methods that are designed to simplify interfacing with more complex peripherals such as Ethernet communications, mouse functionality, SPI communications, etc.  To implement a given library, all that is required is the #include preprocessor directive followed by the file name of the library you wish to use. Here is an example:

                    #include "ExampleLibraryName.h"  // preprocessor directive

void Setup()
{
pinMode(13, OUTPUT);             // Set Pin 13 as Output
}

void loop()
{
  digitalWrite(13, HIGH);        // turn the LED on (HIGH is the voltage level)
  delay(100);                    // wait for a second
  digitalWrite(13, LOW);         // turn the LED off by making the voltage LOW
  delay(100);    
}
                  

The code snippet above just shows the syntax required to implement a library named "ExampleLibraryName.h" and does not actually call any methods or functions from the fictitious library.

A library consists of 2 parts: a header file and a source file.  The header file (denoted by the *.h extension) is a essentially a list of all functions and methods that are defined in the source file.  A header file may reference other libraries by including preprocessor directives like in the code snippet above.  The source file is where the methods or functions are actually coded. The source and header files can be written in C or C++ as the Arduino sketch compiler (avr-gcc) works with the C and C++ languages.  For a tutorial on writing a C++ library you can refer to The Arduino Guide.  The next section will cover writing a library using the simplified Arduino syntax.     

Writing a Custom Library

The following example is a bit like swatting a fly with a brick: the methodology far exceeds the need but for the sake of simple examples it gets the job done.  I want to write a sketch to flash an LED: turn on for X time and then off for Y time.  The following code will do this:

                    void setup()              // initialize function
{
 pinMode(13, OUTPUT);     // define pin 13 as an output.
}

void loop()               // main loop
{
  digitalWrite(13, HIGH);  // turn the LED ON
  delay(100);              // wait for 100ms
  digitalWrite(13, LOW);   // turn the LED OFF
  delay(100);              // wait for 100ms
}
                  

It is considered good practice to not execute code inside the main loop directly.  In order to change the previous code snippet to adhere to this convention you will need to open a second tab.  Look at the differences between the code snippets below and compare them to one above.  As you will notice the executable code is in the second snippet and that file is referenced in the preprocessor directive at the top of the main file.

                    /*************  Main File  *************/

#include "BlinkLibraryTest.C"   // PREPROCESSOR DIRECTIVE

void setup()
{
 pinMode(13, OUTPUT);           // initialize digital pin 13 as an output.
}

void loop()                     // main loop
{
  BlinkPattern();               // function being called
}
                  

                    /*************  Source File  *************/

#include "Arduino.h"        // PREPROCESSOR DIRECTIVE referencing Arduino library for sketch syntax

void BlinkPattern()         // function being called in main file
{
  digitalWrite(13, HIGH);   // turn the LED ON
  delay(100);               // wait for 100ms
  digitalWrite(13, LOW);    // turn the LED OFF
  delay(100);               // wait for 100ms
}
                  

For larger programs a header file can be used to create an aggregate point for source files and other preprocessor directives.  The image below shows a rudimentary flow diagram of the interaction of a header file, source files, and a main.

In the last example below, you will see the alterations to include a header file with the previous LED blinking program.  If the previous code structure was excessive for this tiny bit of code, adding a header is even more so.  See the changes below: 

                    /*************  Main File  *************/

#include "BlinkLibraryTest.h"    // PREPROCESSOR DIRECTIVE calling header file

void setup()
{
 pinMode(13, OUTPUT);            // initialize digital pin 13 as an output.
}

void loop()
{
  BlinkPattern();
}
                  

                    /*************  Source File  *************/

#include "Arduino.h"       // PREPROCESSOR DIRECTIVE calling Arduino library

void BlinkPattern()        // function being called in main file
{
  digitalWrite(13, HIGH);  // turn the LED ON
  delay(100);              // wait for 100ms
  digitalWrite(13, LOW);   // turn the LED OFF
  delay(100);              // wait for 100ms
}
                  

                    /*************  Header File  *************/

#include "BlinkLibraryTest.c"  // PREPROCESSOR DIRECTIVE calling source file

                  

Framework for Project Series

Now that we have a general understanding of source/header file hierarchy, we can start the framework for this project series.  We will need a header and sections dedicated to the following operations:
  1. Servo Control
  2. Ultrasonic Sensor Operation
  3. Object Detection Algorithm
  4. Bluetooth Connectivity
  5. Smart Device Control

You should have a *.c file for every bullet above, a header, and a main file.  Most of the files will be left blank.  The header should be filled with the #include statements linking all the source files together and main file should have the #include statement linking the header file.  The next segment in the project series will cover servo control and will build on this code framework.  
Previous
Next Post »
My photo

Hi, I`m Sostenes, Electrical Technician and PLC`S Programmer.
Everyday I`m exploring the world of Electrical to find better solution for Automation. I believe everyday can become a Electrician with the right learning materials.
My goal with BLOG is to help you learn Electrical.
Related Posts Plugin for WordPress, Blogger...

Label

KITAIFA NEWS KIMATAIFA MICHEZO BURUDANI SIASA TECHNICAL ARTICLES f HAPA KAZI TU. LEKULE TV EDITORIALS ARTICLES DC DIGITAL ROBOTICS SEMICONDUCTORS MAKALA GENERATOR GALLERY AC EXPERIMENTS MANUFACTURING-ENGINEERING MAGAZETI REFERENCE IOT FUNDAMENTAL OF ELECTRICITY ELECTRONICS ELECTRICAL ENGINEER MEASUREMENT VIDEO ZANZIBAR YETU TRANSDUCER & SENSOR MITINDO ARDUINO RENEWABLE ENERGY AUTOMOBILE SYNCHRONOUS GENERATOR ELECTRICAL DISTRIBUTION CABLES DIGITAL ELECTRONICS AUTOMOTIVE PROTECTION SOLAR TEARDOWN DIODE AND CIRCUITS BASIC ELECTRICAL ELECTRONICS MOTOR SWITCHES CIRCUIT BREAKERS MICROCONTROLLER CIRCUITS THEORY PANEL BUILDING ELECTRONICS DEVICES MIRACLES SWITCHGEAR ANALOG MOBILE DEVICES CAMERA TECHNOLOGY GENERATION WEARABLES BATTERIES COMMUNICATION FREE CIRCUITS INDUSTRIAL AUTOMATION SPECIAL MACHINES ELECTRICAL SAFETY ENERGY EFFIDIENCY-BUILDING DRONE NUCLEAR ENERGY CONTROL SYSTEM FILTER`S SMATRPHONE BIOGAS POWER TANZIA BELT CONVEYOR MATERIAL HANDLING RELAY ELECTRICAL INSTRUMENTS PLC`S TRANSFORMER AC CIRCUITS CIRCUIT SCHEMATIC SYMBOLS DDISCRETE SEMICONDUCTOR CIRCUITS WIND POWER C.B DEVICES DC CIRCUITS DIODES AND RECTIFIERS FUSE SPECIAL TRANSFORMER THERMAL POWER PLANT cartoon CELL CHEMISTRY EARTHING SYSTEM ELECTRIC LAMP ENERGY SOURCE FUNDAMENTAL OF ELECTRICITY 2 BIPOLAR JUNCTION TRANSISTOR 555 TIMER CIRCUITS AUTOCAD C PROGRAMMING HYDRO POWER LOGIC GATES OPERATIONAL AMPLIFIER`S SOLID-STATE DEVICE THEORRY DEFECE & MILITARY FLUORESCENT LAMP HOME AUTOMATION INDUSTRIAL ROBOTICS ANDROID COMPUTER ELECTRICAL DRIVES GROUNDING SYSTEM BLUETOOTH CALCULUS REFERENCE DC METERING CIRCUITS DC NETWORK ANALYSIS ELECTRICAL SAFETY TIPS ELECTRICIAN SCHOOL ELECTRON TUBES FUNDAMENTAL OF ELECTRICITY 1 INDUCTION MACHINES INSULATIONS ALGEBRA REFERENCE HMI[Human Interface Machines] INDUCTION MOTOR KARNAUGH MAPPING USEUL EQUIATIONS AND CONVERSION FACTOR ANALOG INTEGRATED CIRCUITS BASIC CONCEPTS AND TEST EQUIPMENTS DIGITAL COMMUNICATION DIGITAL-ANALOG CONVERSION ELECTRICAL SOFTWARE GAS TURBINE ILLUMINATION OHM`S LAW POWER ELECTRONICS THYRISTOR USB AUDIO BOOLEAN ALGEBRA DIGITAL INTEGRATED CIRCUITS FUNDAMENTAL OF ELECTRICITY 3 PHYSICS OF CONDUCTORS AND INSULATORS SPECIAL MOTOR STEAM POWER PLANTS TESTING TRANSMISION LINE C-BISCUIT CAPACITORS COMBINATION LOGIC FUNCTION COMPLEX NUMBERS ELECTRICAL LAWS HMI[HUMANI INTERFACE MACHINES INVERTER LADDER DIAGRAM MULTIVIBRATORS RC AND L/R TIME CONSTANTS SCADA SERIES AND PARALLEL CIRCUITS USING THE SPICE CIRCUIT SIMULATION PROGRAM AMPLIFIERS AND ACTIVE DEVICES BASIC CONCEPTS OF ELECTRICITY CONDUCTOR AND INSULATORS TABLES CONDUITS FITTING AND SUPPORTS CONTROL MOTION ELECTRICAL INSTRUMENTATION SIGNALS ELECTRICAL TOOLS INDUCTORS LiDAR MAGNETISM AND ELECTROMAGNETISM PLYPHASE AC CIRCUITS RECLOSER SAFE LIVING WITH GAS AND LPG SAFETY CLOTHING STEPPER MOTOR SYNCHRONOUS MOTOR AC METRING CIRCUITS APPS & SOFTWARE BASIC AC THEORY BECOME AN ELECTRICIAN BINARY ARITHMETIC BUSHING DIGITAL STORAGE MEMROY ELECTRICIAN JOBS HEAT ENGINES HOME THEATER INPECTIONS LIGHT SABER MOSFET NUMERATION SYSTEM POWER FACTORS REACTANCE AND IMPEDANCE INDUCTIVE RESONANCE SCIENTIFIC NOTATION AND METRIC PREFIXES SULFURIC ACID TROUBLESHOOTING TROUBLESHOOTING-THEORY & PRACTICE 12C BUS APPLE BATTERIES AND POWER SYSTEMS ELECTROMECHANICAL RELAYS ENERGY EFFICIENCY-LIGHT INDUSTRIAL SAFETY EQUIPMENTS MEGGER MXED-FREQUENCY AC SIGNALS PRINCIPLE OF DIGITAL COMPUTING QUESTIONS REACTANCE AND IMPEDANCE-CAPATIVE RECTIFIER AND CONVERTERS SEQUENTIAL CIRCUITS SERRIES-PARALLEL COMBINATION CIRCUITS SHIFT REGISTERS BUILDING SERVICES COMPRESSOR CRANES DC MOTOR DRIVES DIVIDER CIRCUIT AND KIRCHHOFF`S LAW ELECTRICAL DISTRIBUTION EQUIPMENTS 1 ELECTRICAL DISTRIBUTION EQUIPMENTS B ELECTRICAL TOOL KIT ELECTRICIAN JOB DESCRIPTION LAPTOP THERMOCOUPLE TRIGONOMENTRY REFERENCE UART WIRELESS BIOMASS CONTACTOR ELECTRIC ILLUMINATION ELECTRICAL SAFETY TRAINING FILTER DESIGN HARDWARE INDUSTRIAL DRIVES JUNCTION FIELD-EFFECT TRANSISTORS NASA NUCLEAR POWER SCIENCE VALVE WWE oscilloscope 3D TECHNOLOGIES COLOR CODES ELECTRIC TRACTION FEATURED FLEXIBLE ELECTRONICS FLUKE GEARMOTORS INTRODUCTION LASSER MATERIAL PID PUMP SEAL ELECTRICIAN CAREER ELECTRICITY SUPPLY AND DISTRIBUTION MUSIC NEUTRAL PERIODIC TABLES OF THE ELEMENTS POLYPHASE AC CIRCUITS PROJECTS REATORS SATELLITE STAR DELTA VIBRATION WATERPROOF