Hello there. First post here in the forum. I have been looking for quite long for a solution to my problem but I didn't find anything close to my case so I decided to register.
I'm new to C++ language so I'm not quite sure about what can be done inside classes.
I wanted to pack all my hardware interface functions (potensiometers, buttons and leds) from a project I'm working on inside a class in order to get things less complicated. In the original project I'm using the bounce library for debouncing the buttons and I wanted to hide the bounce object inside my class. So the basic concept was that in the constructor I declare the pin I want to use and then if it is potentiometer, button or led. If it is a button I wanted to automatically create a bounce object one time and use it inside the functions referred to the button. The problem is that I cannot declare the bounce object inside the header because I get the error that I haven't declare in the function scope. The same goes if I declare it inside the constructor. The only way I could use it without compiling errors was to declare it in every function separately but in this case I had to put a delay in order to make it work in the real world.
So the question here is if there is a way to declare the bounce object inside the constructor one time and use it's functions inside my class'es functions.
Here is the code so far.
The header file
#include <Bounce.h>
#ifndef hardinter_h
#define hardinter_h
#include "WProgram.h"
class hardinter : public Bounce
{
public:
hardinter(int pin,char type); //constructor
boolean potactivity(); //potentiometer move detection
int potvalue(); //current value of potentiometer
boolean butstate(); //state of button
unsigned long buttime(); //time the button has been pressed
void ledstate(); //led's state
private:
int _pin; //pin of current object
char _type; //type of current pin (potentiometer, button, led)
int _smooth(); //smoothing potentiometer values
int _lastvalue; //last value of potentiometer
int _crnvalue; //current value of potentiometer
boolean _butstate; //state of button
unsigned long _buttime; //time the button has been pressed
boolean _ledstate; //led's state
int i; //counter for smoothing function
};
#endif
The ccp file
#include "hardinter.h"
#include "WProgram.h"
hardinter::hardinter(int pin, char type) : Bounce (pin, 10){ //constructor and bounce library inherited
_pin=pin; //set pin
_type=type; //set type
if (type=='p'){ //type potensiometer
pinMode (_pin,INPUT); //set as input
_lastvalue=0; //initialise variables
_smooth(); //call smooth function for initialisation
}
if (type=='b'){ //type button
pinMode (_pin,INPUT); //set as input
Bounce _button(_pin,10); //create Bounce object for the button
}
if (type=='l'){ //type led
pinMode (_pin,OUTPUT); //set as output
}
}
int hardinter::_smooth(){ //smooth function for potentiometer readings
for (i=0; i<10; i++){ //count 10 times
_crnvalue+=analogRead(_pin); //add 10 readings to the current value
}
_crnvalue/=10; //after adding divide with 10 to get the average value
}
boolean hardinter::potactivity(){ //function returning true if potensiometer has made any move
if (_type=='p'){ //check if it is a potensiometer
_smooth();
if (_lastvalue!=_crnvalue){ //check if the potensiometer has the same value as the last time
_lastvalue=_crnvalue; //store the new value of the potensiometer
return true;
}
else {
return false;
}
}
}
int hardinter::potvalue(){ //function for returning the potentiometer's current value
if (_type=='p'){ //check if it is a potensiometer
_smooth(); //call smooth function
return _crnvalue; //return current value
}
}
boolean hardinter::butstate() { //function for returning button state
if (_type=='b'){
if (_button.update()){ //check if the bounce object was updated
if (_button.risingEdge()){ //if there was a low to high change
_buttime=millis(); //store the current time for buttime function
return true; //return true
}
else {
return false;
}
}
}
}
unsigned long hardinter::buttime(){
if (_button.update()){ //check if the bounce object was updated
if (_button.fallingEdge()){ //if there was a high to low change
_buttime=millis()-_buttime; //calculate the time the button was in high state
return _buttime; //return the time
}
}
}
To make it more clear in the ccp file. Inside the constructor I declare the bounce object and then try to call it's functions inside butstate and buttime functions.