VS Code / Platform IO Multiple files from flat main.cpp file

I am seeking help transitioning from a flat file main.cpp (which worked fine) to a multi-file setup in VSC/PlatformIO. After two days of online research, failure, and fatigue, I have decided to post my files here to beg for help.

Below is the error message I receive on build, and below that are my files (two .cpp and a .h) Any help would be greatly appreciated.

THE ERROR MESSAGE
AppData\Local\Temp\cc6saFwQ.ltrans0.ltrans.o: In function `main':
<artificial>:(.text.startup+0x2f4): undefined reference to `speedSetting'
<artificial>:(.text.startup+0x2f8): undefined reference to `speedSetting'
<artificial>:(.text.startup+0x2fc): undefined reference to `speedSetting'
<artificial>:(.text.startup+0x300): undefined reference to `speedSetting'
<artificial>:(.text.startup+0x4e8): undefined reference to `timeStamp'
<artificial>:(.text.startup+0x4ec): undefined reference to `timeStamp'
<artificial>:(.text.startup+0x4f0): undefined reference to `timeStamp'
<artificial>:(.text.startup+0x4f4): undefined reference to `timeStamp'
<artificial>:(.text.startup+0x516): undefined reference to `stallGuard'
<artificial>:(.text.startup+0x51a): undefined reference to `stallGuard'
<artificial>:(.text.startup+0x53a): undefined reference to `stallGuard'
<artificial>:(.text.startup+0x53e): undefined reference to `stallGuard'
<artificial>:(.text.startup+0x58c): undefined reference to `timeStamp'
<artificial>:(.text.startup+0x590): undefined reference to `timeStamp'
<artificial>:(.text.startup+0x594): undefined reference to `timeStamp'
<artificial>:(.text.startup+0x598): undefined reference to `timeStamp'
<artificial>:(.text.startup+0x59c): undefined reference to `speedSetting'
<artificial>:(.text.startup+0x5a0): undefined reference to `speedSetting'
<artificial>:(.text.startup+0x5a4): undefined reference to `speedSetting'
<artificial>:(.text.startup+0x5a8): undefined reference to `speedSetting'
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\itsybitsy32u4_5V\firmware.elf] Error 1
// src/main.cpp

#include <TMCStepper.h>
#include <Arduino.h>
#include "BRINGUP.h"

TMC2209Stepper driver(&SERIAL_PORT, R_SENSE, DRIVER_ADDRESS);

void setup()
{
  speedSetting = 5500;
  SERIAL_PORT.begin(115200); // INITIALIZE UART TMC2209
  delay(10);
  driver.begin();         // Initialize driver
  driver.microsteps(8);   // Set microsteps
  pinMode(ENN_PIN, HIGH); // Activates all motor power internal to TMC2209
}

void loop()
{
  updateTerminal(driver);
  driver.VACTUAL(speedSetting); // SET SPEED OF MOTOR
}
// include/BRINGUP.h

#pragma once

#include <Arduino.h>
#include <TMCStepper.h>

class TMC2209Stepper;

#define ENN_PIN 7           // Enable
#define SERIAL_PORT Serial1 // 328u HardwareSerial port pins 0 1
#define DRIVER_ADDRESS 0b00 // TMC2209 Driver address

#define R_SENSE 0.11f // Makerbase = 110milliOhm -- schematic says "0R11"

// prototypes
extern char16_t stallGuard;
extern long speedSetting;
extern long timeStamp;

void updateTerminal(TMC2209Stepper &driver);
// src/BRINGUP.cpp

#include "BRINGUP.h"

void updateTerminal(TMC2209Stepper &driver) // displays stallguard every second
{
    if (millis() - timeStamp >= 1000)
    {
        stallGuard = driver.SG_RESULT();
        Serial.print(F("Stall: "));
        Serial.print(stallGuard);
        timeStamp = millis();
    }
}

The variables need to be defined.

void updateTerminal(TMC2209Stepper &driver) // displays stallguard every second
{
    if (millis() - timeStamp >= 1000)
    {
        stallGuard = driver.SG_RESULT();
        Serial.print(F("Stall: "));
        Serial.print(stallGuard);
        timeStamp = millis();
    }
}

char16_t stallGuard;
long speedSetting;
long timeStamp;

Thanks gfalvo! That worked. I do not understand why the declaration in the .h was not sufficient. It is surprising how blind to C++ I was in the Arduino IDE.

See https://www.cprogramming.com/declare_vs_define.html

It works exactly the same way when you create .h and .cpp files in the Arduino IDE.

Thanks for the link. In the Arduino IDE I usually did flat file programs where I believe declaration and initialization seemed to happen in a single line at the top of the main.ino.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.