Skip to content

Instantly share code, notes, and snippets.

@rinozen
Created September 3, 2018 04:47
Show Gist options
  • Save rinozen/ab73d79dbe7be05d6adceb85872da84a to your computer and use it in GitHub Desktop.
Save rinozen/ab73d79dbe7be05d6adceb85872da84a to your computer and use it in GitHub Desktop.
Arduino 8Ch PPM Decoder with Smoothing and Connection Status
unsigned long int ft, lt, x, tfail;
int ch, chx[9][13];
const int idx = 10;
const int total = 11;
const int val = 12;
void setup() {
Serial.begin(9600);
ft=0; lt=0; x=0; tfail=0; ch=0;
for (int i=0; i<9; i++) {
for (int j=0; j<13; j++) {
chx[i][j]=0;
}
}
pinMode(2, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(2), decodePPM, FALLING);
}
void loop() {
if ((millis()-tfail)>500) {
Serial.println("Disconnect");
} else {
Serial.print(chx[0][val]);Serial.print("\t");
Serial.print(chx[1][val]);Serial.print("\t");
Serial.print(chx[2][val]);Serial.print("\t");
Serial.print(chx[3][val]);Serial.print("\t");
Serial.print(chx[4][val]);Serial.print("\t");
Serial.print(chx[5][val]);Serial.print("\t");
Serial.print(chx[6][val]);Serial.print("\t");
Serial.print(chx[7][val]);Serial.print("\t");
Serial.println(chx[8][val]);
}
}
void decodePPM() {
lt = micros();
tfail = millis();
x = lt-ft;
ft = lt;
if (x>3000) {
ch = 0;
chx[0][val] = x;
} else {
ch+=1;
int indx = chx[ch][idx];
chx[ch][total] = chx[ch][total] - chx[ch][indx];
chx[ch][indx] = x;
chx[ch][total] = chx[ch][total] + chx[ch][indx];
chx[ch][idx] = chx[ch][idx] + 1;
if (chx[ch][idx]>9) {
chx[ch][idx] = 0;
}
chx[ch][val] = chx[ch][total]/10;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment