Hi @lexter333 ,
the Arduino IDE (unfortunately in this case
) takes care that functions defined later in a sketch are brought to the knowledge of the compiler early.
Usually a C/C++ programmer has to take care that the compiler knows the interface specification of a function (does it require parameters and if yes how many and which types).
If you put these two lines in front of your comms.h
void HelpersSendLEDIndication();
void HelpersReceiveLEDIndication();
before their first use the compiler will accept that they are defined later in the sketch.
This version compiles:
#pragma once
#define TXD1 19
#define RXD1 21
/**********************
****** LEDs ***********
***********************/
#define SendLED 16 //Send LED
#define ReceiveLED 17 //Receive LED
// Use Serial1 for UART communication
HardwareSerial mySerial(2);
void HelpersSendLEDIndication();
void HelpersReceiveLEDIndication();
//Called from the ino Setup
void HelpersSetup() {
mySerial.begin(9600, SERIAL_8N1, RXD1, TXD1); // UART setup
Serial.println("ESP32 UART Receiver");
pinMode(SendLED, OUTPUT);
pinMode(ReceiveLED, OUTPUT);
digitalWrite(SendLED, LOW);
digitalWrite(ReceiveLED, LOW);
delay(50);
//flash both leds as an indication
HelpersSendLEDIndication();
HelpersReceiveLEDIndication();
}
void HelpersSendLEDIndication(){
for (int i = 0; i <= 3; i++) {
digitalWrite(SendLED, HIGH);
delay(100);
digitalWrite(SendLED, LOW);
}
}
void HelpersReceiveLEDIndication() {
for (int i = 0; i <= 3; i++) {
digitalWrite(ReceiveLED, HIGH);
delay(100);
digitalWrite(ReceiveLED, LOW);
}
}
However as @gcjr wrote already in well written C/C++ you would (forward) declare the functions in an .h or .hpp file and provide the final declaration of the function in a .c or .cpp file.
The reason for this is that someone using your library does not need to know how the functions are programmed as long as he/she nows how to call a specific function from outside.
Good luck!
ec2021
P.S.: I included a "#pragma once" in the .h-file to make sure that it will only be included once in a single project (include guard).
If you are interested you may look here to see a very simple example of
comms.h split to .h and .cpp
Example sketch sketch.ino that makes use of comms.h:
/*
Forum: https://forum.arduino.cc/t/breaking-ino-files-into-h-files-fails/1406752/4
Wokwi: https://wokwi.com/projects/442462078146788353
*/
#include "comms.h"
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
HelpersSetup();
}
unsigned long lastS = 0;
unsigned long lastR = 0;
void loop() {
if (millis() - lastS > 1000) {
lastS = millis();
HelpersSendLEDIndication();
}
if (millis() - lastR > 2000) {
lastR = millis();
HelpersReceiveLEDIndication();
}
}
comms.h
#pragma once
#include "Arduino.h"
#define TXD1 19
#define RXD1 21
/**********************
****** LEDs ***********
***********************/
#define SendLED 16 //Send LED
#define ReceiveLED 17 //Receive LED
// Functions
void HelpersSendLEDIndication();
void HelpersReceiveLEDIndication();
void HelpersSetup();
comms.cpp
#pragma once
#include "comms.h"
// Use Serial1 for UART communication
HardwareSerial mySerial(2);
//Called from the ino Setup
void HelpersSetup() {
mySerial.begin(9600, SERIAL_8N1, RXD1, TXD1); // UART setup
Serial.println("ESP32 UART Receiver");
pinMode(SendLED, OUTPUT);
pinMode(ReceiveLED, OUTPUT);
digitalWrite(SendLED, LOW);
digitalWrite(ReceiveLED, LOW);
delay(50);
//flash both leds as an indication
HelpersSendLEDIndication();
HelpersReceiveLEDIndication();
}
void HelpersSendLEDIndication(){
for (int i = 0; i <= 3; i++) {
digitalWrite(SendLED, HIGH);
delay(100);
digitalWrite(SendLED, LOW);
}
}
void HelpersReceiveLEDIndication() {
for (int i = 0; i <= 3; i++) {
digitalWrite(ReceiveLED, HIGH);
delay(100);
digitalWrite(ReceiveLED, LOW);
}
}
Feel free to check it out on https://wokwi.com/projects/442462078146788353
It is NOT very elaborated but demonstrates the principle use ...