I have code where I use pointer functions to trigger different functions in the loop based on receiving the A and B keys from a keypad. However, when I run it on Tinkercad, it doesn’t work.
#include <Adafruit_LiquidCrystal.h>
#include <Keypad.h>
#include <math.h>
Adafruit_LiquidCrystal lcd(0);
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
String inputString = "";
int x1, y1, x2 = 0 , y2 = 0;
float x3, y3 ;
int OTDS, OTAZ;
bool firstCoordinateSet = false;
bool secondCoordinateSet = false;
bool OTAZ_Set = false;
bool OTDS_Set = false;
float angle;
float distance;
typedef void (*FunctionPointer)();
FunctionPointer selectedFunction;
void functioncoord() {
char key = keypad.getKey();
if (key) {
if (key == 'C') {
inputString = "";
lcd.clear();
firstCoordinateSet = false;
secondCoordinateSet = false;
} else if (key == '*') {
if (inputString.length() > 0) {
inputString.remove(inputString.length() - 1);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(inputString);
}
} else if (key == 'A') {
if (inputString.length() == 8) {
x1 = inputString.substring(0, 4).toInt();
y1 = inputString.substring(4).toInt();
inputString = "";
lcd.clear();
firstCoordinateSet = true;
lcd.print("Target Coord Set");
delay(1000);
lcd.clear();
}
} else if (key == 'B') {
if (inputString.length() == 8) {
x2 = inputString.substring(0, 4).toInt();
y2 = inputString.substring(4).toInt();
inputString = "";
lcd.clear();
secondCoordinateSet = true;
lcd.print("Mortar Coord Set");
delay(1000);
lcd.clear();
}
} else if (key == '#') {
if (firstCoordinateSet) {
calculate_A();
lcd.setCursor(0, 0);
lcd.print("Arg:");
lcd.print(round((angle * 6400 / 360)));
lcd.print(" mils");
lcd.setCursor(0, 1);
lcd.print("DS:");
lcd.print(distance);
lcd.print(" m");
}
} else {
if (inputString.length() < 8) {
inputString += key;
}
String displayString = inputString;
if (inputString.length() > 4) {
displayString = inputString.substring(0, 4) + "," + inputString.substring(4);
}
lcd.setCursor(0, 0);
lcd.print(displayString);
}
}
}
void functionangle() {
char key = keypad.getKey();
if (key) {
if (key == 'C') {
inputString = "";
lcd.clear();
firstCoordinateSet = false;
secondCoordinateSet = false;
OTAZ_Set = false;
OTDS_Set = false;
} else if (key == '*') {
if (inputString.length() > 0) {
inputString.remove(inputString.length() - 1);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(inputString);
}
} else if (key == 'A') {
if (inputString.length() == 8) {
if (!firstCoordinateSet) {
x1 = inputString.substring(0, 4).toInt();
y1 = inputString.substring(4).toInt();
inputString = "";
lcd.clear();
firstCoordinateSet = true;
lcd.print("Observation Post");
lcd.setCursor(0, 1);
lcd.print("Coord Set");
delay(1000);
lcd.clear();
} else if (firstCoordinateSet && !secondCoordinateSet) {
x2 = inputString.substring(0, 4).toInt();
y2 = inputString.substring(4).toInt();
inputString = "";
lcd.clear();
secondCoordinateSet = true;
lcd.print("Mortar Coord Set");
delay(1000);
lcd.clear();
}
}
} else if (key == 'B') {
if (inputString.length() > 0 && !OTAZ_Set) {
OTAZ = inputString.substring(0,8).toInt();
inputString = "";
lcd.clear();
OTAZ_Set = true;
lcd.print("OTAZ Set");
delay(1000);
lcd.clear();
} else if (inputString.length() > 0 && OTAZ_Set && !OTDS_Set) {
OTDS = inputString.substring(0,8).toInt();
inputString = "";
lcd.clear();
OTDS_Set = true;
lcd.print("OTDS Set");
delay(1000);
lcd.clear();
}
} else if (key == '#') {
if (firstCoordinateSet) {
calculate_B();
lcd.setCursor(0, 0);
lcd.print("Arg:");
lcd.print(round((angle * 6400 / 360)));
lcd.print(" mils");
lcd.setCursor(0, 1);
lcd.print("DS:");
lcd.print(distance);
lcd.print(" m");
}
} else {
if (inputString.length() < 8) {
inputString += key;
}
String displayString = inputString;
if (inputString.length() > 4) {
displayString = inputString.substring(0, 4) + "," + inputString.substring(4);
}
lcd.setCursor(0, 0);
lcd.print(displayString);
}
}
}
void setup() {
lcd.begin(16, 2);
lcd.setBacklight(LOW);
lcd.print("Mortar Calc");
delay(1000);
lcd.clear();
while (!selectedFunction) {
char key = keypad.getKey();
if (key) {
inputString += key;
lcd.print(inputString);
if (key == 'A') {
selectedFunction = functioncoord;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SquareCoordinate");
lcd.setCursor(0, 1);
lcd.print("Method");
delay(1500);
inputString = "";
lcd.clear();
} else if (key == 'B') {
selectedFunction = functionangle;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Polar Method");
delay(1500);
inputString = "";
lcd.clear();
}
}
}
}
void loop() {
if (selectedFunction) {
selectedFunction();
}
}
void calculate_A() {
float deltaX = x1 - x2;
float deltaY = y1 - y2;
if (deltaX > 0 && deltaY > 0) {
angle = (180 + atan2(deltaX, deltaY) * (180 / PI));
} else if (deltaX < 0 && deltaY < 0) {
angle = (180+atan2(-1*deltaX, -1*deltaY) * (180 / PI));
} else if (deltaX > 0 && deltaY < 0) {
angle = (atan2(deltaX, deltaY) * (180 / PI));
} else if (deltaX < 0 && deltaY > 0) {
angle = (180 - atan2(-1 * deltaX, -1 * deltaY) * (180 / PI));
}
distance = sqrt(pow(deltaX, 2) + pow(deltaY, 2))*10;
}
void calculate_B() {
x3 = (float)x1 + OTDS*sin((float)OTAZ*360/6400*PI/180)*0.1;
y3 = (float)y1 + OTDS*cos((float)OTAZ*360/6400*PI/180)*0.1;
float deltaX = x3 - (float)x2;
float deltaY = y3 - (float)y2;
if (deltaX > 0 && deltaY > 0) {
angle = (180 + atan2(deltaX, deltaY) * (180 / PI));
} else if (deltaX < 0 && deltaY < 0) { /
angle = (180+atan2(-1*deltaX, -1*deltaY) * (180 / PI));
} else if (deltaX > 0 && deltaY < 0) {
angle = (atan2(deltaX, deltaY) * (180 / PI));
} else if (deltaX < 0 && deltaY > 0) {
angle = (180 - atan2(-1 * deltaX, -1 * deltaY) * (180 / PI));
}
distance = sqrt(pow(deltaX, 2) + pow(deltaY, 2))*10;
}
The error message is as follows: invalid headerflie
If I make one of the functions, either functionangle or functioncoord, an empty function, the code works fine. So, I suspect that the repeated content in both functions might be the cause. I have tried many different approaches over the past week, but I still can’t solve it. I really need your help!
주민건 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.