Need help with photo interrupters

Nothing happens when I rotate my wheel through my photo interrupter. Blank serial monitor. ANy ideas?

volatile byte revolutions; //this is the revolution counter
unsigned int rpm; //revolutions per minute
unsigned long timeold; //needed for the differantial time

void setup() {
  
Serial.begin(9600);

attachInterrupt(2, rev_counter, RISING);

revolutions=0; 
rpm=0;
timeold=0;

}

void loop() {
  if (revolutions >= 1) { //Calculates rpm after every 1 revs.

    rpm = 60000 / (millis() - timeold); 
    timeold=millis();
    revolutions=0;
    
    Serial.print(rpm,DEC);
    Serial.print('\n');
    } 
}

void rev_counter() {
  
revolutions++;  
}
attachInterrupt(2, rev_counter, RISING);

I'm no sure where interrupt pin 2 is on a Mega, but I'm pretty sure it's not on pin 21.

Always use the digitalPinToInterrupt() function...

attachInterrupt(digitalPinToInterrupt(2), rev_counter, RISING);

That way it's obvious which pin you intended to connect to.

Also, the plastic you used may be completely transparent to infrared. Paint it black or something.

Hi thanks for the reply. I am following this link and it tells me that it is attached to pin 21.

Painted it black still nothing happening...

Try whether your sensor works by using some piece of aluminium foil that you hold in between and then remove. This should make the pin rise. You can also check the output of your sensor with a multi meter. You should be able to pick up the signal that way as well.

In your code you count "revolutions" but your disk seems to have many openings, so about 20 counts (I didn't count the slits) would make for one revolution, not just 1 count.

I changed it to 20 but still nothing is happening. The only time I get something on my serial monitor is when I wiggle the wire to pin connection 21, giving me a constant value of 65535.

Did you test the sensor itself properly as I suggested? Like testing the output with a multimeter, blocking it with something like alu foil to really block it... It sounds like you're having wiring issues.

I believe pin 21 is INT0, INT2 is pin 19.

I don't see any current limit resistors on your picture, you may have fried your photo interrupter already, can you post a link or part number?

I tried pin 21 and it doesn't work. I managed to make the code work by changing it a bit and adding resistors. However, it still gives me values which I don't think is possible.

volatile byte revolutions; //this is the revolution counter
unsigned int rpm; //revolutions per minute
unsigned long timeold; //needed for the differantial time

void setup() {
  
Serial.begin(9600);
attachInterrupt(0, rev_counter, RISING);

revolutions=0; 
rpm=0;
timeold=0;

}

void loop() {
  if (revolutions >= 24) { //Calculates rpm after every 20 revs.

    rpm = 60000 / (millis() - timeold); //there are 20 holes on the encoder wheel
    timeold=millis();
    revolutions=0;
    
    Serial.print(rpm,DEC);
    Serial.print('\n');
    } 
}

void rev_counter() {
  
revolutions++;  
}

if (revolutions >= 24) { //Calculates rpm after every 20 revs.

How many?

Serial.print('\n');

There's also Serial.println() which adds the \n automatically.

Code otherwise looks sound. What values do you get and what do you expect?