counting PWM to trigger an outputs

HI All,
I am new to coding and so far successfully managed to code a nano to trigger a relay followed by a servo motor with delay.
However, i would like to this to trigger from a PWM input on pin 12 count to a predetermined number (approx 150 pulses) and then trigger the below code.
The “button” in the code below was only to try out pulses and count codes from various other projects.
I will still have this button in the project later just to trigger the servo without the relay.
any suggestions where to start or how to achieve this.

#include <Servo.h>
const int buttonPin = 2; //button input on pin 2
const int ledPin = 13; //led on pin 13
const int relayPin = 3; // relay on pin 3
const int pwm = 12; //pwm input

Servo servoA;
int pos = 0;
int OPEN = 2000;
int buttonState = 0;

void setup() {
// inititialize Output Pins:
pinMode(relayPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(pwm, INPUT);


void loop() {
pulseIn(buttonPin, HIGH);

buttonState = digitalRead(buttonPin);
if (buttonState == LOW){
 digitalWrite(LED_BUILTIN, HIGH);
 digitalWrite (relayPin, HIGH);
} else {
 digitalWrite(LED_BUILTIN, LOW);
digitalWrite (relayPin, LOW);


The best way would probably be to have each pulse of your PWM to trigger an interrupt and do the counting in there. Here is an excellent example code that acomplishes that in this thread.