Skip to content

Instantly share code, notes, and snippets.

@jazzyjackson
Last active May 21, 2019 18:07
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 jazzyjackson/2ceb93149125ef53b16b8118707ffc72 to your computer and use it in GitHub Desktop.
Save jazzyjackson/2ceb93149125ef53b16b8118707ffc72 to your computer and use it in GitHub Desktop.
typedef struct{
byte pin;
volatile short timeHigh;
volatile short timeLow;
volatile long lastFalling;
volatile long lastRising;
void dutyCycleUpdate(){
switch(digitalRead(this->pin)){
case HIGH:
this->lastRising = micros();
this->timeLow = this->lastRising - this->lastFalling;
break;
case LOW:
this->lastFalling = micros();
this->timeLow = this->lastFalling - this->lastRising;
break;
}
}
byte checkDutyCycle(){
return map(this->timeHigh, 0, this->timeLow + this->timeHigh, 0, 255);
}
} DutyCycle;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment