TuningSource Example Sketch will not compile on Pro Mini

I want to build a dedicated TuningSource for tuning MegaTinyCore devices (eg ATtiny1604 / ATtiny402 / ATtiny3226 as I'm fed up fiddling with my scope as a 1KHz source for the sketch from File/Examples/megaTinyCore/megaTinyTuner (which incidentally works a treat).

I am using Arduino 1.8.19 running on an Ubuntu desktop.

I have a Pro Mini based on ATmega168 - it's got a big chunky 16MHz crystal (is why I sourced it) so I assume it's a 5v 16MHz version. I can successfully load and run the File/Examples/01.Basics/Blink changing the delays to prove it's loaded and run the code.

I opened up the File/Examples/megaTinyCore/TuningSource and tried to compile the code but it threw the following errors. (I also imported the sketch into PlaotoformIO and it had similar issues.)

Here are the compiler error messages with the All warnings selected...

Arduino: 1.8.19 (Linux), Board: "Arduino Pro or Pro Mini, ATmega168 (5V, 16 MHz)"











/snap/arduino/85/arduino-builder -dump-prefs -logger=machine -hardware /snap/arduino/85/hardware -hardware /home/peter/snap/arduino/85/.arduino15/packages -tools /snap/arduino/85/tools-builder -tools /snap/arduino/85/hardware/tools/avr -tools /home/peter/snap/arduino/85/.arduino15/packages -built-in-libraries /snap/arduino/85/libraries -libraries /home/peter/snap/arduino/current/Arduino/libraries -fqbn=arduino:avr:pro:cpu=16MHzatmega168 -vid-pid=0403_6001 -ide-version=10819 -build-path /tmp/arduino_build_683736 -warnings=all -build-cache /tmp/arduino_cache_185918 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.avr-gcc.path=/home/peter/snap/arduino/85/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7 -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino7.path=/home/peter/snap/arduino/85/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7 -prefs=runtime.tools.arduinoOTA.path=/home/peter/snap/arduino/85/.arduino15/packages/arduino/tools/arduinoOTA/1.3.0 -prefs=runtime.tools.arduinoOTA-1.3.0.path=/home/peter/snap/arduino/85/.arduino15/packages/arduino/tools/arduinoOTA/1.3.0 -prefs=runtime.tools.avrdude.path=/home/peter/snap/arduino/85/.arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17 -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=/home/peter/snap/arduino/85/.arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17 -verbose /home/peter/Documents/PlatformIO/Projects/TuningSource/TuningSource.ino
/snap/arduino/85/arduino-builder -compile -logger=machine -hardware /snap/arduino/85/hardware -hardware /home/peter/snap/arduino/85/.arduino15/packages -tools /snap/arduino/85/tools-builder -tools /snap/arduino/85/hardware/tools/avr -tools /home/peter/snap/arduino/85/.arduino15/packages -built-in-libraries /snap/arduino/85/libraries -libraries /home/peter/snap/arduino/current/Arduino/libraries -fqbn=arduino:avr:pro:cpu=16MHzatmega168 -vid-pid=0403_6001 -ide-version=10819 -build-path /tmp/arduino_build_683736 -warnings=all -build-cache /tmp/arduino_cache_185918 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.avr-gcc.path=/home/peter/snap/arduino/85/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7 -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino7.path=/home/peter/snap/arduino/85/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7 -prefs=runtime.tools.arduinoOTA.path=/home/peter/snap/arduino/85/.arduino15/packages/arduino/tools/arduinoOTA/1.3.0 -prefs=runtime.tools.arduinoOTA-1.3.0.path=/home/peter/snap/arduino/85/.arduino15/packages/arduino/tools/arduinoOTA/1.3.0 -prefs=runtime.tools.avrdude.path=/home/peter/snap/arduino/85/.arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17 -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=/home/peter/snap/arduino/85/.arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17 -verbose /home/peter/Documents/PlatformIO/Projects/TuningSource/TuningSource.ino
Using board 'pro' from platform in folder: /home/peter/snap/arduino/85/.arduino15/packages/arduino/hardware/avr/1.8.6
Using core 'arduino' from platform in folder: /home/peter/snap/arduino/85/.arduino15/packages/arduino/hardware/avr/1.8.6
Detecting libraries used...
/home/peter/snap/arduino/85/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega168 -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_PRO -DARDUINO_ARCH_AVR -I/home/peter/snap/arduino/85/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino -I/home/peter/snap/arduino/85/.arduino15/packages/arduino/hardware/avr/1.8.6/variants/eightanaloginputs /tmp/arduino_build_683736/sketch/TuningSource.ino.cpp -o /dev/null
Alternatives for EEPROM.h: [EEPROM@2.0]
ResolveLibrary(EEPROM.h)
  -> candidates: [EEPROM@2.0]
/home/peter/snap/arduino/85/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega168 -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_PRO -DARDUINO_ARCH_AVR -I/home/peter/snap/arduino/85/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino -I/home/peter/snap/arduino/85/.arduino15/packages/arduino/hardware/avr/1.8.6/variants/eightanaloginputs -I/home/peter/snap/arduino/85/.arduino15/packages/arduino/hardware/avr/1.8.6/libraries/EEPROM/src /tmp/arduino_build_683736/sketch/TuningSource.ino.cpp -o /dev/null
Error while detecting libraries included by /tmp/arduino_build_683736/sketch/TuningSource.ino.cpp
Generating function prototypes...
/home/peter/snap/arduino/85/.arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino7/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega168 -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_PRO -DARDUINO_ARCH_AVR -I/home/peter/snap/arduino/85/.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino -I/home/peter/snap/arduino/85/.arduino15/packages/arduino/hardware/avr/1.8.6/variants/eightanaloginputs -I/home/peter/snap/arduino/85/.arduino15/packages/arduino/hardware/avr/1.8.6/libraries/EEPROM/src /tmp/arduino_build_683736/sketch/TuningSource.ino.cpp -o /tmp/arduino_build_683736/preproc/ctags_target_for_gcc_minus_e.cpp
TuningSource:18:0: error: unterminated #ifndef
 #ifndef TUNE_OUTPUT_PIN // If not defined in parts.h, need to pick default pin
 
TuningSource:5:0: error: unterminated #if
 #if __AVR_ARCH__ >=100
 
Using library EEPROM at version 2.0 in folder: /home/peter/snap/arduino/85/.arduino15/packages/arduino/hardware/avr/1.8.6/libraries/EEPROM 
exit status 1
unterminated #ifndef

I guess the first issue is resolving the libraries - how do I achieve this?

Check your version of megaTinyCore's TuningSource.ino for (some) missing #endif (not likely).

Check your sketch for an unterminated #if , #ifdef or #ifndef (needing #endif)

Not libraries.

Perhaps an example designed for an ATtiny is unlikely to compile as is for a board using an ATmega.

I really struggle with all this pre-compile time code - I was hoping for a quick win to a nice 500Hz signal - I might just hack the code and set TCCR1A, TCCR1B, ICR1, OCR1A and OCR1B until I get my signal. I am not trying to create a general tuning source for all boards - just for the one I plan to dedicate to the task.

void setup() {
   pinMode(3, OUTPUT);
   tone(3, 500);
}
void loop() {
}

?

That works for me. Thanks
The tone library uses the timer in normal mode to generate interrupts that are then converted into pin changes. I get that this allows pin flexibility but that's extra code running about. I had a think and came up with a timer set up that generates the waveform directly, albeit there is no pin choice. If you're interested, the code with explainers is below....

// cSpell:includeRegExp CStyleComment
// Written by P Charles-Jones
// May 14 2025
//
// ProMini168p_500Hz_Source

#include <Arduino.h>

// Pin Identification
//
// Timer  AVR   Arduino
//
// OC2B   PD3   3
// OC0B   PD5   5
// OC0A   PD6   6
// OC1A   PB1   9 <- Use this pin
// OC1B   PB2   10
// OC2A   PB3   11
//
// Discussion
// I wanted to create a 500Hz timing source to help tune the megaTinyCore devices.
// I selected a real cheap clone but one with a chunky 16MHz crystal.
// I failed with the supplied TuningSource sketch as it got tripped up on compile.
// It was suggested I use the tone library (this worked) but looking into this I can 
// see it uses a timer to generate interrupts which are then processed into the pin signal.
// This seemed a bit involved and I was keen to have a timer generate the signal directly.
// Looking at the data sheet for a ATmega168, I selected the 16 bit Timer1.
// If you select output OC1A, you can have a fast PWM signal that toggles the output pin
// on a compare match. If my target output is to be 500Hz, I need the pin to toggle at 1kHz.
// I am using OCR1A for the TOP value and this outputs on PB1 or Arduino pin 9.
// The frequency is defined as PWM_f = F_CPU / (prescaler * (1+TOP))
// Rearrange this and you get (1+TOP) = F_PCU / (PWM_f * prescaler)
// F_PCU is 16000000, PWM_f is 1000
// To get value for TOP below 0xFFFF, the  prescaler needs to be 64.
// (I tried  prescaler of 8 but even counting TOP to MAX, was too fast for 1kHz)
// This yields a value for TOP of 249 

// put function declarations here:

void setup() {
  // put your setup code here, to run once:

  // comments 
  // PWM mode 15, Fast PWM with OCR1A as TOP                                    WGM13:0 = 0b1111
  // toggle OC1A on compare match to get a 50% duty cycle (only works for OC1A) COM1A1:0 = 0b10
  // prescaler = 64                                                             CS12:0 = 0b011
  // TOP = 249 = 0xF9 the value for OCR1A
  // output on OC1A PB1 Arduino pin 9
  // the bit shifting of 0s is just for completeness to mention each register bit position

  OCR1A = 0xF9;                                                                       
  TCCR1A = (0<<COM1A1)|(1<<COM1A0)|(0<<COM1B1)|(0<COM1B0)|(1<<WGM11)|(1<<WGM10);  
  TCCR1B = (0<<ICNC1)|(0<<ICES1)|(1<<WGM13)|(1<<WGM12)|(0<<CS12)|(1<<CS11)|(1<<CS10);        
  TCCR1C = (0<<FOC1A)|(0<<FOC1B);

  // // I tried using PWM mode 14 and hence ICR1 to set TOP, you need to keep OCR1A greater 
  // // than the counter. It nearly worked with a recorded frequency of 488.00Hz, not sure why.
  // ICR1 = 0xF9;  
  // OCR1A= 0xFFFF;                                                                     
  // TCCR1A = (0<<COM1A1)|(1<<COM1A0)|(0<<COM1B1)|(0<COM1B0)|(1<<WGM11)|(0<<WGM10);  
  // TCCR1B = (0<<ICNC1)|(0<<ICES1)|(1<<WGM13)|(1<<WGM12)|(0<<CS12)|(1<<CS11)|(1<<CS10);        
  // TCCR1C = (0<<FOC1A)|(0<<FOC1B);

  // enable the pin for output - data sheet says do this after timer set up.
  DDRB = (1<<PB1);

  // as a flourish, save some power
  // PRTIM1 is the essential one to have set 0
  PRR = (1<<PRTWI)|(1<<PRTIM2)|(1<<PRTIM0)|(0<<PRTIM1)|(1<<PRSPI)|(1<<PRUSART0)|(1<<PRADC);     
}

void loop() {
  // put your main code here, to run repeatedly:
}

// put function definitions here: