Trying to undestand interrupts

I made this sketch that is meant to read the period of a cycle.
An interrupt does a time reading when a cycle begins and attaches the second interrupt that was supposed
to substract the current time from the first reading.
However it does not work, what am I missing?

"THROprev_time" works a bit in the begining and then it shows random numbers, even negative numbers.
THROvalue is showing mostly 0s.

Thank you community

_2Interrupt_Study.ino (685 Bytes)

In the SecondRead() function you do not detach the interrupt before attaching another one. Could be a problem.

Why have you got 2 ISRs ? Increment a counter in a single ISR and you will know whether it is the first or second trigger.

Printing in an ISR is regarded as dodgy at best because interrupts are disabled in ISRs and printing uses interrupts.

Have a look at this version

/*Arduino Mega interrupt Table
  Interupt 0- Pin2
  Interupt 1- Pin3
  Interupt 2- Pin21
  Interupt 3- Pin20
  Interupt 4- Pin19
  Interupt 5- Pin18
*/


volatile unsigned long isrMIllis;
volatile bool newValue = false;

unsigned long prevIsrMillis;
unsigned long latestIsrMillis;


void setup() {
  Serial.begin(9600);
  attachInterrupt(4, FirstRead, RISING);

}

void loop() {
   if (newValue == true) {
      prevISRmillis = latestIsrMillis; // remember the value
      noInterrupts();   // pause interrupts while getting the latest value
         latestIsrMillis = isrMillis;
         newValue = false;
      interrupts();  // interrupts back on
      Serial.print("timeMillis ");
      Serial.println(latestIsrMillis - prevIsrMillis);

}


void FirstRead() {

  isrMillis = millis();
  newValue = true;

}

Note how there is no printing inside the ISR - printing needs interrupts enabled and also is very slow.

I have not tested this and I'm not sure if it does exactly what yo want - but it should illustrate how to use an interrupt

...R

You are saving a 32-bit unsigned long in a 16-bit int. After 32.767 seconds you start getting negative numbers.

ALWAYS use 'unsigned long' with millis() and micros().

Robin, thanks a lot,

Your sketch does not compile, very weird. i fixed a couple of brakets and names, but:

Arduino: 1.6.12 (Mac OS X), TD: 1.36, Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

/Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/../lib/gcc/avr/4.9.2/../../../../avr/bin/ar: dlopen(/Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/../libexec/gcc/avr/4.9.2/liblto_plugin.so, 2): Symbol not found: _environ
Referenced from: /Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/../libexec/gcc/avr/4.9.2/liblto_plugin.so
Expected in: /usr/lib/libSystem.B.dylib

There is a ton of other errors like this...

Any ideas? I attached your skech back , i modified just some val names

Period_Read01_Robin.ino (820 Bytes)

/*Arduino Mega interrupt Table
  Interupt 0- Pin2
  Interupt 1- Pin3
  Interupt 2- Pin21
  Interupt 3- Pin20
  Interupt 4- Pin19
  Interupt 5- Pin18
*/


volatile unsigned long isrMillis;
volatile bool newValue = false;

unsigned long prevIsrMillis;
unsigned long latestIsrMillis;


void setup() {
  Serial.begin(9600);
  attachInterrupt(4, FirstRead, RISING);

}

void loop() {
   if (newValue == true) {
      prevIsrMillis = latestIsrMillis; // remember the value
      noInterrupts();   // pause interrupts while getting the latest value
         latestIsrMillis = isrMillis;
         newValue = false;
      interrupts();  // interrupts back on
      Serial.print("timeMillis ");
      Serial.println(latestIsrMillis - prevIsrMillis);

      }
}


void FirstRead() {

  isrMillis = millis();
  newValue = true;

}

Just POST the code.

This compiles fine on Ubuntu / IDE 1.6.12

I am on Mac 10.75 IDE 1.8.4

Still, does not work...

I copy pasted your code.

Any ideas?

thanks

I copy pasted your code.

It's your code - I simply posted it so that others can see it.

I rebooted and the code actually worked.

Thank you Robin2

Mitch