Arduino struct with functions in seperate folder

struct function arduino files header cpp pointer

Pre-requisites

Use struct data type to declare, create and use it in a function. The struct functions are abstracted in a seperate folder to show how it can be used and referred in the main.ino.

The struct LatLong can be declared:

struct LatLong {
  double latitude;
  double longitude;
};

The struct can be defined:

LatLong latlong = {1.2480631, 103.8285657};

The struct can be used in a function declaration:

void goToSentosa(struct LatLong *);

The struct can be used in a function definition:

void goToSentosa(struct LatLong *ll) {
  ...
}

Code

Download code
#include "src/latlong/latlong.h"

LatLong latlong = {1.2480631, 103.8285657};

void setup() {
  Serial.begin(9600);
  while (!Serial) { }
  Serial.println("Locations of places around the world!");
}

void loop() {
  goToSentosa(&latlong);
  Serial.print("Sentosa: (");
  Serial.print(latlong.latitude, 6);
  Serial.print(", ");
  Serial.print(latlong.longitude, 6);
  Serial.println(")");

  goToEiffelTower(&latlong);
  Serial.print("Eiffel Tower: (");
  Serial.print(latlong.latitude, 6);
  Serial.print(", ");
  Serial.print(latlong.longitude, 6);
  Serial.println(")");

  delay(2000);
}

Serial console

Serial output from the firmware.

Arduino struct with functions in seperate folder serial console