So after a bit more work i've now got some code that works XD
Instead of using PinChangeInt() i am now using the two hardware interupt pins on my arduino UNO.
These receive the servo signal from my RC Receiver. The two inputs are connected via a jumper.
One of the inputs is used to detect an interrupt on a RISING edge, The other on a FALLING edge.
Works pretty good, occasionally my math gets an erronous result, so if its outside the expected range the math is done again.
#include <PinChangeInt.h>
/*
Radio controlled switch
Using an RC PWM receiver out put to turn a LED on and off
When there is a rising edge on rcPin, pin 11, the ISR stores the time since the program started running in
the variable risingMicros.
Then, when a falling edge is detected on rcPin, the ISR stores the time since program started running in the
Variable fallingMicros.
To find the pulseWidth the program subtracts rising time from falling time. While this math is done we prevent
the values changing by turning interrupts off.
We can then test pulseWidth to decide if the switch should be on or off.
*/
// Assign Pin names
int ledPin = 13; // make ledPin = pin 13
int rcPin = 2; //make rcPin = pin 2 (hardware interrupt 0)
int rcPin2 = 3; //make rcPin = pin 3 (hardware interrupt 1)
//Create global variable in which to store our measured pulse width
int pulseWidth = 1500; //And initialize the variable with the value 1500
//declare unsigned long variables in which to store rising and falling micros, for us to do math with later.
unsigned long risingMicros;
unsigned long fallingMicros;
// Interrupt Service Routine (ISR)
void pinRising ()
{
// handle pin rising here
//if(rcPin == HIGH){
risingMicros = micros();// if rc pin is HIGH then store the time since the program started in risingMicros
//}
} // end of pinRising routine
void pinFalling ()
{
// handle pin falling here
//if(rcPin == LOW){
fallingMicros = micros();// if rc pin is LOW then store the time since the program started in fallingMicros
//}
} // end of pinFalling routine
void setup()
{
pinMode(ledPin, OUTPUT); //sets ledPin as an output
pinMode(rcPin, INPUT); //sets rcPin as an input
pinMode(rcPin2, INPUT); //sets rcPin2 as an input
attachInterrupt (0, pinRising, RISING); // attach interrupt handler rising edge
attachInterrupt (1, pinFalling, FALLING); // attach interrupt handler falling edge
Serial.begin(115200); //open up a serial port for debugging and messages
delay(1000);
Serial.println("RC Switch Project"); //warm up serial port
delay(1000);
}
void loop()
{
//pulseWidth is already primed at 1500 for the first loop.
if(pulseWidth >= 1500){
digitalWrite(ledPin, HIGH); //turn on the LED
Serial.print("PWM is ");
Serial.print(pulseWidth);
Serial.println(" so turn led on"); //shout it out
}else{
digitalWrite(ledPin, LOW); // turn LED off
Serial.print("PWM is ");
Serial.print(pulseWidth);
Serial.println(" so turn led off"); //shout it out
}
delay(1000); //wait 1 second
// work out the puslewidth at the end of each loop of the code
noInterrupts();// turn off interrupts while we do math
pulseWidth = fallingMicros - risingMicros;
interrupts();// turn interrupts back on now math has been done
Serial.print(fallingMicros);
Serial.print(" - ");
Serial.print(risingMicros);
Serial.print(" = ");
Serial.println(pulseWidth); //Print out values for debugging purposes
// ERROR CHECK, If the calculated values were updated before the math was completed, then the value would
// be read wrong, to avoid this we test the result, if it is outside of expected range, we throw an error on the
//serial port, and then do the math again. this is repeated until the statement becomes true
while(pulseWidth < 800 || pulseWidth > 2200){
Serial.print("ERROR: PulseWidth Outside expected range (");
Serial.print(pulseWidth);
Serial.println(") recalculating pulsewidth");
noInterrupts();// interrupts off while doing math
pulseWidth = fallingMicros - risingMicros;// do math again
interrupts();//interrupts back on once math is done
//tell us what values you got this time
Serial.print(fallingMicros);
Serial.print(" - ");
Serial.print(risingMicros);
Serial.print(" = ");
Serial.println(pulseWidth); //Print out values for debugging purposes
//wait 2 seconds before you check again
delay(2000);
}
}
I added a PinChange library, but when I try to add it to my project it doesn't paste an #include statement into my project like what other library's do. Perhaps I need to reinstall the library.
A link to the library would be useful. Step-by-step details about "add it to my project" would be useful, too. Personally, I don't find it too difficult to just type #include followed by the header file name, so I've never used the tool that you seem to be referring to.
Arduino Playground - HomePage this is the library i was trying to use
http://arduino.cc/en/Guide/Libraries how i was adding a library
Libraries - Arduino Reference How i was importing the library into my project
Frankly, I'd be using a proper receiver to control the things that it is supposed to control, and the Arduino to do stuff it is suited for. It is NOT a receiver, so making it control servos and stuff doesn't eliminate the need for a real receiver.
I am using a "proper receiver", the arduino is being used to convert the receivers PWM output to a digital output.