This test shows a problem I encountered with the IDE 1.8.19 trying to use a .cpp file and a header file:
First attempt:
I place a header file test2.h in the working directory
I copy the contents of my code file, test2.cpp, and paste it in the skecth as per exhibit 1.
Hit the Verify icon and the compile works.
Next I want to test using a file instead of the copy and paste process:
I place the file named test2.cpp in the working directory.
I modify the sketch per exhibit 2
Compile fails. The compiler no longer recognize int8_t/int16_t as data types.
To get this process to work, I have to do 2 things, First I have to replace all int8_t and int16_t with unsigned char and int respectively. Replacing int8_t with byte was not accepted. Second, to get the line of code:
micro_snap = TCNT1;
to compile, I had to add to test2.cpp:
#include <arduino.h>
As an aside, I still have not figured out a way to incorporate the line
TCCR1A = 0;
in the test2.cpp file.
Questions: Why does the compiler no longer accept int8_t as data type, or byte for that matter.
Why the need to explicitly include the arduino library
How do I move TCCR1A=0 within one of the files.
//###################################################################
//Exhibit 1 Copy and paste code from test2.cpp in sketch. test2.h in directory
void setup() {
TCCR1A = 0;
Serial.begin(115200);
event_check();
}
//Section below exclusive of void loop(){} is the content of test2.cpp
#include "test2.h"
int8_t old_Btim = 0;
int8_t sum_32 = 0;
int8_t sum_2 = 0;
void event_check(){
int8_t Btimer;
int8_t delta;
int16_t micro_snap;
int8_t next = 0;
micro_snap = TCNT1;
Btimer = ((micro_snap>>8) & 0x7f);
delta = Btimer - old_Btim;
if (delta<0){delta += 128;}
old_Btim = Btimer;
sum_2 += delta;
if (sum_2 > 1){
sum_2 -= 2; //2.048ms threshold with 1.024ms resolution
next = 1;
}
sum_32 += delta;
if (sum_32 > 31){
next = 2; //32.768ms threshold with 1.024ms resolution
sum_32 -= 32;
}
return next;
}
void loop() {
}
//#####################################
contents of test2.h:
extern int8_t old_Btim;
extern int8_t sum_32;
extern int8_t sum_2;
Compilation result:
Sketch uses 1506 bytes (4%) of program storage space. Maximum is 30720 bytes.
Global variables use 187 bytes (9%) of dynamic memory, leaving 1861 bytes for local variables. Maximum is 2048 bytes.
//###################################################################
//Exhibit 2. Both test2.cpp and test2.h are in directory
void event_check();
void setup() {
TCCR1A = 0;
Serial.begin(115200);
event_check();
}
void loop() {
}
Sketch uses 1502 bytes (4%) of program storage space. Maximum is 30720 bytes.
Global variables use 187 bytes (9%) of dynamic memory, leaving 1861 bytes for local variables. Maximum is 2048 bytes.