├── LibraryExample1 └── LibraryExample1.ino ├── README.md └── TestLibrary ├── TestLibrary.cpp ├── TestLibrary.h └── keywords.txt /LibraryExample1/LibraryExample1.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | TestLib testlib(true); 4 | 5 | void setup() { 6 | // put your setup code here, to run once: 7 | 8 | testlib.begin(9600); 9 | randomSeed(analogRead(A0)); 10 | 11 | } 12 | 13 | void loop() { 14 | // put your main code here, to run repeatedly: 15 | long rndNo = testlib.getRandomNumber(); 16 | 17 | // This cannot be done - it is PRIVATE 18 | //float test = testlib.getPi(); 19 | 20 | Serial.println(rndNo); 21 | 22 | delay(2000); 23 | } 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ArduinoLibrary 2 | How to create an Arduino Library (see YouTube video #71 here: https://youtube.com/c/RalphBacon) 3 | 4 | Here you will find the files used in the demonstration video above. 5 | 6 | The video goes through all steps that the beginner will need when creating an Arduino Library. It's a painless, easy to do exercise that will free up your sketch for the more pertinent areas of code as you move mature code into the library. 7 | 8 | Sample demo library code in GitHub 9 | 10 | Remember to put the library files (.cpp, .h and keywords.txt) into a folder 11 | called "TestLibrary" in the libraries folder in your Arduino sketch folder. 12 | Then put the sketch file (.ino) in a folder in your normal Arduino sketch folder. 13 | (This is how they are already organised in the GitHub repository/zip file) 14 | 15 | Just click the download button on this GitHub page to get a zipped file of the above. 16 | 17 | Links to useful sites: 18 | 19 | All about Arduino IDE keywords 20 | https://spencer.bliven.us/index.php/2012/01/18/arduino-ide-keywords/ 21 | 22 | The Arduino Library specification 23 | https://github.com/arduino/Arduino/wiki/Arduino-IDE-1.5:-Library-specification 24 | -------------------------------------------------------------------------------- /TestLibrary/TestLibrary.cpp: -------------------------------------------------------------------------------- 1 | #include "TestLibrary.h" 2 | 3 | TestLib::TestLib(bool displayMsg) { 4 | // Anything you need when instantiating your object goes here 5 | _msg = displayMsg; 6 | } 7 | 8 | // this is our 'begin' function 9 | void TestLib::begin(int baudRate) { 10 | Serial.begin(baudRate); 11 | if (_msg) { 12 | Serial.println("TestLib constructor instantiated (created) successfully."); 13 | } 14 | } 15 | 16 | // Pretend this is one or more complex and involved functions you have written 17 | long TestLib::getRandomNumber() { 18 | 19 | unsigned long specialNumber = random(5, 1000); 20 | 21 | specialNumber *= getPi(); 22 | 23 | specialNumber -= 5; 24 | 25 | return specialNumber; 26 | 27 | } 28 | 29 | // Private method for this class 30 | float TestLib::getPi() { 31 | return 3.1415926; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /TestLibrary/TestLibrary.h: -------------------------------------------------------------------------------- 1 | #ifndef tl 2 | #define tl 3 | 4 | #if (ARDUINO >=100) 5 | #include "Arduino.h" 6 | #else 7 | #include "WProgram.h" 8 | #endif 9 | 10 | class TestLib { 11 | public: 12 | // Constructor 13 | TestLib(bool displayMsg=false); 14 | 15 | // Methods 16 | void begin(int baudRate=9600); 17 | long getRandomNumber(); 18 | 19 | private: 20 | bool _msg; 21 | float getPi(); 22 | }; 23 | #endif 24 | -------------------------------------------------------------------------------- /TestLibrary/keywords.txt: -------------------------------------------------------------------------------- 1 | # ----------------------------------------- 2 | # Syntax colouring for RsbLib demo library 3 | # ----------------------------------------- 4 | 5 | # Datatypes (such as objects) 6 | TestLib KEYWORD1 7 | 8 | #Methods / functions 9 | begin KEYWORD2 10 | getRandomNumber KEYWORD2 11 | 12 | # Constants 13 | 14 | 15 | --------------------------------------------------------------------------------