digitalWrite(), digitalRead() from custom libraries

I have been moving some functionality in my code to an external custom cpp, and h file, these methods need to digital read and write but for some reason this function does not work, in #included <Arduion.h> is there another library which controls the functions of digital read, write?

Sorry for the vague description, but there is no errors, the problem is that the when i use digitalWrite(8, HIGH); from the external class nothing happens, the pin stays in a low state

Did you do pinMode(8, OUTPUT)?

Yes i included pinMode(8, OUTPUT); after a lot of experementing I found the problem I had a function that setup the pinmode and did the digitalWrites, but the pinmode needs to be in the constructor thansk for all the help

but the pinmode needs to be in the constructor

No, it almost certainly does NOT belong there, unless you KNOW that the constructor will only be called after init() has been called. That means that the constructor has to be called in setup() or in loop(). And, that is a strange way to write a class.

You SHOULD have a begin() method that makes the pinMode() calls, and you need to call that method in setup() for any global instances of your class.

PaulS Thanks for the reply.

I had it in a setup method that was callled in the main class's setup but for some reason this did not activate the pinMode(8, OUTPUT); and i was unable to use the digitalWrite(8, LOW/HIGH); this was solved when i moved it to the constructor for my custom class, I then used a variable of type myCustomClass and only call a new myCustom class in the setup which ensures that the constructer will only be called once in the setup() of my main class.

Thank you for the advice.

I never had any problem with code moved to a .h file or a separate .ino file.

@KarelKruger, post the code that is giving the problem (the complete program. please).

...R

KarelKruger:
PaulS Thanks for the reply.

I had it in a setup method that was callled in the main class's setup but for some reason this did not activate the pinMode(8, OUTPUT); and i was unable to use the digitalWrite(8, LOW/HIGH); this was solved when i moved it to the constructor for my custom class, I then used a variable of type myCustomClass and only call a new myCustom class in the setup which ensures that the constructer will only be called once in the setup() of my main class.

Thank you for the advice.

then you re dealing with constantly dereferencing the pointer to the newly created instance.

better to just use a setup() method in your class, inject the hardware stuff there

This was the first time i was writing a custom class for an arduino (and c++ in general) so its more of a test. Apologies for my amateurish code

Main.ino (224 Bytes)

MyCustomClass.cpp (296 Bytes)

MyCustomClass.h (290 Bytes)

Delta_G thanks for the reply.

All the examples of how to create a custom library showd it this way, but I might be looking at the wrong examples.

The function goal is to control a siple rgb (turning reb, ble or green) depending on a secondaty input on pin 6

MyCustomClass.h (622 Bytes)

KarelKruger:
Delta_G thanks for the reply.

All the examples of how to create a custom library showd it this way, but I might be looking at the wrong examples.

The function goal is to control a siple rgb (turning reb, ble or green) depending on a secondaty input on pin 6

debug it in a simple single .ino file... then make it into a header/cpp file

like this:

class MyCustomClass
{
  public:
    MyCustomClass(uint8_t powerPin);
    void PowerOn();
    void classSetup();
  private:
    uint8_t pin;
};

MyCustomClass::MyCustomClass(uint8_t powerPin) : pin(powerPin){
  // nothiing to do here // 
}

void MyCustomClass::classSetup()
{
  pinMode(pin, OUTPUT);
}

void MyCustomClass::PowerOn()
{
  digitalWrite(pin, HIGH);
}

MyCustomClass myClass(8);

void setup()
{
  myClass.classSetup();

  myClass.PowerOn();
}
void loop()
{
  
}

BulldogLowell:
debug it in a simple single .ino file... then make it into a header/cpp file

like this:

class MyCustomClass

{
  public:
    MyCustomClass(uint8_t powerPin);
    void PowerOn();
    void classSetup();
  private:
    uint8_t pin;
};

MyCustomClass::MyCustomClass(uint8_t powerPin) : pin(powerPin){
  // nothiing to do here //
}

void MyCustomClass::classSetup()
{
  pinMode(pin, OUTPUT);
}

void MyCustomClass::PowerOn()
{
  digitalWrite(pin, HIGH);
}

MyCustomClass myClass(8);

void setup()
{
  myClass.classSetup();

myClass.PowerOn();
}
void loop()
{
 
}

I'll ty that thankyou very mutch :slight_smile:

KarelKruger:
I'll ty that thankyou very mutch :slight_smile:

After more than a year, I would like to say on behalf of you that the single sketch of Post#12 works. Now, the task is to put some of the codes into *.h file and *.cpp file; compress these two files into a *.zip file and then execute the *.ino sketch by including the *.zip file. It would be nice to see the steps how to do it along with brief textual description on why this line will (should) go under *.h file and that line will (should) go under *.cpp file.

Sketch of Post#12 (with slight modification to blink L at 1 sec interval; tested on MEGA)

class MyCustomClass
{
  private:
    byte pin;
  public:
    MyCustomClass(byte powerpin):pin(powerpin){}
    void classSetup();
    void PowerOn();
    void PowerDown();
};

void MyCustomClass::classSetup()
{
  pinMode(pin, OUTPUT);      //direction set
}

void MyCustomClass::PowerOn()
{
  digitalWrite(pin, HIGH);
}

void MyCustomClass::PowerDown()
{
  digitalWrite(pin, LOW);
}

MyCustomClass myClass(13);   //DPin assignment to variable pin

void setup()
{
  myClass.classSetup();
}

void loop()
{
  myClass.PowerOn();   //performs digitalWrite(13, HIGH)
  delay(1000);
  myClass.PowerDown();  //performs digitalWrite(13, LOW)
  delay(1000);
}

@GolamMostafa

What does "execute ino by including zip" mean?

And do you really have to revive an old thread for this? You could have started your own one, referring to this thread if needed (although I don't seem to see the need).

This is a dead Thread. Let it rest in peace.

...R

Robin2:
This is a dead Thread. Let it rest in peace.

But, the content is still alive. Some of the Forum members might be interested to know the need/reason of using Class Data Structure to blink a LED which could just be done in a simple way. There could be no reason behind it; but, it could be a simple exercise that things could be done in different ways and what are those ways -- how to make the *.h file, *.cpp file, *.zip file and then running the application having included the *.zip file in the Arduino IDE.

sterretje:
What does "execute ino by including zip" mean?

1. There is the following sketch (borrowed from Post#12 of this thread) which blinks L of Arduino using Class Structure.

class MyCustomClass
{
  private:
    byte pin;
  public:
    MyCustomClass(byte powerpin):pin(powerpin){}
    void classSetup();
    void PowerOn();
    void PowerDown();
};

void MyCustomClass::classSetup()
{
  pinMode(pin, OUTPUT);      //direction set
}

void MyCustomClass::PowerOn()
{
  digitalWrite(pin, HIGH);
}

void MyCustomClass::PowerDown()
{
  digitalWrite(pin, LOW);
}

MyCustomClass myClass(13);   //DPin assignment to variable pin

void setup()
{
  myClass.classSetup();
}

void loop()
{
  myClass.PowerOn();   //performs digitalWrite(13, HIGH)
  delay(1000);
  myClass.PowerDown();  //performs digitalWrite(13, LOW)
  delay(1000);
}

2. I/OP wish to create a file named MyCustomClass.h. Which code lines of Step-1 should be moved into this header file and why?

3. I/OP wish to create a file named MyCustomClass.cpp. Which code lines of Step-1 should be moved into this cpp file and why?

4. I/OP wish to make a MyCustomClass.zip file out of *.h and *cpp files; this can be done by I/OP using File Explorer tool of Windows 10.

5. After making *.h and *.cpp files, there will be remaining some codes in Step-1 which is the 'application program/ino file'. What will be the structure of this *.ino file?

6. Using Include Library tool of the Arduino IDE, the MyCustomClass.zip file will be included in the IDE.

7. I/OP will be executing the application program of Step-5, the L should start blinking.

Note: Some of the Forum members are primarily hardware oriented, and they are eager to know a little bit more on the C++ application; this is the spirit that triggered this poster to revive the year old thread.

You could have started your own thread and used this code as an example.

(2) and (3)
I'm not a C++ programmer but to my knowledge the implementation of methods goes in the .cpp and the rest in the in the .h.

(5)
Whatever is leftover (this includes the instantiation of the object(s)) plus an include.

(6)
Explains what I asked; you add a library to the IDE, you do not include a .zip in the .ino (my interpretation of what you said).
If you really want to make a library for publishing, you can zip it; else there is no need. You could have created a subdirectory (name is not critical as far as I know) in the libraries folder and you could have copied the .h and .cpp in there.
Please note that splitting code over several files does not necessary mean that a library is needed.

The following three separate files are created:
1. Application Program (appProg)

#include<MyCustomClass.h>

MyCustomClass myClass(13);   //DPin assignment to variable pin

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

void loop()
{
  myClass.PowerOn();   //performs digitalWrite(13, HIGH)
  delay(1000);
  myClass.PowerDown();  //performs digitalWrite(13, LOW)
  delay(1000);
}

2. The header file: MyCustomClass.h

class MyCustomClass
{
  private:
    int pin;     //byte pin gives error;
  public:
    MyCustomClass(int powerpin):pin(powerpin){}
    void classSetup();
    void PowerOn();
    void PowerDown();
};

3. The cpp file: MyCustomClass.cpp

#include<MyCustomClass.h>
#include<Arduino.h>          //without this file, there are errors

void MyCustomClass::classSetup()
{
  pinMode(pin, OUTPUT);      //direction set
}

void MyCustomClass::PowerOn()
{
  digitalWrite(pin, HIGH);
}

void MyCustomClass::PowerDown()
{
  digitalWrite(pin, LOW);
}

4. I created MyCustomClass.zip file from MyCustomClass.h and MyCustomClass.cpp files and included it in the IDE using Include Library --> Add .ZIP Library.... In my PC, the Library has been automatically included in this folder: C:\users\siam\documents\Arduino\libraries\MyCustomClass.

I like the above method as there are so many folders under which Arduino related libraries are found -- where to put my folder containing *.h and *.cpp files. Let the IDE do it for me.

5. My application program of Step-1 was not compiling initially; error disappeared when I added Arduino.h in the *.cpp file and changed the data type from byte to int in the *.h file. (When I have a single file of Post#18, the program/sketch works fine with byte data type and without explicit inclusion of Arduino.h. Why?)

6. The appProg of Step-1 is working well.

A very big thank and + @sterretje for the guidance. This program will work for me as a great teacher to study the bureaucratic structure/syntax that are found in the *.h and *cpp files that come with sensor/device/Arduino Libraries.