Counting pulses using attachInterrupt();

So I have attached a flow meter to my watering system, but because it's not the original one it gives way too many pulses compared to the original one. I'll have an Arduino there for other purposes too, so I decided I could fix this using the Arduino too.

I've been trying to make the Arduino output 1 pulse for every 100 pulses it recieves. I'm using the attachInterrupt(); function which then goes to a function, that increments a pulses value. All my loop function currently has is an if that checks, if the pulses value is over a specified amount, and if so makes a pulse. The problem is that I can't test it there, so I decided to output the pulse to the LED_BUILTIN, instead of the watering system controller and wired up a button instead of the flow meter.

Here is my current code, which neither works, nor can I really understand what's happening, because the value that I print to the Serial Monitor looks kinda random to me.

const int outputPulseLenght = 100;
const int pulseRatio = 100;
const int outputPin = 6;
const int interruptPin = 3;

volatile int pulses = 0;

void setup() {

  Serial.begin(9600);
  pinMode(outputPin, OUTPUT);
  digitalWrite(outputPin, LOW);
  attachInterrupt(digitalPinToInterrupt(interruptPin), countPulse, RISING);

}

void loop() {

  Serial.print("Pulses: ");
  Serial.print(pulses);

  if(pulses >= pulseRatio){

    pulse();
    pulses-=pulseRatio;
    
  }
  delay(200);
  
}

void countPulse() {

  pulses++;
  
}

void pulse() {

    digitalWrite(outputPin, HIGH);
    delay(500);
    digitalWrite(outputPin, LOW);
    
}

I'm not very sure that I wired it up correctly, but I used this circuit (https://www.arduino.cc/en/Tutorial/Button). Can you see any mistakes in what I did?

The countPulse() function is an ISR. It calls the pulse() function which calls delay(). You should not use delay in an ISR - and pulse() is part of the ISR.

Pete

You define "const int outputPulseLenght = 100;" but use a constant 500 when generating the output pulse.

Either make 'pulses' a single byte ('byte', 'uint8_t', or 'unsigned char') or add code to male a local copy of 'pulses' while interrupts are disabled. You don't want the value of 'pulses' to change between fetching the two bytes.