Split up code into more ino files

Updated on 3 September 2021
dev board Arduino Nano 33 BLE Sense
firmware Arduino
chip LSM9DS1
chip HTS221
sensor IMU
sensor temperature
sensor humidity
This tutorial is more than 1 year old. If the steps below do not work, then please check the latest versions and the documentations of the individual tools used.

Before starting

Dependancies

Ensure the following dependancies are downloaded and available:

Pre-requisites

Try these simpler or similar examples:

Buy the components

Code

Download code nano33-ble-sense-more-ino.ino
#include <Arduino_HTS221.h>
#include <Arduino_LSM9DS1.h>

void setup() {
  Serial.begin(9600);
  while (!Serial) {}

  if (!initSensor()) {
    Serial.println("Failed to initialize humidity temperature sensor!");
  }

  if (!initIMU()) {
    Serial.println("Failed to initialize IMU!");
  }

  Serial.println("Let's start!");
}

void loop() {
  float temperature;
  float humidity;
  getSensorValues(&temperature, &humidity);

  Serial.print("Temperature = ");
  Serial.print(temperature);
  Serial.println(" °C");

  Serial.print("Humidity = ");
  Serial.print(humidity);
  Serial.println(" %");

  Serial.println();

  delay(1000);

  float x, y, z;
  if (getAcceleration(&x, &y, &z)) {
    Serial.print("X: ");
    Serial.print(x);
    Serial.print('\t');

    Serial.print("Y: ");
    Serial.print(y);
    Serial.print('\t');

    Serial.print("Z: ");
    Serial.println(z);
  }
}

Makefile

BOARD?=arduino:mbed:nano33ble
PORT?=/dev/cu.usbmodem14*
BUILD=build

.PHONY: default lint all flash clean

default: lint all flash clean

lint:
	cpplint --extensions=ino --filter=-legal/copyright,-runtime/int *.ino

all:
	arduino-cli compile --fqbn $(BOARD) --output-dir $(BUILD) ./

flash:
	arduino-cli upload --fqbn $(BOARD) --port $(PORT) --input-dir $(BUILD)

clean:
	rm -r build

Description

Display IMU, temperature and humidity data with Arduino board Nano 33 BLE Sense with IMU LSM9DS1 and sensor HTS221.

Install the dependancy:

arduino-cli lib install Arduino_LSM9DS1
arduino-cli lib install Arduino_HTS221

Split up the code into various *.ino files with a folder structure like this:

$ tree
.
├── Makefile
├── imu.ino
├── nano33-ble-sense-more-ino.ino
└── sensor.ino

References