Interrupt usage

Hello all,

I need a bit of advice if you could. My Arduino knowledge is very basic, I hope I am not asking a too silly question.

I am looking for a way to monitor a switch which, when pressed, would tell the Arduino to stop what it's doing and go to a different function/routine.

My project begins and does a signal calibration (audio and light measurement to assess the current levels) then moves to the main loop. I need to be able to press a button at any time to start the calibration sequence again.

Performance is critical as the Arduino is barely fast enough to do the sound/light measurements I need, I don't want to waste cycles polling a pin every time - if possible.

I have discovered the interrupt code but I understand I cannot run a whole function inside it, it's just for basic tasks. Another way would be to use the interrupt to just flag a boolean variable which then could be checked every time the main loop is starting again. This would delay a little the calibration but it may be acceptable.

Any ideas would be appreciated!
Thanks
Tony

I think they key to this is to make sure that the code you already have, doesn't do any blocking. Then you can be assured your calibration switch will be read frequently.

The below code is Blink without Delay as an example. The blinking is an analog of your main code, with no blocking, so the button can always be read pretty soon. Whenever the calibration button is seen to be pressed, the function doCal() is called.

/* Blink WITHOUT DELAY with a digitalRead() at the end of loop()
 
 Turns on and off a light emitting diode(LED) connected to a digital  
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.
 
 The circuit:
 * LED attached from pin 13 to ground.
 * Note: on most Arduinos, there is already an LED on the board
 that's attached to pin 13, so no hardware is needed for this example.
 
 
 created 2005
 by David A. Mellis
 modified 8 Feb 2010
 by Paul Stoffregen
 
 This example code is in the public domain.
 
 
 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
 */

// constants won't change. Used here to 
// set pin numbers:
const int ledPin =  13;      // the number of the LED pin
int button = 8;  //we'll pretend there's a button here, to read each time through loop()
                 //button is active low with pullups enabled
// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 500;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);  
  pinMode (button, INPUT_PULLUP); 
  Serial.begin(9600); 
  Serial.println("Starting Blink without Delay()....."); 
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the 
  // difference between the current time and last time you blinked 
  // the LED is bigger than the interval at which you want to 
  // blink the LED.
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }


  Serial.println(digitalRead(button));
  //check if calibration is needed
  if (!digitalRead(button)) doCal();
}

void doCal()
{
  Serial.println(" calibrating");
  delay(2500); //simulate the calibration thing happening
  Serial.println(" calibrated");
}

HTH?

Hi Jimbo,

Thanks, I appreciate your time in replying and writing the example code.

Yes, that could be a solution, my only issue is that the main loop will be waiting for sound and light triggers so it won't read the button while doing that.

One could just keep the button pressed - there is a 1 second timeout anyway. However since I posted I tried using the interrupt to just record that someone pressed the button so at the end of the main loop an IF checks if the button has been pressed while the Arduino was busy and redirects to the calibration section if that's true.

I feel I am happy with this solution to be honest. It is not far away from yours, it just gives the system that little extra responsiveness.

I then assume that moving away from the main loop using an interrupt - which would be the best case scenario - is not easy/recommended to do?

Thanks
Tony

Delta_G:
Keep it that way by also letting it check for triggers and timing so fast. Never make your program wait for anything.

Yeah that was pretty much my thinking. If the existing code is doing any blocking, you're screwed anyway.

Read your triggers same as I read the calibration button.

I did a speed test a while ago: a more-or-less empty loop() on an Uno goes 150k times a second.

//speed test

long startTime;
long runTime;
long counter=0;

void setup()
{
  startTime = millis();
  Serial.begin(9600);
  Serial.print("Start ");
  Serial.println(millis());
}

void loop()
{
  runTime = millis()- startTime;
  counter++;

  if (runTime >= 1000)
  {
    Serial.print("Looped ");
    Serial.print(counter);
    Serial.print(" times in ");
    Serial.print(runTime);
    Serial.print(" ms");
    while(1){
    };
  }

}