in defining a library which identifiers have to be the same which not

Hello,

I have read the creating library-tutorial which uses morse-code as the example (beeping S-O-S)
inside the example a lot of different things in different places all were named "morse".

Does this mean that these names have to be the same or is it just a c-programmers habit to name it this way?

I know code should be written in brackets but the colors seem not to work inside code-blocks
header-file

class Morse
{
public:
Morse(int pin);
void dot();
void dash();
private:
int _pin;
};

Does the red word "Morse" have to be the same as the green word "Morse"?

cpp-file

If the words don't have to be the same which one th red or the green one is related to the "Morse"-word in the cpp-file?

Morse::Morse(int pin)
{
pinMode(pin, OUTPUT);
_pin = pin;
}

void Morse::dot()
{
digitalWrite(_pin, HIGH);
delay(250);
digitalWrite(_pin, LOW);
delay(250);
}

void Morse::dash()
{
digitalWrite(_pin, HIGH);
delay(1000);
digitalWrite(_pin, LOW);
delay(250);
}

If these words can be different I guess easiest way to understand is to re-write this code-example with names that differ. I suggest to add a prefix that says something about what the word means at this place. Example className_Morse

best regards

Stefan

There's a nice C++ tutorial that is helpful in learning this stuff:
http://www.cplusplus.com/doc/tutorial/classes/#constructors

This constructor function is declared just like a regular member function, but with a name that matches the class name and without any return type; not even void.

So yes, the constructor and the class must be named the same.

Hello pert,

thank you for answering so fast. I started reading the classes -tutorial but it is "fullblown all inclusive".
which again confuses me. I'm not a newbee to programming in general. I have coded tenthousands of lines in delphi. But I'm a newbee about c++. As a newbee I'm not familiar to the technical terms. There are quite a lot of technical terms in this tutorial that are just used without explaining them really.

So does anybody know about another tutorial that takes more time (and words) to explain ALL technical terms?

best regards

Stefan

In the meantime I have found a very basic tutorial that shows an example that could be described as

simply deposit a part of your code in another file to keep your main-file smaller.
It does not use anything like constructors or objects.

just wanted to share the link. The name of the website is program

https://www.brainy-bits.com/arduino-libraries-tutorial/

explain it in small and easy to understand pieces.

Anyway I'm still interested in the object-and-constructor-thing

best regards

Stefan

For me, the Morse library tutorial combined with the cplusplus tutorial was a very good combination. I had no prior experience with C++, and much less experience with programming in general than you have.

I do recommend starting from the beginning of the tutorial though, rather than jumping in at the classes section. When you hit a word you don't understand, just hop on Google to find an explanation.

The tricky thing is that Arduino's documentation only covers the Arduino-specific API and the very basics of C++. You can use the whole C++ language when writing your library, but Arduino doesn't provide that documentation. There are many excellent resources for learning C++, but they are not specific to embedded applications. Some of the information you find in a general C++ tutorial doesn't really apply to an embedded system. So you need to have some intuition as to which information to filter out, and which to pay attention to. Somehow, I didn't find that to be very difficult.

My approach was to read through the tutorial, but skip over the parts that didn't seem immediately useful to me. Later, when I had the need for an additional concept, I remembered that section I had skipped over in the tutorial and went back to read it.

I'm certainly no expert in the subject, but this information has allowed me to write a handful of my own libraries, and understand most of the code I find in other people's libraries.

StefanL38:
In the meantime I have found a very basic tutorial that shows an example that could be described as

simply deposit a part of your code in another file to keep your main-file smaller.
It does not use anything like constructors or objects.

just wanted to share the link. The name of the website is program

https://www.brainy-bits.com/arduino-libraries-tutorial/

explain it in small and easy to understand pieces.

It doesn't seem very useful. It only gives a short explanation of what a library is, how to install it, and how to add an #include directive to your sketch. They promise a future tutorial on how to write a library, but I couldn't find it on the website.

the first mentioned tutorial does the most simple version of an include-file
like I described it:

place some (standard)-code in another file. It is like hide away some columms in an excel-sheet (because they contain the standard-stuff that is not interesting)

I have found another tutorial which explains the term class

This helped my to understand it very quickly. Though I would rename some of the things in the code to make it easier to distinguish between them.

So I re-wrote the code with some changes to the names

header-file "myLED_class.h"

#ifndef LED_H
#define LED_H

#include <Arduino.h>

class Led_class {
  
  private:
    byte pin;
    
  public:
    // Setup pin to which the LED shall be connected and call init()
    Led_class(byte pin); // parameter contains the number of the pin

    void init(); // here in the header-file *.h only the names of the methods are mentioned 
    
    // Power on the LED
    void on();

    // Power off the LED
    void off();
};

#endif

cpp-file "myLED_class.cpp"

#include "myLED_class.h"

Led_class::Led_class(byte pin) {
  this->pin = pin;
  init();
}

// 
void Led_class::init() {
  pinMode(pin, OUTPUT);
  off();
}

void Led_class::on() {
  digitalWrite(pin, HIGH);
}

void Led_class::off() {
  digitalWrite(pin, LOW);
}

Demo-Code that makes use of the LED_class

/*
 * This examples shows how to blink a LED
 * using the Led class from the Led library
 */
#include "myLED_class.h"
// Use the built-in LED
#define LED_PIN 13

// Create a Led object
// This will set the pin to OUTPUT
Led_class MyLED_object(LED_PIN);

void setup() { }

void loop() {
    // Power on the LED
    MyLED_object.on();
    delay(1000);
    // Power off the LED
    MyLED_object.off();
    delay(1000);
}

best regards

Stefan