How to reset RPM to 0?

Hi everyone I’m a bit of a noob when it comes to coding but how would I reset RPM to 0? In the serial viewer it will start as 0 until the 4 pulses are detected then it will give the RPM but it will keep displaying this number for example 46, until the next 4 pulses are detected but I wish it to return to 0 until the next 4 pulses are detected.

int SprocketRatio=2.625;
int Gear1=2.286;
int Gear2=1.778;
int Gear3=1.500;
int Gear4=1.333;
int Gear5=1.214;
int Gear6=1.138;
int Primary=1.717;
int WheelCircum=1.953;
volatile byte rpmcount;
unsigned int rpm;
unsigned int RPM;
unsigned long timeold;
void setup()
 {
   Serial.begin(9600);
   attachInterrupt(0, rpm_up, RISING);
   digitalWrite(2, HIGH);
   rpmcount = 0;
   rpm = 0;
   timeold = 0;
 }

 void loop()
 {
  int sensorValue = analogRead(A0);
  float voltage=sensorValue*(5.0/1023.0);
  if (rpmcount >= 4) { 
     rpm = (7500*rpmcount)/(millis() - timeold);
     timeold = millis();
     rpmcount = 0;
   }
  if (voltage <1) 
  {
    RPM=rpm*WheelCircum*Gear1*Primary*SprocketRatio;
    Serial.print("Gear 1, RPM: ");
    Serial.println(RPM,DEC);
  }
  else if (voltage >=1.01 && voltage<=1.5) 
  {
    RPM=rpm*WheelCircum*Gear2*Primary*SprocketRatio;
    Serial.print("Gear 2, RPM: ");
    Serial.println(RPM,DEC);
  }
  else if (voltage >=1.51 && voltage<=2.0) 
  {
    RPM=rpm*WheelCircum*Gear3*Primary*SprocketRatio;
    Serial.print("Gear 3, RPM: ");
    Serial.println(RPM,DEC);
  }
  else if (voltage >=2.01 && voltage<=2.5) 
  {
    RPM=rpm*WheelCircum*Gear4*Primary*SprocketRatio;
    Serial.print("Gear 4, RPM: ");
    Serial.println(RPM,DEC);
  }
  else if (voltage >=2.51 && voltage<=3.0) 
  {
    RPM=rpm*WheelCircum*Gear5*Primary*SprocketRatio;
    Serial.print("Gear 5, RPM: ");
    Serial.println(RPM,DEC);
  }
  else if (voltage >=3.01) 
  {
    RPM=rpm*WheelCircum*Gear6*Primary*SprocketRatio;
    Serial.print("Gear 6, RPM: ");
    Serial.println(RPM,DEC);
  }
  }

 void rpm_up()
 {
   rpmcount++;
 }

int SprocketRatio=2.625;Do you understand what an int is ?

It can store a value between something like negative 30,000 and positive 30,000. Why?

It's an integer value, meaning it can only store whole numbers (e.g. 1, -5, 1024), not floating point values (e.g. 2.625, 3.14).

https://www.arduino.cc/en/reference/int
https://www.arduino.cc/en/reference/float

Pieter

Ah thank you, would this explain the RPM not resetting to 0 as well?

would this explain the RPM not resetting to 0 as well?

No, but it would explain why you are getting the wrong figures even if you have not noticed it.

As to resetting RPM to zero, can you please explain exactly what you want to happen ? Do you want the output to stop until another 4 pulses are received or are you happy for zero to be output until they are received ?

I'm happy for the output to be 0 until they are recieved

I'm happy for the output to be 0 until they are recieved

if (rpmcount >= 4)
{
  rpm = (7500 * rpmcount) / (millis() - timeold);
  timeold = millis();
  rpmcount = 0;
}
else
{
  rpm = 0;
}