Programming help

Hi I have this problem …

in my encoder.h file, excerpt below I have defined an extern volatile variable for use in an ISR. The ISR is defined in a file called encoder.cpp

extern volatile uint16_t A_Counter;     // Initialise this to 10253 / (360 / 261) - the position of due west - 261 for the dome when the scope is at 270. 
extern long Azimuth;

in main.cpp, I want to initialise the variable, so I wrote this:

#include "encoder.h"

  A_Counter = 7433;         // note the variable is declared in the header file as extern. Its initial value is assigned here

when I compile the source i get this error:

 Executing task: C:\Users\Paul\.platformio\penv\Scripts\pio.exe run <

Processing ATmega4809_pyupdi_upload (platform: atmelmegaavr; board: ATmega4809; framework: arduino)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/atmelmegaavr/ATmega4809.html
PLATFORM: Atmel megaAVR (1.4.0) > ATmega4809
HARDWARE: ATMEGA4809 16MHz, 6KB RAM, 48KB Flash
PACKAGES:
 - framework-arduino-megaavr-megacorex 1.0.7
 - toolchain-atmelavr 2.70300.201015 (7.3.0)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 10 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <src>
Building in release mode
Compiling .pio\build\ATmega4809_pyupdi_upload\src\main.cpp.o
src\main.cpp:9:3: error: 'A_Counter' does not name a type
   A_Counter =7433;         // note the variable is declared in the header file as extern. It is defined here
   ^~~~~~~~~
*** [.pio\build\ATmega4809_pyupdi_upload\src\main.cpp.o] Error 1
================================================================================================== [FAILED] Took 0.84 seconds ==================================================================================================
The terminal process "C:\Users\Paul\.platformio\penv\Scripts\pio.exe 'run'" terminated with exit code: 1.

seems a bit basic assigning values to variables…thanks for any thoughts on this.
Paul

The proper way is:

Declaration in encoder.h:

extern volatile uint16_t A_Counter;

Definition and initialization in encoder.cpp:

volatile uint16_t A_Counter = 7433;

Use in other code, e.g. main.cpp:

void some_function() {
    if (A_Counter > 300) {
        A_Counter = 300;
    }
}

Your current code is a mixture between definition (as it is outside of any function) and use (as it does not specify a type).

ah, great, thanks, I’ll give it a try.