How to Read Functions of an Arduino Library

In this tutorial I'll show you lot how to create your own Arduino library.

You lot'll see, this might be much simpler than y'all thought! Very simply put, an Arduino library is a bunch of Cpp files that you take to organize in a certain way. So you import it, use it, and that'south it.

Creating an Arduino library is a nifty way to write reusable lawmaking for your projects, and also to help other Arduino developers in their own projects.

Let's get started!

Step ane: Write your Arduino library code

Starting code

Let's get-go with a very elementary plan, and from that create a library step past footstep.

First, write this minor Arduino sketch, which only has ane function. The function volition take two integers as parameters and return the sum.


You are learning how to employ Arduino to build your ain projects?

Check out Arduino For Beginners and learn step past step.


int addTwoInts(int a, int b) {   return a + b; }  void setup()  {   Serial.begin(9600);   int upshot = addTwoInts(4,3);   Serial.println(effect); }  void loop() {}        

Hither nosotros'll make the addTwoInts() function a part of a library named my_library.

For now, all the code is in the aforementioned file, which isn't really practical for separating the library from the main lawmaking.

Separating the library code from the rest

Let'due south create 2 files: a Cpp and a header file.

To do that, detect where your Arduino sketch is located. Usually information technology'due south within the "My Documents/Arduino" folder, unless you inverse the default location. Also, you can find where is your current sketch file directly from the Arduino IDE: click on "Sketch" > "Show Sketch Binder".

Arduino IDE - Show Sketch Folder

In this folder, y'all should already accept a .ino file. If you named your project MyProgram, then the file is called MyProgram.ino, and is located inside the MyProgram/ folder.

Create 2 new files: my_library.h and my_library.cpp. So restart your Arduino IDE, or close/open your current sketch. You should now see 3 files.

Arduino Separate Header and Cpp files

Write the post-obit lawmaking into those 3 files.

my_library.h

#ifndef MY_LIBRARY_H #define MY_LIBRARY_H  #include <Arduino.h>  int addTwoInts(int a, int b);  

0 Response to "How to Read Functions of an Arduino Library"

Publicar un comentario

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel