Doing a simple calculation not working.

Hi every one,
I am trying to do a simple calculation but not getting the answer I am expecting. I don't know whether this is the correct section of the forum, feel free to move if not.

Here is my code reading a reed witch activated by a magnet on a shaft.

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

  volatile byte rpmcount;
  unsigned int time;
  unsigned int rpm;
  unsigned long timeold;

 void setup()
 {
  Serial.begin(9600);
    attachInterrupt(0, rpm_fun, FALLING);

    rpmcount = 0;
    rpm = 0;
    timeold = 0;
    time = 0;
   
 }

 void loop()
 {
  if (rpmcount >= 20) { 
     //Update RPM every 20 counts
     
     detachInterrupt(0);
     
     Serial.print("rpmcount");
     Serial.println(rpmcount);
     
     Serial.print("micros");
     Serial.println(micros());
     
     Serial.print("timeold ");
     Serial.println(timeold);
     
     time = micros() - timeold;
     Serial.print("Time ");
     Serial.println(time);
     
     timeold = micros();
     rpmcount = 0;
    
     attachInterrupt(0, rpm_fun, FALLING);
   }
 }

 void rpm_fun()
 {
   rpmcount++;
  
 }

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

I expected time = micros() - timeold; to work like this if in simple terms micros()=25 and timeold=20 then time =5
However here is a sample from my serial monitor;

rpmcount 20
micros()   8709992
timeold    8555748
time =     24032

according to my calculation time should = 154244

Unfortunately I can't seem to copy paste the serial monitor results but all the readings follow the same pattern

I would very much appreciate some help. Do calculations have to done in hex or binary?
Thank's
EDIT have now been able to get all the readings from the serial monitor

rpmcount20
micros8072928
timeold 0
Time 12552
rpmcount20
micros8196636
timeold 8073820
Time 58140
rpmcount20
micros8308436
timeold 8197832
Time 45924
rpmcount20
micros8416472
timeold 8309628
Time 42168
rpmcount20
micros8554604
timeold 8417668
Time 6716
rpmcount20
micros8709992
timeold 8555748
Time 24032
rpmcount20
micros9081688
timeold 8711192
Time 43676
rpmcount20
micros9776944
timeold 9082892
Time 3955

Change int to long :slight_smile:

You need to change it to long because an unsigned int only can store a max value 65535.
You are overrun its value

according to my calculation time should = 154244

The result is bigger than 65535

I changed to long and this was the monitor results

rpmcount20
micros13221452
timeold 0
Time 13222052
rpmcount20
micros13351148
timeold 13222544
Time 129564
rpmcount20
micros13460384
timeold 13352492
Time 108852
rpmcount20
micros13590376
timeold 13461736
Time 129604
rpmcount20
micros13711664
timeold 13591728
Time 120896
rpmcount20
micros13816324
timeold 13713012
Time 104272
rpmcount20
micros13954612
timeold 13817668
Time 137904
rpmcount20
micros14289420
timeold 13955956
Time 334424

taking this section it is getting much nearer thanks to the int long suggestion

rpmcount20
micros13590376
timeold 13461736
Time 129604

but my calculator answers is 128640 a difference of 964
and

rpmcount20
micros13816324
timeold 13713012
Time 104272

a difference of 960

Where would this difference be coming from?

Time is passing as your sketch runs. You are calling micros() twice and expecting to get the same answer - as you can see, you don't, because 960 microseconds have passed. If that's what you want, store micros in a variable and use that for the print and the subtraction.

A little effort formatting the output would be useful, too. Not that difficult, either.

First a very big thank you guys. And yes I should of formatted the output.

I have done as you suggested wildbill and have got accurate readings.

I intended taking 20 divided by time and multiplying by 60000000 to get RPM however when I divide 20 by time I get 0 I will post code and output tomorrow as the pc linked to Arduino has no internet connection and keyboard. Also my GF thinks it a bit strange that I keep going to work and back every 10 minuets.

I intended taking 20 divided by time and multiplying by 60000000 to get RPM however when I divide 20 by time I get 0

Integer maths is different from floating point maths. 20 divided by anything bigger than 20 is zero (the integer part of the float). You need to plan your calculation carefully. In this case try dividing 60000000 by time and multiplying by 20 (should be the same equation).