Hello

Welcome lekule blog

Hi, I`m Sostenes, Electrical Technician and PLC`S Programmer.
Everyday I`m exploring the world of Electrical to find better solution for Automation.
together in the world. #lekule86
Join us on

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.  

Share this:

ABOUTME

Hi all. This is deepak from Bthemez. We're providing content for Bold site and we’ve been in internet, social media and affiliate for too long time and its my profession. We are web designer & developer living India! What can I say, we are the best..

Post a Comment
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.

Labels

LEKULE TV EDITORIALS ARTICLES DC ROBOTICS DIGITAL SEMICONDUCTORS GENERATOR AC EXPERIMENTS MANUFACTURING-ENGINEERING REFERENCE FUNDAMENTAL OF ELECTRICITY ELECTRONICS ELECTRICAL ENGINEER MEASUREMENT TRANSDUCER & SENSOR VIDEO ARDUINO RENEWABLE ENERGY AUTOMOBILE TEARDOWN SYNCHRONOUS GENERATOR DIGITAL ELECTRONICS ELECTRICAL DISTRIBUTION CABLES AUTOMOTIVE MICROCONTROLLER SOLAR PROTECTION DIODE AND CIRCUITS BASIC ELECTRICAL ELECTRONICS MOTOR SWITCHES CIRCUIT BREAKERS CIRCUITS THEORY PANEL BUILDING ELECTRONICS DEVICES MIRACLES SWITCHGEAR ANALOG MOBILE DEVICES WEARABLES CAMERA TECHNOLOGY COMMUNICATION GENERATION BATTERIES FREE CIRCUITS INDUSTRIAL AUTOMATION SPECIAL MACHINES ELECTRICAL SAFETY ENERGY EFFIDIENCY-BUILDING DRONE CONTROL SYSTEM NUCLEAR ENERGY SMATRPHONE FILTER`S POWER BIOGAS BELT CONVEYOR MATERIAL HANDLING RELAY ELECTRICAL INSTRUMENTS ENERGY SOURCE 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 CELL CHEMISTRY EARTHING SYSTEM ELECTRIC LAMP FUNDAMENTAL OF ELECTRICITY 2 BIPOLAR JUNCTION TRANSISTOR 555 TIMER CIRCUITS AUTOCAD BLUETOOTH C PROGRAMMING HOME AUTOMATION HYDRO POWER LOGIC GATES OPERATIONAL AMPLIFIER`S SOLID-STATE DEVICE THEORRY COMPUTER DEFECE & MILITARY FLUORESCENT LAMP INDUSTRIAL ROBOTICS ANDROID ELECTRICAL DRIVES GROUNDING SYSTEM CALCULUS REFERENCE DC METERING CIRCUITS DC NETWORK ANALYSIS ELECTRICAL SAFETY TIPS ELECTRICIAN SCHOOL ELECTRON TUBES FUNDAMENTAL OF ELECTRICITY 1 INDUCTION MACHINES INSULATIONS USB 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 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 CONTROL MOTION ELECTRICAL LAWS 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 APPS & SOFTWARE BASIC CONCEPTS OF ELECTRICITY CONDUCTOR AND INSULATORS TABLES CONDUITS FITTING AND SUPPORTS 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 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 RECTIFIER AND CONVERTERS RESONANCE SCIENTIFIC NOTATION AND METRIC PREFIXES SULFURIC ACID TROUBLESHOOTING TROUBLESHOOTING-THEORY & PRACTICE 12C BUS APPLE BATTERIES AND POWER SYSTEMS DC MOTOR DRIVES ELECTROMECHANICAL RELAYS ENERGY EFFICIENCY-LIGHT INDUSTRIAL SAFETY EQUIPMENTS MEGGER MXED-FREQUENCY AC SIGNALS PRINCIPLE OF DIGITAL COMPUTING QUESTIONS REACTANCE AND IMPEDANCE-CAPATIVE SEQUENTIAL CIRCUITS SERRIES-PARALLEL COMBINATION CIRCUITS SHIFT REGISTERS WIRELESS BUILDING SERVICES COMPRESSOR CRANES DIVIDER CIRCUIT AND KIRCHHOFF`S LAW ELECTRICAL DISTRIBUTION EQUIPMENTS 1 ELECTRICAL DISTRIBUTION EQUIPMENTS B ELECTRICAL TOOL KIT ELECTRICIAN JOB DESCRIPTION INDUSTRIAL DRIVES LAPTOP SCIENCE THERMOCOUPLE TRIGONOMENTRY REFERENCE UART oscilloscope BIOMASS CONTACTOR ELECTRIC ILLUMINATION ELECTRICAL SAFETY TRAINING ELECTROMECHANICAL FEATURED FILTER DESIGN HARDWARE JUNCTION FIELD-EFFECT TRANSISTORS NASA NUCLEAR POWER VALVE COLOR CODES ELECTRIC TRACTION FLEXIBLE ELECTRONICS FLUKE GEARMOTORS INTRODUCTION LASSER 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