Program seems to only run with serial monitor open

int thermistorPin = A0;

// Variables for translating thermistor-data to degrees in Celsius
float R1 = 10000;
float T0 = 298.15;
float Rntc, temp, temp_c;
float beta = 3486;

float temperatureInC(int pinNumber){
/* This piece of code does the translation between analog data and degrees in Celsius
- based on the Steinhart-Hart-equation */

  int voltage; 
  voltage = analogRead(pinNumber); 
  Rntc = R1 * (1023.0 / voltage - 1.0);
  temp = 1 / T0;
  temp += 1/beta * log(Rntc/R1); 
  temp = 1 / temp;
  temp = temp - 273.15;

  return temp;

float T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12;
int hvile = 10000; // Variable for easy delay adjustments between measurements
volatile int pinKnapp = 2; // Button pin
int printVerdi = 1;

void knappISR(){
  // Function to be run as an interrupt
  Serial.print("Måling én: ");
  Serial.print("Måling to: ");
  Serial.print("Måling tre: ");
  Serial.print(" ^ ");
  Serial.print("print nr.");
  Serial.println(" ^");

/* Following is a function that applies a delay after calling the attachInterrupt-code to
prevent fully-automatic printing to the serial monitor */
static unsigned long aktuell_millis = 0;
void ISRdebounce(){
  if(millis() - aktuell_millis > 1000){
    aktuell_millis = millis();

void setup() {
  Serial.println("Temeperaturmålinger i Celsius, gjort over 24 timer hver andre time"); 
  // The previous means: "measurements in Celsius over a 24-hour span, done every other hour" and is purely for aesthetics
  pinMode(pinKnapp, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(2), ISRdebounce, LOW);

  T1 = temperatureInC(thermistorPin);
  T2 = temperatureInC(thermistorPin);
  T3 = temperatureInC(thermistorPin);

void loop() {

I am norwegian so some of the variables are named with norwegian words such as "knapp" which means "button".

What I am trying to achieve is a program that can run by a battery (no computer connected) and define the different T-foats (T1, T2, T3 etc...), so that when a computer is connected to the arduino, the data can be printed in the serial monitor.

What I have noticed is that when I run the program on the arduino without the serial monitor opened (whether it is powered by a battery or computer), then connect the arduino to the computer, open the serial monitor and run the ISR, there are no stored data. It runs the program first when the monitor is opened.

I have just little over a month of experience with programming, this being in school-context. I can not seem to find where the problem lies. The only thing I could think of, is that starting the serial in voi setup caused it. I tried to solve this by moving Serial.begin() inside the interrupt-code (knappISR()) and ending the code with Serial.end(), and as the problem was still there, I concluded that it had to be something else.

Thanks. -Richard

(1) Don't print when in an ISR as interrupts are disabled and Serial uses interrupts
(2) Opening the Serial monitor will reset most Arduinos

Serial.print() won’t work in an interrupt.

Serial.print has to wait for the serial hardware interrupt to empty the buffer, so if you call it
from another ISR it will jam up the system - you've hit deadlock as the serial ISR can't run till the
current ISR exits, but the current ISR is in the innards of the serial code that waits for the output
buffer to drain via the serial ISR - this is called "deadlock".

Similarly don't call delay() in an ISR.

You shouldn't be doing anything heavyweight in an ISR, keep it short as poss.

Thanks a lot for the replies.

I now wonder if there are any ways to work around this? For example: to make the ISR call another piece of code that prints to the monitor, or would this maybe change absolutely nothing?

Newbie here, that gladly take any (constructive) feedback!

or would this maybe change absolutely nothing?

Correct. No change

Set a flag variable in the ISR

isrHasRun = true;

Then do the printing in loop() and set the flag variable to false.