Skip to content

Instantly share code, notes, and snippets.

@sagiii
Last active May 21, 2020 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sagiii/d5d599442b51c62457e2371f995d97e3 to your computer and use it in GitHub Desktop.
Save sagiii/d5d599442b51c62457e2371f995d97e3 to your computer and use it in GitHub Desktop.
Servo PWM probe for Arduino. Assuming up edge of evey port is aligned.
const int NPIN = 2; // pin count
const int PINS[NPIN] = {2, 3}; // input pins
volatile long long t0 = 0, t[2] = {0};
void edge_function(int pin) {
byte level = digitalRead(PINS[pin]);
if(level && pin == 0) { // react to up edge of only pin 0.
t0 = micros();
Serial.print((int)t[0]);
Serial.print("\t");
Serial.print((int)t[1]);
Serial.println();
}
if (!level) {
t[pin] = micros() - t0;
}
}
void edge0() {
edge_function(0);
}
void edge1() {
edge_function(1);
}
void setup() {
Serial.begin(115200);
pinMode(PINS[0], INPUT_PULLUP);
pinMode(PINS[1], INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(PINS[0]), edge0, CHANGE); // only pin 0 need up edge detect.
attachInterrupt(digitalPinToInterrupt(PINS[1]), edge1, FALLING); // otherwise just falling edge is needed.
}
void loop() {
// put your main code here, to run repeatedly:
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment