Hello, this is my first post in this Forum, so I would like to apologize in advance if I might explain my issue not well enough.
In general I would like to know how to use (I think they are) members of classes in a sub file.
In my case I have the following problem. I have several button I want to poll using Bounce2 library.
Part of my code (main.cpp) looks like this, I omitted the rest including definition of the variables used in the example as they are not related to my problem:
#include "Bounce2.h"
Bounce interrupt_Measure = Bounce();
Bounce interrupt_KeyUp = Bounce();
Bounce interrupt_KeyDown = Bounce();
Bounce interrupt_KeySelect = Bounce();
Bounce interrupt_KeyLeft = Bounce();
Bounce interrupt_KeyRight = Bounce();
void setup() {
interrupt_Measure.attach(MEASURE_PIN);
interrupt_KeyUp.attach(KEYUP_PIN);
interrupt_KeyDown.attach(KEYDOWN_PIN);
interrupt_KeySelect.attach(KEYSELECT_PIN);
interrupt_KeyLeft.attach(KEYLEFT_PIN);
interrupt_KeyRight.attach(KEYRIGHT_PIN);
interrupt_Measure.interval(DEBOUNCE_TIME);
interrupt_KeyUp.interval(DEBOUNCE_TIME);
interrupt_KeyDown.interval(DEBOUNCE_TIME);
interrupt_KeySelect.interval(DEBOUNCE_TIME);
interrupt_KeyLeft.interval(DEBOUNCE_TIME);
interrupt_KeyRight.interval(DEBOUNCE_TIME);
}
void(loop){
if (interrupt_Measure.update()) {
if (!interrupt_Measure.read()) {
Interrupt_Flag = MEASURE_FLAG;
last_interrupt_timer = millis();
button_pressed = MEASURE_FLAG;
} else
button_pressed = 0;
}
if (interrupt_KeyUp.update()) {
if (!interrupt_KeyUp.read()) {
Interrupt_Flag = KEYUP_FLAG;
last_interrupt_timer = millis();
button_pressed = KEYUP_FLAG;
} else
button_pressed = 0;
}
if (interrupt_KeyDown.update()) {
if (!interrupt_KeyDown.read()) {
Interrupt_Flag = KEYDOWN_FLAG;
last_interrupt_timer = millis();
button_pressed = KEYDOWN_FLAG;
} else
button_pressed = 0;
}
if (interrupt_KeySelect.update()) {
if (!interrupt_KeySelect.read()) {
Interrupt_Flag = KEYSELECT_FLAG;
last_interrupt_timer = millis();
button_pressed = KEYSELECT_FLAG;
} else
button_pressed = 0;
}
if (interrupt_KeyLeft.update()) {
if (!interrupt_KeyLeft.read()) {
Interrupt_Flag = KEYLEFT_FLAG;
last_interrupt_timer = millis();
button_pressed = KEYLEFT_FLAG;
} else
button_pressed = 0;
}
if (interrupt_KeyRight.update()) {
if (!interrupt_KeyRight.read()) {
Interrupt_Flag = KEYRIGHT_FLAG;
last_interrupt_timer = millis();
button_pressed = KEYRIGHT_FLAG;
} else
button_pressed = 0;
}
}
As you see there is a lot of code that does essentially the same, only for different button. Therefore I would like to replace at least the part in the main file with a function like
void handle_Interrupts(boolean *Interrupt_Flag,boolean *button_pressed,unsigned long *last_interrupt_timer)
, that I put into an additional interrupts file.
I tried to implement everything like with ordinary variables, which obviously failed.
Here is what I did:
I created an interrupts.cpp with following content:
#include "Global.h"
#include "Interrups.h"
#include "Bounce2.h"
Bounce interrupt_Measure = Bounce();
Bounce interrupt_KeyUp = Bounce();
Bounce interrupt_KeyDown = Bounce();
Bounce interrupt_KeySelect = Bounce();
Bounce interrupt_KeyLeft = Bounce();
Bounce interrupt_KeyRight = Bounce();
void handle_Interrupts(boolean *Interrupt_Flag,boolean *button_pressed,unsigned long *last_interrupt_timer){
if (interrupt_Measure.update()) {
if (!interrupt_Measure.read()) {
*Interrupt_Flag = MEASURE_FLAG;
*last_interrupt_timer = millis();
*button_pressed = MEASURE_FLAG;
} else
*button_pressed = 0;
}
if (interrupt_KeyUp.update()) {
if (!interrupt_KeyUp.read()) {
*Interrupt_Flag = KEYUP_FLAG;
*last_interrupt_timer = millis();
*button_pressed = KEYUP_FLAG;
} else
*button_pressed = 0;
}
if (interrupt_KeyDown.update()) {
if (!interrupt_KeyDown.read()) {
*Interrupt_Flag = KEYDOWN_FLAG;
*last_interrupt_timer = millis();
*button_pressed = KEYDOWN_FLAG;
} else
*button_pressed = 0;
}
if (interrupt_KeySelect.update()) {
if (!interrupt_KeySelect.read()) {
*Interrupt_Flag = KEYSELECT_FLAG;
*last_interrupt_timer = millis();
*button_pressed = KEYSELECT_FLAG;
} else
*button_pressed = 0;
}
if (interrupt_KeyLeft.update()) {
if (!interrupt_KeyLeft.read()) {
*Interrupt_Flag = KEYLEFT_FLAG;
*last_interrupt_timer = millis();
*button_pressed = KEYLEFT_FLAG;
} else
*button_pressed = 0;
}
if (interrupt_KeyRight.update()) {
if (!interrupt_KeyRight.read()) {
*Interrupt_Flag = KEYRIGHT_FLAG;
*last_interrupt_timer = millis();
*button_pressed = KEYRIGHT_FLAG;
} else
*button_pressed = 0;
}
}
further Interrupts.h:
#ifndef INTERRUPS_H_
#define INTERRUPS_H_
#include "Global.h"
#include "Bounce2.h"
void handle_Interrupts(boolean *Interrupt_Flag,boolean *button_pressed,unsigned long *last_interrupt_timer);
#endif /* INTERRUPS_H_ */
and a global.h :
#include <Arduino.h>
#include "Bounce2.h"
extern Bounce interrupt_Measure ;
extern Bounce interrupt_KeyUp ;
extern Bounce interrupt_KeyDown ;
extern Bounce interrupt_KeySelect ;
extern Bounce interrupt_KeyLeft;
extern Bounce interrupt_KeyRight;
from the main.cpp I removed the code in the main loop and included Global.h, Interrupts.h and Bounce2.h.
Compilations then ends with error messages like:
./include/Interrupts.cpp.o: In function `_GLOBAL__sub_I_interrupt_Measure':
C:\Users\clemens\Documents\ArduinoEclipse\DisplayVisu\Release/../include/Interrupts.cpp:20: multiple definition of `interrupt_Measure'
./DisplayVisu.cpp.o:C:\Users\clemens\Documents\ArduinoEclipse\DisplayVisu\Release/../DisplayVisu.cpp:270: first defined here
collect2.exe: error: ld returned 1 exit status
make: *** [DisplayVisu.elf] Error 1
I would be very glad I someone of you could help me with this, as I came across this problem on different occasions (like simply calling a debugging Serial.println in some sub library, when it was defined in the main file).
Thank you very much!