Creating my own library

Hi everyone;
First of all sorry about my English.

So, I’m trying to develop a library which will check if a push-button was pressed or not. But unfortunately it doesn’t work properly.
Below follow the code:

Headers File

/*
	botoes.h
	Biblioteca para controles dos botoes no projeto da "Casa Inteligente"
	Por: João Pedro Rodrigues, Janeiro, 2014
*/

#ifndef botoes_h
#define botoes_h

#include "Arduino.h"

class botoes
{
	public:
		botao();
		bool clicou(int b[int x]);
	private:
		int _b;
};

#endif

Cpp file

#include "Arduino.h"
#include "botoes.h"

botoes::botoes()
{
	const int b0=50, b1=49, b2=48, b3=47, b4=46, b5=45, b6=44;
	int botao[7]= {0,0,0,0,0,0,0};
	
	pinMode(b[0], INPUT);
	pinMode(b[1], INPUT);
	pinMode(b[2], INPUT);
	pinMode(b[3], INPUT);
	pinMode(b[4], INPUT);
	pinMode(b[5], INPUT);
	pinMode(b[6], INPUT);
	
	botao[0] = digitalRead(b0);
	botao[1] = digitalRead(b1);
	botao[2] = digitalRead(b2);
	botao[3] = digitalRead(b3);
	botao[4] = digitalRead(b4);
	botao[5] = digitalRead(b5);
	botao[6] = digitalRead(b6);
	
}

bool botoes::clicou(int b[int x])
{
	int UltEstado[7] = {0,0,0,0,0,0,0};
	
	if(botao[x]!= UltEstado[x])
	{
		if(botao[x]== HIGH)
		{
			delay(50);
			return true;
		}
	}
	return false;
}

Arduino’s code

#include <botoes.h>

botao botao();

void setup()
{
  Serial.begin(9600);
}

void loop()
{
   if (clicou(b[0])
   {
    Serial.print ("Clicou"); 
   }
}

Thanks a lot for any help :slight_smile:

This

b0=50, b1=49, b2=48, b3=47, b4=46, b5=45, b6=44;

And this,

pinMode(b[0], INPUT); pinMode(b[1], INPUT); pinMode(b[2], INPUT); pinMode(b[3], INPUT); pinMode(b[4], INPUT); pinMode(b[5], INPUT); pinMode(b[6], INPUT);

Are not the same. You want byte b[7] = { /*button pins here */}, use byte instead of int, byte saves memory.

Why use UltEstado[x] if your not going to update it? All you need is if(botao[x]). Another thing you should look into is Button Debouncing. There is an example sketch given to you in the Arduino software, learn how it works.

There is more, but well get to that when you take care of everything above. You can also condense all you pinModes into a FOR loop

Hi HazardsMind .
Thanks for your help.

Here the changes I’ve made:
Cpp file

#include "Arduino.h"
#include "botoes.h"

botoes::botoes()
{
	byte b[0]=50, b[1]=49, b[2]=48, b[3]=47, b[4]=46, b[5]=45, b[6]=44;
	int botao[7]= {0,0,0,0,0,0,0};
	
	for (int i=0; i<=7; i++)
	{
	pinMode(b[i], INPUT);
	botao[i] = digitalRead(b[i]);
	}
	
}

bool botoes::clicou(int b[int x])
{
	int UltEstado[7] = {0,0,0,0,0,0,0};
	
	if(botao[x]!= UltEstado[x])
	{
		if(botao[x]== HIGH)
		{
			delay(50);
			return true;
		}
	}
	return false;
}

Why use UltEstado
if your not going to update it?

I’m using it, because I intend to implement a counter in the future…

I’ve tried to compile the Arduino code but a error appears saying " ‘botao’ does not name a type"

Im not surprised.

botao(); // .h => should be botoes to match your CLASS and what is written in your .CPP file botoes::botoes() // .cpp botao botao(); // sketch

Again, Not how to write it.

byte b[0]=50, b[1]=49, b[2]=48, b[3]=47, b[4]=46, b[5]=45, b[6]=44;

You want this. byte b[7] = { 50,49,48,47,46,45,44};

if (clicou(b[0])

this here needs another closing parenthesis.

Sketch:

include " botoes.h"

botoes myToes() // or something . . . if ( myToes.clicou( 0 ) ) // 0 = button on pin 50 . . .

I decided to do some of the work for you, just so you can compare yours to mine.

New .h file:

//botoes.h

/*
	botoes.h
	Biblioteca para controles dos botoes no projeto da "Casa Inteligente"
	Por: João Pedro Rodrigues, Janeiro, 2014
	
	Edited by HazardsMind
*/

#ifndef botoes_h
#define botoes_h

#include "Arduino.h"
#define MaxButtons 10

class botoes
{
	public:  
           // constructor
		botoes(byte *buttonPins, byte Num_of_buttons);

          // method
		bool clicou(int x);
	private:
		int _b;
		int botao[MaxButtons];
};

#endif

New .cpp file:

// botoes.cpp

#include "Arduino.h"
#include "botoes.h"

botoes::botoes(byte *buttonPins, byte Num_of_buttons)
{	
  for (int i = 0; i < Num_of_buttons; i++)
  {
    pinMode(buttonPins[ i ], INPUT);
    botao[ i ] = buttonPins[ i ];
  }	
}

bool botoes::clicou(int x)
{
	int UltEstado[7] = {0,0,0,0,0,0,0};
	if(digitalRead(botao[ x ])!= UltEstado[x])
	{
		if(digitalRead(botao[ x ])== HIGH)
		{
			delay(50);
			return true;
		}
	}
	return false;
}

New Sketch:

#include <botoes.h>

byte myButtons[2] = {2, 3}; // you can set what pins you want to use as button, just make sure you tell it how many buttons you are using. In this case, I said pins 2 and 3, and i'm using 2 buttons total

botoes myToes(myButtons, 2); // Button pins and number of total buttons

void setup()
{
  Serial.begin(9600);
}

void loop()
{
   if (myToes.clicou(0))  // 0 = button on pin 2,    1 = button on pin 3
   {
    Serial.println ("Clicou"); 
   }
}