Writing one's own library

Dear Members

Since Lockdown I set myself the task of writing a Library for my Arduino sketch which in itself works well
I've tried and tried by example but without success after 4 weeks of study to solve the problem.
I have completed a sketch in IDE 1.8.12 to include class definitions etc etc to be the contents of my .h file and appropriate code for my .cpp file followed by the operating sketch taken from my original code.
I am so depressed now no amount of research by comparison with known works have given me an answer.

Is it best if I upload my complete code for your critical examination by you experts.

One big disadvantage of mine may be that I am 80 years OLD!!!
Only 8 more week in isolation to go, ain't that good, I need cheering up big time.

You have not told what you have tried from application point of view. You can try a simple application like:

Develop a Class/Library based sketch to blink L (built-in LED of UNO) at 2-sec interval.

#include<MyCustomClass.h>

MyCustomClass myClass(13);     //DPin-13 is connected with L

void setup()
{
    myClass.classSetup();         //pin direction
}

void loop()
{
   myClass.LedOn();
   delay(2000);
   myClass.LedOff();
   delay(2000);
}

Write codes for the contents of MyCustomClass.h and MyCustomClass.cpp files.

Thanks for your early reply. I have done the blink one and the morse work fine for me, but stuck on my more complicated sketch. Do you have time to look at the code?
Mike

I suggest you start to analyze how a (any) libary is written which you are commonly using.

LiquidCristal, Servo, ... it doesn't matter which one, just that you get an idea how others are doing that.

Then, there are these 4 topics which might be of interest for your:

https://www.arduino.cc/en/Hacking/libraryTutorial
https://www.arduino.cc/en/Reference/APIStyleGuide
https://www.arduino.cc/en/hacking/libraries

and than there is one more explaining the file/folder structure, I can't find currently...

med1977:
Thanks for your early reply. I have done the blink one and the morse work fine for me, but stuck on my more complicated sketch. Do you have time to look at the code?
Mike

Can you please your L blinking code?

Hi Do you mead upload by library/sketch?

med1977:
Hi Do you mead upload by library/sketch?

Post your sketch here with code tags (</>) so that I can upload it in my UNO and check that the L blinks at 2-sec interval.

In the 4 week period I have analyzed several working sketches which use libraries .h and .cpp but have not manage to get mine to compile even after very careful comparisons have been made.

what's your final target?
Do you really want to write a library, even for distribution for everyone's useage, or do you just want to separate some parts of your code (i.e. classes) in .cpp an .h files within your sketch?

If you posted code and error messages, we could point you in the right direction and point out the common pitfalls

In the 4 week period I have analyzed several working sketches which use libraries .h and .cpp but have not manage to get mine to compile even after very careful comparisons have been made.

Please, post the codes of at least one simple working sketch that you have explored.

HOPING to make things clearer :-

Having written a now well tried sketch with 5 variables tested and compiled correctly, after putting these variables in a .h file my sketch will not compile, it doesn’t find them. Error message is ‘not declared in this scope’ The only fix I can find is to put the same 5 variables, redefined, just before setup(). My question is why can it not find them from inside .h ? Snippit below

// unsigned long duration; int mSecs; etc etc
// sketch works with these defined before setup()

unsigned long duration; int mSecs; int msecs;
boolean valid; int loopcount;

void setup() { Serial.begin(9600); }

void loop() {
duration = pulseIn(2, HIGH);
Serial.print(" microSecs = " ); Serial.println(duration);
mSecs = duration/1000;
Serial.print(" milliSecs = " ); Serial.println(mSecs);
}

For goodness sake, just post your library files and a test sketch that uses them and be done with it.

…and please use code tags.

med1977:
HOPING to make things clearer :-

I wish to see your library as zip file which I will include in my IDE and then I will declare the *.h file in my sketch. Please, give a link (or attach) to your zip which I will explore to see what methods/functions it contains.

Attached my zip file including a folder with sketch in it.

DoorBellZip.zip (1.73 KB)

put all 3 files in the same folder

replace #include "DoorBell.h" in your .ino file
add a #include "DoorBell.h" in your .cpp file.
press CTRL-T in all three codes

Check your .ino sketch

DoorBell28-03:15:3: error: 'duration' was not declared in this scope

   duration = pulseIn(2, HIGH);

which is pretty clear, as duration was not declared.

P.S.: don't do a pinMode in the constructor. Use a dedicated .begin() method!

Why didn’t you just POST the damn code?

                                                       // DoorBell28-01 17 Apr 2020  new library attempt
// print this shit out when finished

//#ifndef DoorBell.h
//#define DoorBell.h  // put#endif at far end of .h eventually
#include <Arduino.h>  // Gives access to all arduino IDE fns etc etc

class DoorBell  {  // class where all variables are declared
public:            // public contents accessible by sketch.ino
                   // The 1st item in the class is called the constructor
DoorBell(int inpin, int outpin);   // only two pins in this instance
                  // has no return type and used only once
 unsigned long duration;  int mSecs; int msecs;
 boolean valid; int loopcount;
                  // now define the function(s) for user to call
                  // from Arduino IDE such as digtalWrite etc etc

                  void bell();
                  
private:  // private variable use, place an _ before variable
       int _inpin; int _outpin;   //  and finally put } and ;      
 
};
                //  Now follows DoorBell.cpp code
                  //  And a contructor in this form
                  
 DoorBell::DoorBell(int inpin, int outpin){
  // note double colon says DoorBell:: is part of DoorBell class
   pinMode(_inpin, INPUT);  _inpin = inpin;
   pinMode(_outpin, OUTPUT); _outpin = outpin;
   }  
   // end of DoorBell.cpp file
#include <DoorBell.h>
#include <Arduino.h>

  // unsigned long duration; int mSecs; int loopcount; etc etc 
  // works with these defined before setup()




   void setup() { Serial.begin(9600);  }  

   void loop()  {  duration = pulseIn(2, HIGH);
   Serial.print(" microSecs = " );  Serial.println(duration);
   mSecs = duration/1000;   
   Serial.print(" milliSecs = " );  Serial.println(mSecs);


    }
DoorBell::DoorBell(int inpin, int outpin){
  // note double colon says DoorBell:: is part of DoorBell class
   pinMode(_inpin, INPUT);  _inpin = inpin;
   pinMode(_outpin, OUTPUT); _outpin = outpin;
   }

Not sensible to set the pinMode of an undefined variable.

When you were researching other code,
did you notice how they formatted their code?

GolamMostafa:
I wish to see your library as zip file which I will include in my IDE and then I will declare the *.h file in my sketch. Please, give a link (or attach) to your zip which I will explore to see what methods/functions it contains.

Hey Golam Have you received my zip file and sketch sent yesterday. Thank you for offering to help. Michael