Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
csw1
Published

Arduino Library tutorial

This tutorial will teach you how to make a Arduino zip libary easily for any sensor

IntermediateFull instructions provided1 hour841
Arduino Library tutorial

Things used in this project

Story

Read more

Schematics

index_EFPX9lwfSm.png

Code

pushbutton code

C/C++
#include <button.h> // the library




button button(2,13,0); //creating a  button object(pushbutton,ledpins,state of pushbutton)
 

void setup() {

}

void loop() {
  
 button.work();//the code
}

button.h

C/C++
#ifndef button_h  //ifndef and define make sure that you dont use library twice
#define button_h
#include "Arduino.h"  //Arduino.h means include all the arduino functions such as pinmode//,HIGH,LOW ETC..

class button{    //Making a  class
    public:   // 'public:' and 'private:' refer to the security of the functions and //variables listed in that set. Contents that are public can be 
	                                               //	accessed from a sketch for use, however private contents can only be
	                                               //	accessed from within the class itself.
    button(int pinone,int pintwo,int state);//pinone is button pin and pintwo is ledpin a//nd state is the state ofmthe pusbutton(HIGH or LOW)

    void work();                 //Where the code works

    private:
    int _pinone, _pintwo,_state;

};

#endif  //ending the library

C++ sourcefile.cpp

C/C++
#include "Arduino.h"  //include earduino functions
#include "button.h"  //include the previous file button.h

button::button(int pinone,int pintwo,int state){
    pinMode(pinone, INPUT_PULLUP);//pinmode
	pinMode(pintwo, OUTPUT);

    _pinone = pinone; //making the private variables equal to the public
	_pintwo = pintwo;
	_state = state;
}

void button::work(){                   //the code
     _state = digitalRead(_pinone);
     if (_state == LOW) {
    
    digitalWrite(_pintwo, HIGH);
  } else {
    
    digitalWrite(_pintwo, LOW);
  }

}

keywords.txt

C/C++
button	KEYWORD1
work	KEYWORD2

//keywords are used to highlight thewords in the arduino sketch
//KEYWORD1 are classes wchich are highlighted in red
//KEYWORD2 are other important functions 

The pushbutton library codes

Credits

csw1
4 projects • 4 followers
Contact

Comments

Please log in or sign up to comment.