Here's a code snippet to get you started.
#define IP1 2 // use your pin numbers here
#define IP2 3
#define RELAY 13
#define N_PULSES 40
void setup () {
pinMode (IP1, INPUT);
pinMode (IP2, INPUT);
pinMode (RELAY, OUTPUT);
digitalWrite (RELAY, LOW);
}
void loop () {
// wait for button to be pressed
// NOTE: input doesn't really need to be debounced
// assumes pull down resistor on switch
while (digitalRead(IP1) != HIGH) {};
// relay activated
digitalWrite (RELAY, HIGH);
for (int i = 0; i < N_PULSES; i++) {
// just use pulseIn() to count the pulses
// don't care about the pulse width
pulseIn (IP2, HIGH);
}
// relay deactivated
digitalWrite (RELAY, LOW);
// assumes the one-shot on IP1 is set to < the time N_PULSES takes
// if not then wait here for IP1 to go LOW again ie. button released
}
As you said a monostable on the button I'm assuming a short debounced pulse on IP1.
If it's really just a pushbutton the release of the buttton will have to be debounced.
Hope that will allow you to have a crack at it.
Rob