Hello. This code is for UNO.
I made this simple class to detect click and double clicks, it use attachInterrupt(). This is for my 3D table. One click command = 0|1, double click command = 1|2. I want the attachInterrupt() which is in setup to be initialized in class constructor.But it not allow me due attachInterrupt() need static function which can't call non-static function in object. For the moment I let attachInterrupt() out of class, and it works fine. Any idea how to put the attachInterrupt() in a class ?
And any mistake you see in code, pls correct me. ![]()
this is the code:
class Button {
private:
int BUTTON_PIN;
bool debug; //Enable logs
bool pushTriger;
unsigned long time_pushed; //Time when the button pushed
int detection_click_time; //mseconds. During that period of time check double click
int delay_time_click_not_work; // mseconds. When you push button,the volt can tremble and see it like second click. After click, during that period of time ignore clicks.
int click_counter;
//bool buttonStatus=0;
int buff; //The time passed from the last batton push buff=millis()-time_pushed
int command=0;
int last_command_turn=1;
public:
int button=0;
Button(){
BUTTON_PIN=2;
//BUTTON_PIN=pin;
buff=0;
detection_click_time;
click_counter=0;
delay_time_click_not_work;
debug=false;
pinMode(BUTTON_PIN,INPUT);
//attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), test, RISING);
}
Button(bool debug_initialize,int pin=2, int detection_click_time_initialize=1000,int delay_time_click_not_work_iitialize=200){
BUTTON_PIN=pin;
debug=debug_initialize;
buff=0;
detection_click_time=detection_click_time_initialize;
click_counter=0;
delay_time_click_not_work=delay_time_click_not_work_iitialize;
pinMode(pin,INPUT);
//attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), test, RISING);
}
void buttonPushed(){
if(debug){Serial.println("Interrupt pushButton Button");}
if(!pushTriger) { //remember time when is triger triggered
time_pushed=millis();
buttonCheck();
}
buff=millis()-time_pushed;
if((buff<=detection_click_time) && !pushTriger){
//time_pushed=millis();
pushTriger=true;
click_counter=1;
if(debug){Serial.println("Interrupt pushButton Button pushed first");}
}else if((buff<=detection_click_time && (buff>delay_time_click_not_work)) && pushTriger){
//pushTriger=true;
click_counter++;
time_pushed=millis();
if(debug){Serial.print("Interrupt pushButton Button pushed second time: ");}
if(debug){Serial.println(click_counter);}
}
}
int buttonCheck(){
buff=millis()-time_pushed;
if(pushTriger && (buff>detection_click_time)){
if(debug){Serial.println("Begins check ");}
if(click_counter==1){
if(command==0){
command=last_command_turn;
}else{
command=0;
}
}else if(click_counter>=2 ){
if(command==2){
command=1;
last_command_turn=1;
}else if(command==1){
command=2;
last_command_turn=2;
}else{
if(last_command_turn==1){
last_command_turn=2;
}else{
last_command_turn=1;
}
}
}
time_pushed=0;
click_counter=0;
pushTriger=false;
if(debug){Serial.print("pushTriger: ");}
if(debug){Serial.println(pushTriger);}
if(debug){Serial.print("***** command: ");}
if(debug){Serial.println(command);}
}else{
}
return command;
}
};
Button button1(true); // first argument is debug true|false
void button_press(){
button1.buttonPushed();
}
int BUTTON_PIN=2;
void setup() {
Serial.begin(115200);
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN),button_press, RISING);
}
int command_for_loop=0; //This takes the command returned buttonCheck()
void loop() {
command_for_loop=button1.buttonCheck();
//Serial.println(command_for_loop);
}