Comparing two output

hello everyone
i'm trying to compare current output with last one.

the outputs are capturing by an interrupt.

for example for outputs :
1505.55
1505.69
1505.42
1512.95
1510.86
.
.
.
these outputs are repeating

this is my code :
<

//----------------------------------------------------------------

volatile float Tx = 0 ;

volatile float f = 0 ;

volatile float t = 0 ;
 
volatile int m = 0 ;

void setup() {
 
  Serial.begin(19200);
   Serial3.begin(38400);

  capture_pin2.config(CAPTURE_TIME_WINDOW);
attachInterrupt(digitalPinToInterrupt(2), measure , LOW );

}

void measure (){
  uint32_t status,duty,period;
  status=capture_pin2.get_duty_and_period(duty,period);

 t = (static_cast<double>(duty)/static_cast<double>(capture_pin2.ticks_per_usec()) );
  if ( (t > 1400) && (t < 1600))
{  Tx = t ;}

 }
void loop() {

if (Tx - f > 5)
{
  
 f = Tx;
 
  m++;

 
}
  delay(20) ;

Serial.println(m); 

//-------------------------------------------------------------------

but it doesn't work . it keep giving 1 in serial port

any little help is greatly appriciated in advance.

Please follow the advice given in the link below when posting code. Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Have you tried printing he values that you are comparing ? Are they what you expect ?

1504.952
1513.857
1516.905
1515.31
1516.119
1511.881
1509.667
1512.405
1509.619
1510.595
1510.952
1509.143
1510.119
1509.929
1511.119
1509.571
1509.5
1510.357
1509.048
1509.738
1508.405
1508.571
1508.738
1508.262
1508.976
1509.024
1510.071
1507.857
1508.548
1508.095
1508.405
1509.143
1508
1508.19
1509.071
1507.905
1506.619
1507.429
1507.905
1506.595
1506.31
1505.286

these are value of period and it is repeating

when i change the

Serial.println(m);

to 

Serial.println(Tx);

the Tx is completely normal as it's expected

The values printed are meaningless without text labels indicating which value is being printed and you have not said whether the values are what you expect

I don’t see 1 in serial port

now i just put

 Serial.println("------------------------"); 
  Serial.println(Tx); 
  
  Serial.println(f); 
Serial.println(m); 

and noticed that the output is like this :

1476.62
1476.62
1

1476.62
1476.62
1

1476.62
1476.62
1

1476.62
1476.62
1

1476.62
1476.62
1

but when i compile below code

volatile float Tx = 0 ;

volatile float f = 0 ;

volatile float t = 0 ;
 
volatile int m = 0 ;

void setup() {
 
  Serial.begin(19200);
   Serial3.begin(38400);

  capture_pin2.config(CAPTURE_TIME_WINDOW);
attachInterrupt(digitalPinToInterrupt(2), measure , LOW );

}

void measure (){
  uint32_t status,duty,period;
  status=capture_pin2.get_duty_and_period(duty,period);

 t = (static_cast<double>(duty)/static_cast<double>(capture_pin2.ticks_per_usec()) );
  if ( (t > 1400) && (t < 1600))
{  Tx = t ;}

 }
void loop() {

  delay(20) ;

Serial.println(Tx); 

//-------------------------------------------------------------------

Tx keep changing.

do you have any idea why it doesn't change when i compile the main code that's mentioned at the begining.

the list of numbers is for Tx not for m.

Let's go back to basics

Which Arduino board are you using and what is connected to pin 2 to cause the interrupts ?

i'm using due and a pulse generator is connected to pin 2 .
when the output goes below 1506 then the generator will change the output and go above 1512
image

Neither code will compile. I don't have to try it, I can easily see it won't compile just by reading it. You waste our time.

by those i mean when i put " f " for saving the last output , the current output doesn't change . i understood it when i print " f " and " Tx " and that's why the counter doesn't increase .
and now the question is " why the output " Tx " doesn't change when i put " f " for saving last one in it and compare with current one " Tx" , while when i omit " f " from the code the current output start to change ?!?!"

Tx - f is never > 5 from what you posted so m++ never executed

if you take a look at the figure that i have posted , you can see that the value of " Tx " jumps from 1506 to 1516 at spikes. so the current value (" Tx ") is bigger than the last one (" f ") at spikes
and i want to count the spikes.

You posted a lot of stuff, just not waht is relevant. Post the Tx and f where it shows Tx changing, then we talk

in this code Tx changes :

volatile float Tx = 0 ;

volatile float f = 0 ;

volatile float t = 0 ;
 
volatile int m = 0 ;

void setup() {
 
  Serial.begin(19200);
   Serial3.begin(38400);

  capture_pin2.config(CAPTURE_TIME_WINDOW);
attachInterrupt(digitalPinToInterrupt(2), measure , LOW );

}

void measure (){
  uint32_t status,duty,period;
  status=capture_pin2.get_duty_and_period(duty,period);

 t = (static_cast<double>(duty)/static_cast<double>(capture_pin2.ticks_per_usec()) );
  if ( (t > 1400) && (t < 1600))
{  Tx = t ;}

 }
void loop() {
 

  delay(20) ;

Serial.println(Tx); 
}

but in this code Tx doesn't change

volatile float Tx = 0 ;

volatile float f = 0 ;

volatile float t = 0 ;
 
volatile int m = 0 ;

void setup() {
 
  Serial.begin(19200);
   Serial3.begin(38400);

  capture_pin2.config(CAPTURE_TIME_WINDOW);
attachInterrupt(digitalPinToInterrupt(2), measure , LOW );

}

void measure (){
  uint32_t status,duty,period;
  status=capture_pin2.get_duty_and_period(duty,period);

 t = (static_cast<double>(duty)/static_cast<double>(capture_pin2.ticks_per_usec()) );
  if ( (t > 1400) && (t < 1600))
{  Tx = t ;}

 }
void loop() {

if (Tx - f > 5)
{
  
 f = Tx;
 
  m++;

 
}
  delay(20) ;

Serial.println(m); 

i don't see a print in the 2nd version of the code

i think the ISR is doing too much. with delays in loop() there's no guarantee that loop() is seeing all the captured values.

why not have the ISR capture values in a buffer, incrementing and wrapping an index with each capture? loop() would have it's own index and process samples from the buffer whenever the indices differ.

Second code has no closing bracket at the end, so I very much doubt anything is changing there. I asked you to post output where TX is changing together with f value:

Tx: TxValuewhichischanging
f: fValue

If you not going to, I don't think I can help.

the closing bracket is missed here but in this code Tx will change correctely :


#include "tc_lib.h"

using namespace arduino_due;

#define CAPTURE_TIME_WINDOW 2500000 // usecs
#define ANALOG_PIN 7
#define ANALOG_VALUE 127 // values in the interval [0,255] 


capture_tc0_declaration();
auto& capture_pin2=capture_tc0;
volatile float Tx = 0 ;

volatile float f ;

volatile float t = 0 ;
 
volatile int m = 0 ;

void setup() {
 
  Serial.begin(19200);
   Serial3.begin(38400);

  capture_pin2.config(CAPTURE_TIME_WINDOW);
attachInterrupt(digitalPinToInterrupt(2), measure , LOW );

}

 void measure (){
  uint32_t status,duty,period;
  status=capture_pin2.get_duty_and_period(duty,period);

 t = (static_cast<double>(duty)/static_cast<double>(capture_pin2.ticks_per_usec()) );
  if ( (t > 1400) && (t < 1600))
{  Tx = t ;}


 }
void loop() {


  delay(20);
  
Serial.println(Tx);
}

but at the moment i put " f " in it to save the value of " Tx " , it stops changing
the code is here for comparing " f " and " Tx " :


#include "tc_lib.h"

using namespace arduino_due;

#define CAPTURE_TIME_WINDOW 2500000 // usecs
#define ANALOG_PIN 7
#define ANALOG_VALUE 127 // values in the interval [0,255] 


capture_tc0_declaration();
auto& capture_pin2=capture_tc0;
volatile float Tx = 0 ;

volatile float f ;

volatile float t = 0 ;
 
volatile int m = 0 ;

void setup() {
 
  Serial.begin(19200);
   Serial3.begin(38400);

  capture_pin2.config(CAPTURE_TIME_WINDOW);
attachInterrupt(digitalPinToInterrupt(2), measure , LOW );

}

 void measure (){
  uint32_t status,duty,period;
  status=capture_pin2.get_duty_and_period(duty,period);

 t = (static_cast<double>(duty)/static_cast<double>(capture_pin2.ticks_per_usec()) );
  if ( (t > 1400) && (t < 1600))
{  Tx = t ;}


 }
void loop() {
if (Tx - f > 5)
{
  
 f = Tx;
 
  m++;

}
  delay(20);  
 Serial.println(m);
}

you mean delay(20) is interfering ??
in fact " m " doesn't change ever because the " Tx " doesn't come from the " measure interrupt " when i put " f " to store the previous value of " Tx " in it.