Hello guys,

I have trouble understanding why I am not able to extract the data in real time using this code. I am not sure what is happening. I tried looking up the web, but I didn't find anyone who had this problem. Cattledog and jremington helped me to set up the sensor and everything with the code. Go to this link to see what we have done. here

So, coming to the problem

```
#define pii 3.1415926535
byte countsPerRevolution = 113;
float rpm_exponentialavg = 0, sincoeffavg = 0, coscoeffavg = 0, thirdOrder, freq;
float rpm, Inst_speed, sinval, cosval, sincomp, coscomp;
volatile unsigned long countTeeth = 0;
volatile boolean finishCount = false;
volatile long period = 0;
long copy_period;
volatile unsigned long time_last;
volatile unsigned long interruptCount;
unsigned long copy_interruptCount;
void setup()
{
Serial.begin(115200);
Serial.println("start...");
attachInterrupt(digitalPinToInterrupt(3), pulsePeriod, RISING);//interrupt on pin3
}
void loop()
{
while (1)//remove overhead of loop()
{
if (finishCount == true)
{
finishCount = false;//reset flag
detachInterrupt(0);
// disable interrupts, make protected copy of time values
copy_period = period;
copy_interruptCount = interruptCount;
attachInterrupt(0, pulsePeriod, RISING);
freq = 1000000.0 / copy_period;
rpm = (freq * 60) / 113; // to calculate the rpm of the flywheel
rpm_exponentialavg = rpm_exponentialavg * 0.99115044 + rpm * 0.00884956; // running average of the rpm being calculated
Inst_speed = rpm_exponentialavg - rpm; // to calculate the instantaneous speed of the flywheel for every tooth
sinval = sin(2 * pii * (float)countTeeth / 113.0); // to calculate the sine of angle between the teeth in radians.
cosval = cos(2 * pii * (float)countTeeth / 113.0); // to calculate the cosine of angle between the teeth in radians.
countTeeth++; // increment the counter for every tooth passing.
sincomp = sinval * Inst_speed; // to calculate the sine component to perform fourier transformation
coscomp = cosval * Inst_speed; // to calculate the cosine component to perform FT.
// to calculate the running averages of sine and cosine, we get
sincoeffavg = sincoeffavg * 0.99115044 + sincomp * 0.00884956;
coscoeffavg = coscoeffavg * 0.99115044 + coscomp * 0.00884956;
thirdOrder = 2 * sqrt(sincoeffavg * sincoeffavg + coscoeffavg * coscoeffavg) * (2 * pii / 60) * rpm_exponentialavg * 6 * pii / 60;
//debug prints
Serial.println(copy_period);
Serial.print(";");
Serial.print(copy_interruptCount);
}
}
}
void pulsePeriod()//simplify isr
{
period = (micros() - time_last);
time_last = micros();
finishCount = true;
interruptCount++;
}
```

When I print the interrupt count and pulse period. This is what I see.

```
Interrupt Count pulse period
1 464
4 464
6 468
8 468
9 468
11 452
14 448
16 436
18 428
20 420
22 416
25 416
27 416
29 424
32 424
34 436
36 444
38 452
40 460
42 468
45 468
47 464
49 460
51 448
53 440
55 432
57 420
60 420
62 412
64 416
67 420
69 428
```

If you see from the results it is clear that I am not able to extract data of each pulse periods. There is some difference between each interrupt count. I want to understand why this is happening. I anticipated that I will be able to extract each data in real time. That's not the case. Assuming my frequency is varying between 2200 to 3500hz. Arduino should be capable of doing it.

Thanks in advance for your help.

I really want some suggestions from you guys on this.

Suggestions recieved so far:

- Cattledog suggested me to buy new microcontroller with high clock speed.