#include <Servo.h>
#include <digitalWriteFast.h>
Servo myservo;
uint32_t startsecond;
uint32_t newsecond;
uint8_t pulses;
void setup() {
Serial.begin(115200);
myservo.write(90); //center servo, ~1.5 ms pulse
myservo.attach(2); //servo on pin 2
pulses = 0; //start condition
startsecond = millis(); //start condition
}
void loop() {
/*
for a one second period, count the number of pulses; have to
do this the hard way, as we need to leave the pin as an output
*/
newsecond = millis();
if (newsecond - startsecond < 1000) { //if a second has not elapsed
while (digitalReadFast(2) == LOW); // if pin is low, wait for high
while (digitalReadFast(2) == HIGH); // if pin is high, wait for low
pulses = pulses + 1; // increment counter
}
else { // else, disp value and restart timer, counter
startsecond = newsecond;
if (pulses != 50) Serial.println(pulses);
pulses = 0;
}
}