Skip to content

Instantly share code, notes, and snippets.

@DzikuVx
Created January 5, 2016 19:41
Show Gist options
  • Save DzikuVx/77270f15e0cb5389047f to your computer and use it in GitHub Desktop.
Save DzikuVx/77270f15e0cb5389047f to your computer and use it in GitHub Desktop.
Read RC PWM signal with Arduino
#include <PinChangeInterrupt.h>
/*
* Define pins used to provide RC PWM signal to Arduino
* Pins 8, 9 and 10 are used since they work on both ATMega328 and
* ATMega32u4 board. So this code will work on Uno/Mini/Nano/Micro/Leonardo
* See PinChangeInterrupt documentation for usable pins on other boards
*/
const byte channel_pin[] = {8, 9, 10};
volatile unsigned long rising_start[] = {0, 0, 0};
volatile long channel_length[] = {0, 0, 0};
void setup() {
Serial.begin(57600);
pinMode(channel_pin[0], INPUT);
pinMode(channel_pin[1], INPUT);
pinMode(channel_pin[2], INPUT);
attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(channel_pin[0]), onRising0, CHANGE);
attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(channel_pin[1]), onRising1, CHANGE);
attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(channel_pin[2]), onRising2, CHANGE);
}
void processPin(byte pin) {
uint8_t trigger = getPinChangeInterruptTrigger(digitalPinToPCINT(channel_pin[pin]));
if(trigger == RISING) {
rising_start[pin] = micros();
} else if(trigger == FALLING) {
channel_length[pin] = micros() - rising_start[pin];
}
}
void onRising0(void) {
processPin(0);
}
void onRising1(void) {
processPin(1);
}
void onRising2(void) {
processPin(2);
}
void loop() {
Serial.print(channel_length[0]);
Serial.print(" | ");
Serial.print(channel_length[1]);
Serial.print(" | ");
Serial.print(channel_length[2]);
Serial.println("");
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment