I was having trouble understanding the making of libraries, and I was able to make one with the information provided by those who replied down below. If you just want the end result, just go to reply #18 that I made.
The original post is below:
Hi all,
I am currently trying to make a custom library, I have named it Tools. It is meant to contain some simple functions to help make my sketches easier to read. As of now, it has two functions: blink and flash. I kept it short to avoid confusion for now as I am not very good with libraries. I did the Arduino tutorial on libraries and that was ok but to use it, I needed an instance of the class at the top of the sketch, and I do not really want to have an instance of a class. I am trying to make it similar to something like 'pinMode' where I can pass arguments to the library function as the function is called.
What I did:
I used a simple sketch to try out the library below:
#include <Tools.h>
void setup() {
blink(13, 500); //(Pin, Duration)
flash(13, 500, 10); //(Pin, Duariton, Count)
}
void loop() {}
What I got:
The above code I used gave me these error messages
Arduino: 1.6.9 (Windows 10), Board: "Arduino/Genuino Uno"
In file included from C:\Users\*****\Desktop\Library_Test\Library_Test.ino:1:0:
C:\Users\*****\Documents\Arduino\libraries\Tools/Tools.h:16:1: error: 'Class' does not name a type
Class Tools
^
C:\Users\*****\Desktop\Library_Test\Library_Test.ino: In function 'void setup()':
Library_Test:4: error: 'blink' was not declared in this scope
blink(13, 500); //(Pin, Duration)
^
Library_Test:5: error: 'flash' was not declared in this scope
flash(13, 500, 10); //(Pin, Duariton, Count)
^
exit status 1
'blink' was not declared in this scope
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
What I have:
Tools.h
/*
First library, a simple collection of tools, but for the sake of simplicity,
there are only a few functions as of now
Created July 4, 2016
*/
#ifndef Tools_h
#define Tools_h
#include "Arduino.h"
Class Tools
{
public:
void blink(byte pin, int duration);
void flash(byte pin, int duration, int counts);
private:
byte _pin;
byte _duration;
byte _counts;
}
#endif
Tools.cpp
#include "Arduino.h"
#include "Tools.h"
void Tools::blink(byte pin, int duration)
{
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH);
delay(duration / 2.0);
digitalWrite(pin, LOW);
delay(duration / 2.0);
}
void Tools::flash(byte pin, int duration, int counts)
{
pinMode(pin, OUTPUT);
for (int x = 0; x < counts; x++)
{
digitalWrite(pin, HIGH);
delay(duration / 2.0);
digitalWrite(pin, LOW);
delay(duration / 2.0);
}
}
Any help is appreciated.