Hello everyone, I've been following the playground guide for creating your own Library. I've successfully written one that takes care of controlling a L298N DC motor driver. Now i'm in the process of writing one that takes care of reading a PPM stream from a RC receiver.
I first began by writing it as a function as part of my normal program, Here it is;
/*
* The PPM signal from RC RX will be fed into pin 2, interrupt 0, on a rising edge the intefrrupt is triggered.
* From here each pulseWidth is calculated. The channels are then stored for access later.
*
*/
//PPM_INPUT
#define SYNC_PULSE_WIDTH 3000 //Length of the PPM sync pulse
int ppmPin = 2;
unsigned long oldRisingMicros;
unsigned long newRisingMicros;
//channelArray 0 = PPM Sync Pulse, channelArray 1 = channel 1, channelArray 2 = channel 2, etc...
int channelArray[9] = {1000, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500};
int channelCounter = 0;
void setup() {
pinMode(ppmPin, INPUT_PULLUP);
attachInterrupt(0, ppmServiceRoutine, RISING); // attach interrupt handler rising edge
}
void ppmServiceRoutine(){
oldRisingMicros = newRisingMicros;
newRisingMicros = micros();
int pulseWidth = newRisingMicros - oldRisingMicros;
if (pulseWidth >= SYNC_PULSE_WIDTH){
channelCounter = 0;
}
channelArray[channelCounter] = pulseWidth;
channelCounter++;
if (channelCounter >= 9){
Serial.println("channelCounter OUT OF RANGE!!!!!");
channelCounter = 0;
}
}
int GetChannelPosition(int channel){
if (channelArray[0] >= SYNC_PULSE_WIDTH && channelArray[channel] > 800 && channelArray[channel] < 2200){
int pulseWidth = constrain(channelArray[channel], 1000, 2000);
return pulseWidth;
}
else{
Serial.print("channel "); Serial.print(channel); Serial.println(" OUT OF RANGE or NO VALID SYNC PULSE");
return 1500;
}
}
This code works just fine, all i do is call GetChannelPosition(channel) whenever i need channel info.
So next i pushed it out to a Library
RC_PPM.h
/*
Use at your peril!!
Jan 16
*/
#ifndef RC_PPM_h
#define RC_PPM_h
#include "Arduino.h"
class RC_PPM
{
public:
RC_PPM(int PPMpin);
int GetChannelPosition(int channel);
private:
void ppmServiceRoutine();
int _ppmPin;
unsigned long _oldRisingMicros;
unsigned long _newRisingMicros;
int _pulseWidth;
int _position;
//int _channelArray;
int _channelCounter;
};
#endif
RC_PPM.cpp
*
Use at your peril!!
Use to grab channels from the PPM stream of an RC receiver
Jan 16
*/
#include "Arduino.h"
#include "RC_PPM.h"
int _channelArray[9] = {1000, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500};
RC_PPM::RC_PPM(int ppmPin)
{
pinMode(ppmPin, INPUT_PULLUP);
_ppmPin = digitalPinToInterrupt(ppmPin);
attachInterrupt(_ppmPin, ppmServiceRoutine, RISING); // attach interrupt handler rising edge
_channelCounter = 0;
}
void RC_PPM::ppmServiceRoutine()
{
_oldRisingMicros = _newRisingMicros;
_newRisingMicros = micros();
int _pulseWidth = _newRisingMicros - _oldRisingMicros;
if (_pulseWidth >= 3000){
_channelCounter = 0;
}
_channelArray[_channelCounter] = _pulseWidth;
_channelCounter++;
if (_channelCounter >= 9){
Serial.println("channelCounter OUT OF RANGE!!!!!");
_channelCounter = 0;
}
}
int RC_PPM::GetChannelPosition(int channel)
{
_channel = channel;
if (_channelArray[0] >= 3000 && _channelArray[_channel] > 800 && channelArray[_channel] < 2200){
_position = constrain(_channelArray[_channel], 1000, 2000);
return _position;
}
else{
Serial.print("channel "); Serial.print(_channel); Serial.println(" OUT OF RANGE or NO VALID SYNC PULSE");
return 1500;
}
}
when i try to use the Library i get errors which i think are related to the attempt to declare and initialise the channelArray. I've tried many combinations but none seem to have the desired effect. can anyone enlighten me as to how this should be done?
Im also curious as to wether i can attach an interrupt inside of my library? any thoughts??
Thanks in advance,
Ben.