Hi, Im making a Library, that will debounce buttons, and so, I have the code running flawlessly, now the thing is that I want to learn to write Libraries as well, but Im having a bad time with pointers and arrays.
This is my header file:
#ifndef Pulsadores_h
#define Pulsadores_h
#include "Arduino.h"
class Pulsadores
{
public:
Pulsadores(byte,int*);
void HelloWorld(void);
bool Read(long);
private:
byte* _BM; //Button Mode
byte BM;
int* _PA; //Pin Array
byte _NB; //Number of Buttons
long _LT;
long _DT;
};
#endif
And this is my .CPP file
#include "Arduino.h"
#include "Pulsadores.h"
Pulsadores::Pulsadores(byte NB, int *PinArray)
{
int i = -1;
_NB = NB;
_PA = PinArray;
_DT = 25000;
for(i=-1;i<_NB;i++){
*(_BM + i) = 0;
}
}
void Pulsadores::HelloWorld(void)
{
int i = 0;
Serial.println("Hello World");
for(i=0;i<_NB;i++){
Serial.print("Button ");
Serial.print(i+1);
Serial.print(", Pin: ");
Serial.print(*(_PA+i));
Serial.print(" Mode: ");
Serial.print(*(_BM+i));
Serial.println(".");
}
}
bool Pulsadores::Read(long CT)
{
if( (CT-_LT) > _DT )
{
Pulsadores::HelloWorld();
delay(100);
_LT = CT;
}
}
And This is My Arduino Code:
#include <Pulsadores.h>
int PinArray[8] = {53, 51, 49, 47, 45, 43, 41, 39};
Pulsadores A = Pulsadores(8,PinArray);
void setup() {
Serial.begin(115200);
}
void loop() {
A.Read(micros());
}
The thing is that when I run it I get this on the serial monitor:
Hello World
Button 1, Pin: 53 Mode: 37.
Button 2, Pin: 51 Mode: 0.
Button 3, Pin: 49 Mode: 0.
Button 4, Pin: 47 Mode: 0.
Button 5, Pin: 45 Mode: 120.
Button 6, Pin: 43 Mode: 98.
Button 7, Pin: 41 Mode: 193.
Button 8, Pin: 39 Mode: 82.
When I should be getting "Mode 0" for all.
I think this is a really nooby question, but i have no clue what I am doing wrong, thanks!