I give up - setting timer1 in setup()

How do I add an ISR to timer1 in setup() without interfering with Serial.println()?
debug.log(F("Setup() complete...")); is effectively Serial.println("Setup() complete...");

I am seeing the following in the serial monitor where output of "Setup() complete..." is getting cut off mid way.

Initializing SD card...done!
[WiFiEsp] Initializing ESP module
[WiFiEsp] Initilization successful - 1.1.1
From SD card SSID = Telstra6A61B7, KEY = 0AC9703D29
Attempting to connect to WPA SSID: Telstra6A61B7
[WiFiEsp] Connected to Telstra6A61B7
You're connected to the network with IP address: 10.0.0.79
[WiFiEsp] Server started on port 80
====================================================================================
station1
	probe:
	allowed_dry_time:0
	probe_threshold_value:0
	probe_auto:
	auto_run_time:0
	suspend:
	00:00-1(done),02:00-1(done),04:00-1(done),06:00-1(done),08:00-1(done),10:00-1(done),12:00-1(done),14:00-1(done),16:00-1(not done),18:00-1(not done),20:00-1(not done),22:00-1(not done)
station2
	probe:installed
	allowed_dry_time:10
	probe_threshold_value:1500
	probe_auto:yes
	auto_run_time:1
	suspend:
	00:00-10(done)
station3
	probe:
	allowed_dry_time:0
	probe_threshold_value:0
	probe_auto:
	auto_run_time:0
	suspend:
	00:00-10(done)
station4
	probe:
	allowed_dry_time:0
	probe_threshold_value:0
	probe_auto:
	auto_run_time:0
	suspend:
	00:00-1(done),02:00-1(done),04:00-1(done),06:00-1(done),08:00-1(done),10:00-1(done),12:00-1(done),14:00-1(done),16:00-1(not done),18:00-1(not done),20:00-1(not done),22:00-1(not done)
station5
	probe:
	allowed_dry_time:0
	probe_threshold_value:0
	probe_auto:
	auto_run_time:0
	suspend:
	00:00-10(done)
station6
	probe:
	allowed_dry_time:0
	probe_threshold_value:0
	probe_auto:
	auto_run_time:0
	suspend:
	00:00-10(done)
station7
	probe:
	allowed_dry_time:0
	probe_threshold_value:0
	probe_auto:
	auto_run_time:0
	suspend:
	00:00-10(done)
station8
	probe:
	allowed_dry_time:0
	probe_threshold_value:0
	probe_auto:
	auto_run_time:0
	suspend:
	00:00-1(done),02:00-1(done),04:00-1(done),06:00-1(done),08:00-1(done),10:00-1(done),12:00-1(done),14:00-1(done),16:00-1(not done),18:00-1(not done),20:00-1(not done),22:00-1(not done)
====================================================================================
Synchronising RTC from '0.uk.pool.ntp.org'...14/11 15:25...done!
Set
//******************************************************************************************
//* Runs at 1 minute intervals
//******************************************************************************************
bool g_bNewDayFlag = false;
bool g_bISRTriggered = false;

void On1MinTimer()
{
  noInterrupts();
  rtc.readClock();
  g_bNewDayFlag = rtc.getHours() == 0;
  
  // Then iterate through all the programs, for all stations and for today's date, and we need to run any of them based on the current time.
  program.run();

  // A new day has started so we need to read the program.txt and get the irrigations times for the new date so read in the program for each station for the new date.
  if (g_bNewDayFlag)
  {
    g_bNewDayFlag = false;
    debug.init();
    debug.dumpSaved();
    
    debug.log(F("########################################"));
    debug.log(F("NEW DAY"));
    debug.log(F("--------"));
    debug.log(F("Reading program.txt!"));
    debug.log(F("########################################"));
    if (!program.read())
      debug.logRuntimeError(F("IrrigationController.ino"), __LINE__);

    g_nWeekDayNum++;
    if (g_nWeekDayNum == 7)
    {
      OnWeeklyTimer();
      g_nWeekDayNum == 1;
    }
    g_nMinNum++;
    if (g_nMinNum == 10)
    {
      OnAlarmsTimer();
      g_nMinNum == 0;
    }
  }
  g_bISRTriggered = false;
  interrupts();
}

void isrFunction()
{
  g_bISRTriggered = true;
}

//*************************************************************************************************
// Do all the setup tasks.
//*************************************************************************************************
void setup() 
{  
  program.begin();
  
  // Open serial communications and wait for port to open:
  Serial.begin(115200);

  // Wait for serial port to connect - needed for native USB port only
  delay(10);

  if (initSD())
  {
    debug.init();

    serialHC05.begin(9600);

    if (WifiServer.begin() && program.read())
      program.begin();
    else
      debug.logRuntimeError(F("IrrigationController.cpp"), __LINE__);

    WifiServer.synchClock();
    rtc.readClock();
    timeLastAlarms.set(rtc.getHours(), rtc.getMinutes(), rtc.getSeconds());
    [b]noInterrupts();
    Timer1.initialize(600000L);
    Timer1.attachInterrupt(isrFunction);
    interrupts();
    debug.log(F("Setup() complete..."));[/b]
  }
  else
    debug.logRuntimeError(F("IrrigationController.cpp"), __LINE__);
}

You cannot use Serial inside an ISR. Serial output uses interrupts to do its thing.

Your ISR should set the values of some volatile variables, and your loop should poll those variables in a block where interrupts are disabled to determine if something happened.

The pattern is:

struct SomeStuff {
  // variables go here
};

boolean got_isr = false; // this is called a "mutex"
SomeStuff ISR_stuff;
SomeStuff working_stuff;


void ISR() {
  // there has been an interrupt. pdate ISR_stuff
  got_isr = true;
  ISR_stuff.count++;
  ISR_stuff.mostRecent = micros();

}

void loop() {
  noInterrupts();
  if(got_isr) {
    // copy the stuff
    memcpy(&working_stuff, &ISR_stuff, sizeof(SomeStuff));

    // reset the ISR stuff
    ISR_stuff.count = 0;
    got_isr = false;
  }
  interrupts();


  and now we do some work with 
  working_stuff - it contains whatever the ISR 
  put there since last time this loop was last looped


  
}
1 Like

I think I figured out this problem. I think I remember that the timer libraries are not compatible with the RTC1307 library.