I'm trying to create an ISR which operates for 15 seconds when a particular button is pressed. I'm attempting to use the AVR functions on my Arduino Uno to start counting when it enters the ISR, then quit the loop when it reaches 15 seconds.

I'm having difficulty reasoning out where the counter is actually stored that I want to use for my while loop.

Here is my code so far:

#define button_pin 2
#define duration 15
#define osc_in 0

#include <avr/io.h>
#include <avr/interrupt.h>

const float convert = 5.0/1023.0;

void setup() {

  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1 = 0;
  attachInterrupt(digitalPinToInterrupt(button_pin), button_interrupt, CHANGE);


  pinMode(button_pin, INPUT);

void loop() {
    //int channel1 = analogRead(osc_in);

//while (millis() < 10000)
  //Serial.println(channel1 * convert);
  //channel2 = channel1;

void button_interrupt(){
  //(# timer counts + 1) = (target time) / (timer resolution)
  //int timer_counts = duration/(pow(6.4, -5));
  //int dur = 234374;
  OCR1A = 234374;
  TCCR1B |= (1 << CS10);

  while (????????){
    int channel1 = analogRead(osc_in);
    Serial.println(channel1 * convert);
  • Doing serial inside an ISR is typically a bad idea. Maybe consider doing your sampling continuously, and using an ISR to set a flag and initiate a 1-shot timer. The the loop code could check the flag and do the 9600 baud serial reporting as needed.
    – Dave X
    Commented Mar 12, 2016 at 16:17

1 Answer 1


Don't do delays inside an ISR. Don't do serial prints inside an ISR. Read this.

You need to redesign. The ISR can set a flag, that you then test in loop. Do the delay in loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.