Flex sensors with a timer interrupt

Hello Everyone,

My question is ;

How to read flex sensors every 100 ms with using a timer interrupt instead of delay ?

I found some tutorials but I couldn’t figure it out. here is the one of them :

http://www.engblaze.com/microcontroller-tutorial-avr-and-arduino-timer-interrupts/

Thanks !

Simple:

int interrupt = 0;
SIGNAL(TIMER0_COMPA_vect) { // Timer to check every millisecond
  sei(); // allow other interrupts to occur
  static int MSec = 0;
  if (MSec++ < 10)return; // Check every 10 Milliseconds for change
  MSec = 0;
  interrupt = 1;
  // Put your code here only put as little as possible no Serial.print stuff
}

void setup()
{
  Serial.begin(115200);
  Serial.println();
  OCR0A = 0xAF;           //Where the interrupt will occure in the microseconds counter Google OCR0A Arduino for details
  TIMSK0 |= _BV(OCIE0A);  // Enable the timer interrupt
}

void loop()
{
  // Other code to respond to interrupt
  if (interrupt == 1) {
    interrupt = 0;
  }
}

This should give you the ideas and help others :slight_smile:
Z

100ms is a long time. No need for an interrupt or delay. Just check millis. Anytime it is more than 100 ahead of where it was last time your read the sensor then read again. See the Blink Without Delay example in the IDE or on the net for an example code.

First of all , Thank you guys,

Before trying timer interrupt, I used delay function and "milis". It works fine. But I should use "timer interrupt ".

thank you for these ideas !

ezgi:
First of all ,
Thank you guys,

Before trying timer interrupt, I used “milis”. It works fine.
But I should use "timer interrupt ".

thank you for these ideas !

Hi ezgi. Welcome to the forum.

A question: Why should you use an interrupt?

I have found that interrupts have advantages and disadvantages. They are great for handling pin events that are unpredictable, very short, or or that require very precise timing. They suck for anything that actually DOES something besides changing a variable or two.