Skip to content

Instantly share code, notes, and snippets.

@MKo-xx
Created April 12, 2011 14:15
Show Gist options
  • Save MKo-xx/915557 to your computer and use it in GitHub Desktop.
Save MKo-xx/915557 to your computer and use it in GitHub Desktop.
unsigned long duration;
unsigned long time0;
unsigned long time;
void setup() {
Serial.begin(9600);
// signal pin
pinMode(2, INPUT);
//
time0 = micros();
}
void loop() {
//
time = micros();
// read pulse width
// if there is no pulse during 50000 microseconds
// then it returns 0
duration = pulseIn(2, HIGH, 50000);
//
Serial.print("Time interval = ");
Serial.print(time - time0, DEC);
Serial.print(", Pulse width = ");
Serial.println(duration, DEC);
//
time0 = time;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment