Reset rpm counting

So i’m making a project for school with an arduino and IR sensors with the objective of reading rpm on a disc with holes (it’s going to be applied to a gear box), and i want to reset the rpm counter at a certain point, for example, count 130 holes and reset the counting, can someone help me please? it’s important. Thanks.

int ledPin = 13;                
volatile byte rpmcount1;
unsigned int rpm1;
unsigned long timeold1;

volatile byte rpmcount2;
unsigned int rpm2;
unsigned long timeold2;


#include <LiquidCrystal.h>

LiquidCrystal lcd (8, 13, 9, 4, 5, 6, 7);

void rpm_fun1()
 {
   
      

  rpmcount1++;
      
 }

void rpm_fun2()
 {
   
 
   rpmcount2++;
 }

void setup()
 {
   lcd.begin(16, 2); 
   Serial.begin (9600);

   
   attachInterrupt(0, rpm_fun1, FALLING);
   attachInterrupt(1, rpm_fun2, FALLING);

  
   pinMode(ledPin, OUTPUT);
   digitalWrite(ledPin, HIGH);

   rpmcount1 = 0;
   rpm1 = 0;
   timeold1 = 0;

   rpmcount2 = 0;
   rpm2 = 0;
   timeold2 = 0;

   
   attachInterrupt(0, rpm_fun1, FALLING);
   attachInterrupt(1, rpm_fun2, FALLING);
   
   
   pinMode(ledPin, OUTPUT);
   digitalWrite(ledPin, HIGH);

   rpmcount1 = 0;
   rpm1 = 0;
   timeold1 = 0;

   rpmcount2 = 0;
   rpm2 = 0;
   timeold2 = 0;
 }

 void loop()
 {
  
   delay(300);
  
   detachInterrupt(0);
   detachInterrupt(1);
   
   rpm1 = ((49.455*1000/(millis() - timeold1)*rpmcount1)*2)*1.92;
   timeold1 = millis();
   rpmcount1 = 0;
   rpm_fun1;

  
   rpm2 = ((49.455*1000/(millis() - timeold2)*rpmcount2)*2)*130;
   timeold2 = millis();
   rpmcount2 = 0;
   rpm_fun2;

   lcd.clear();
   lcd.setCursor(0,0);
   lcd.print("RPM1= ");
   lcd.print(rpm1);
   lcd.setCursor (0,1); 
   lcd.print(rpm2);
   Serial.print ("RPM1 = ");
   Serial.print(rpm1);
   Serial.print ("\n");
   Serial.print ("RPM2 = ");
   Serial.print (rpm2);
   Serial.print ("\n");
 
   attachInterrupt(0, rpm_fun1, FALLING);
   attachInterrupt(1, rpm_fun2, FALLING);
  }

Of course you should post the code.

This sounds like something that calls for a simple if statement?

Welcome to the forum. Please read this link on how to use the forum http://forum.arduino.cc/index.php/topic,148850.0.html to help you get started.

I can share the code if it makes it easy to help. Thanks

Yes, you will need to post your code in order to get help. Read in the reference link about how to use the code tags

so your code appears in a box like this.

Be sure to let us know what you want the code to do and what it is actually doing instead.

DrAzzy:
Of course you should post the code.

This sounds like something that calls for a simple if statement?

I’ve tried an if, and it didn’t work.

pedromoreira13: I've tried an if, and it didn't work.

There are probably 100 billion ways to use an if statement. Honestly, how on earth can we tell which one of those it is? Post your code, as requested. Please use code tags when you do.

aarg: There are probably 100 billion ways to use an if statement. Honestly, how on earth can we tell which one of those it is? Post your code, as requested. Please use code tags when you do.

The code is already there, i edited the post.

Well, you already know how to reset the RPM count, you did it here:

   rpmcount1 = 0;

So what is the actual problem?

aarg: Well, you already know how to reset the RPM count, you did it here:

   rpmcount1 = 0;

So what is the actual problem?

I want to see the reset on the display, what i mean is, everytime the 130th hole passes the IR sensor, the counting goes back to zero

Why are you attaching the interrupt handlers twice? Why are you attaching the interrupt handlers twice?

Why are you detaching the interrupt handlers at all? Stop doing that. Use noInterrupts() and interrupts() around the code that copies/resets variables used in the handlers.

The reason that if statements don't work in your code is because there are none.

First fix the 25 or so repeated lines in your code (BTW if you can't see that before posting it, we haven't got much hope here, because it means you're not really reading it).

There are copied lines because i have 2 sensors doing 2 different things.

pedromoreira13: There are copied lines because i have 2 sensors doing 2 different things.

Okay, have it your way. Have a nice day.

PaulS: Why are you attaching the interrupt handlers twice? Why are you attaching the interrupt handlers twice?

Why are you detaching the interrupt handlers at all? Stop doing that. Use noInterrupts() and interrupts() around the code that copies/resets variables used in the handlers.

The reason that if statements don't work in your code is because there are none.

Explain this detaching thing please, i know little about Arduino's

There are copied lines because i have 2 sensors doing 2 different things.

There are two external interrupt pins. If you think you can use them for two different sensors, doing two different things, you are, sadly, mistaken.

Explain this detaching thing please, i know little about Arduino's

If you know little about Arduinos, then using interrupts should not be something you are doing, yet.

It really isn't hard to see that you are detaching the interrupt handlers. After all, what else would detachInterrupt() be foing?

You don't suppose that the various timers that the Arduino has detach and reattach the handler for each tick, do you? They do not. Instead, they disable (not detach) interrupts briefly, while they copy/reset data, and then enable (not attach) interrupts again.

The absolute minimum should be done with interrupts disabled.

PaulS:
If you know little about Arduinos, then using interrupts should not be something you are doing, yet.

It really isn’t hard to see that you are detaching the interrupt handlers. After all, what else would detachInterrupt() be foing?

You don’t suppose that the various timers that the Arduino has detach and reattach the handler for each tick, do you? They do not. Instead, they disable (not detach) interrupts briefly, while they copy/reset data, and then enable (not attach) interrupts again.

The absolute minimum should be done with interrupts disabled.

So i have to eliminate all of the interrupts?

So i have to eliminate all of the interrupts?

No. You need to explain what two sensors, doing two different things, you are trying to use. You need to use interrupt handlers appropriately, leaving them attached at all times.

PaulS: No. You need to explain what two sensors, doing two different things, you are trying to use. You need to use interrupt handlers appropriately, leaving them attached at all times.

I'm trying to measure the RPM of a gear with 130 teeth, and i used a vinil disc to the prototype, I drilled it with the 130 holes, and a few inches forward i drilled 1 hole, the porpuse of this is to measure the rpm with the 130 holes in one sensor, and reset the count each time the lonely hole passes the second sensor

the porpuse of this is to measure the rpm with the 130 holes in one sensor, and reset the count each time the lonely hole passes the second sensor

So, the two sensors are not encoders, so they will trigger one interrupt per hole. So, they should be attached to ONE external interrupt pin each.

The interrupt handlers should have meaningful names, like countPulse() and resetCount(), shouldn't they? Neither of them has anything to do with calculating rpm. THAT is done in loop().

Once the functions have meaningful names, I think it is much more likely that you can see where to reset the counter.

Since the counter will only get to 130, there is nothing to worry about in loop() when you access pulseCounter (which is what you WILL rename the counter to).

timeold1 does not tell us ANYTHING about what the time means. lastRPMCalculation, on the other hand, DOES.

LiquidCrystal lcd (8, 13, 9, 4, 5, 6, 7);
int ledPin = 13;

It is not going to work with both the led and the lcd on pin 13.

rpm1 = ((49.455*1000/(millis() - timeold1)*rpmcount1)*2)*1.92;
rpm2 = ((49.455*1000/(millis() - timeold2)*rpmcount2)*2)*130;

Can you please explain these two calculations. They certainly do not give rpm.