Noob creating a library, very little idea on howto (1st Library)

HI and thanks in advance for Open Source :slight_smile: to everyone who helps to make it better and better to someone like me law student who learns through you.

being that said, straight to issue.

I have a 74HC393, is an integrated of 2 parallel 4digits asynchronus shiftregister, it has 0 to 15 cicles.

i've created some functions (you can see them in the code of "TEST.h"), the logic behind is that one cycle requiers a LOW to HIGH for 6 NanoSeconds according to the datasheet, i've tryed it and in a sketch everything works OK.

my idea is to create a library to do this. And i'am stuck. i'll paste below the .h and then de .cpp.
If you have any suggestion on how to do this in an other way is also welcome.

TEST.h:

#ifndef TEST_H
#define TEST_H

#if defined(ARDUINO) && (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif

class TEST
{
public:
float LATCH(int pin);
float MRESET(int pin);
void TEST0a();
void TEST1a();
void TEST2a();
void TEST3a();
void TEST4a();
void TEST5a();
void TEST6a();
void TEST7a();
void TEST8a();
void TEST9a();
void TEST10a();
void TEST11a();
void TEST12a();
void TEST13a();
void TEST14a();
void TEST15a();
void TESTReset();

private:
int _LATCH;
int _MRESET;
};
#endif

TEST.cpp:

#include "TEST.h"

  TEST::LATCH(int pin)
  {
  pinMode(pin, OUTPUT);
  _LATCH = pin;
  };
  
  TEST::MRESET(int pin)
  {
  pinMode(pin1, OUTPUT);
  _MRESET = pin1;
  };
  
  void TEST::TEST0a()
  {
  digitalWrite(_LATCH, LOW);
  delayMicroseconds(6);
  digitalWrite(_LATCH, HIGH);
  }
  void TEST::TEST1a()
  {
  digitalWrite(_LATCH, LOW);
  delayMicroseconds(6);
  digitalWrite(_LATCH, HIGH);
    delayMicroseconds(6);
  digitalWrite(_LATCH, LOW);
  delayMicroseconds(6);
  digitalWrite(_LATCH, HIGH);
  }
  void TEST::TEST2a()
  {
  digitalWrite(_LATCH, LOW);
  delayMicroseconds(6);
  digitalWrite(_LATCH, HIGH);
    delayMicroseconds(6);
  digitalWrite(_LATCH, LOW);
  delayMicroseconds(6);
  digitalWrite(_LATCH, HIGH);
    delayMicroseconds(6);
  digitalWrite(_LATCH, LOW);
  delayMicroseconds(6);
  digitalWrite(_LATCH, HIGH);
  }
  void TEST::TEST3a()
  {
  digitalWrite(_LATCH, LOW);
  delayMicroseconds(6);
  digitalWrite(_LATCH, HIGH);
    delayMicroseconds(6);
  digitalWrite(_LATCH, LOW);
  delayMicroseconds(6);
  digitalWrite(_LATCH, HIGH);
    delayMicroseconds(6);
  digitalWrite(_LATCH, LOW);
  delayMicroseconds(6);
  digitalWrite(_LATCH, HIGH);
    delayMicroseconds(6);
  digitalWrite(_LATCH, LOW);
  delayMicroseconds(6);
  digitalWrite(_LATCH, HIGH);
  }
  void TEST::TEST4a()
  {
  digitalWrite(_LATCH, LOW);
  delayMicroseconds(6);
  digitalWrite(_LATCH, HIGH);
    delayMicroseconds(6);
  digitalWrite(_LATCH, LOW);
  delayMicroseconds(6);
  digitalWrite(_LATCH, HIGH);
    delayMicroseconds(6);
  digitalWrite(_LATCH, LOW);
  delayMicroseconds(6);
  digitalWrite(_LATCH, HIGH);
    delayMicroseconds(6);
  digitalWrite(_LATCH, LOW);
  delayMicroseconds(6);
  digitalWrite(_LATCH, HIGH);
    delayMicroseconds(6);
  digitalWrite(_LATCH, LOW);
  delayMicroseconds(6);
  digitalWrite(_LATCH, HIGH);
  }
  void TEST::TEST5a()
  {
  digitalWrite(_LATCH, LOW);
  delayMicroseconds(6);
  digitalWrite(_LATCH, HIGH);
    delayMicroseconds(6);
  digitalWrite(_LATCH, LOW);
  delayMicroseconds(6);
  digitalWrite(_LATCH, HIGH);
    delayMicroseconds(6);
  digitalWrite(_LATCH, LOW);
  delayMicroseconds(6);
  digitalWrite(_LATCH, HIGH);
    delayMicroseconds(6);
  digitalWrite(_LATCH, LOW);
  delayMicroseconds(6);
  digitalWrite(_LATCH, HIGH);
    delayMicroseconds(6);
  digitalWrite(_LATCH, LOW);
  delayMicroseconds(6);
  digitalWrite(_LATCH, HIGH);
    delayMicroseconds(6);
  digitalWrite(_LATCH, LOW);
  delayMicroseconds(6);
  digitalWrite(_LATCH, HIGH);
  }

//And so on until sequence 15

  void TEST::TESTReset()
  {
  digitalWrite(MRESET, HIGH);
  delayMicroseconds(50);
  digitalWrite(MRESET, LOW);
  }

THANKS in advanced. :slight_smile:

  TEST::MRESET(int pin)
  {
  pinMode(pin1, OUTPUT);
  _MRESET = pin1;
  };

Does this even compile? The argument is pin. The variable in the code is pin1.

By convention, all upper case names are constants. Method names are NOT constants.

What seems to be the problem?

moderator: removed your cross post - it even contained a wrong link

  1. naming a library TEST is a bad idea, you better name it to the hardware involved
  2. naming of the methods of the class should describe the function
    so - latch() and reset() are quite good
  3. upper case names are typically used for #define (compile time defined values)
  4. indentation makes code more readable
  5. add a constructor to remember pin
  6. test code should imho not in the library itself.

This is how I would have setup the library, have a look at it.

74HC393.h

#ifndef 74HC393_H
#define 74HC393_H

#if defined (ARDUINO) && (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif

class 74HC393
{
    74HC393(uint8_t latchPin, uint8_t resetPin);
public:
    void latch(uint8_t  count);
    void reset();

private:
    int _latch;
    int _reset;
};
#endif

74HC393.cpp

#include "74HC393.h"

74HC393::74HC393(uint8_t latchPin, uint8_t resetPin)
{
    pinMode(latchPin, OUTPUT);
    pinMode(resetPin, OUTPUT);
    _latch = latchPin;
    _reset = resetPin;
};


void 74HC393::latch(uint8_t count)
{
    for (uint8_t i=0; i< count; i++)
    {
        digitalWrite(_latch, LOW);
        delayMicroseconds(6);
        digitalWrite(_latch, HIGH);
        delayMicroseconds(6);
    }
}

void 74HC393::reset()
{
    digitalWrite(_reset, HIGH);
    delayMicroseconds(50);
    digitalWrite(_reset, LOW);
}

hope this helps

what also is important is to have some version and metadata in the header.

I use this header and for .cpp file something similar

//
//    FILE: dht.h
//  AUTHOR: Rob Tillaart
// VERSION: 0.1.13
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
//     URL: http://arduino.cc/playground/Main/DHTLib
//
// HISTORY:
// see dht.cpp file
//