Dear Sirs,
I’m new in the Arduino world, and I would need, for data acquisition, to read PWM data coming from a R/C receiver (very classical problem) on my Arduino MEGA 2560.
After few hours on Google, the best code I found to do it is:
I tried it, and it run perfectly…!!
However, I would need to read 4 different channels, so I tried to modify the code (see just bellow) to be able to read another channel in the same time.
The code read PIN2 at its origin, and I tried to read PIN3 in the same time.
But this code is still able to read PWM signals on PIN2, but not in PIN3.
Would someone have any idea of the problem??
Thanks in advance for your help,
Best Regards,
Stéphane
// Reading servos with interrupts
// For an Arduino Mega
// Scott Harris January 2010
//
// This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License.
// To view a copy of this license, visit Creative Commons — Attribution-ShareAlike 3.0 United States — CC BY-SA 3.0 US
volatile long servo0; // servo value
volatile long count0; // temporary variable for servo1
volatile long servo1; // servo value
volatile long count1; // temporary variable for servo1
// PIN 2
// #define int0 (PINE & 0b00010000) // Faster than digitalRead
#define int0 (PINE & 0x10)
// PIN 3
// #define int0 (PINE & 0b00100000)
#define int1 (PINE & 0x20)
void handleInterrupt()
{
if(int0)
count0=micros(); // we got a positive edge
else
servo0=micros()-count0; // Negative edge: get pulsewidth
if(int1)
count1=micros(); // we got a positive edge
else
servo1=micros()-count1; // Negative edge: get pulsewidth
}
void setup()
{
Serial.begin(9600);
pinMode(2,INPUT);
pinMode(3,INPUT);
attachInterrupt(0,handleInterrupt,CHANGE); // Catch up and down
}
void loop()
{
delay(10);
Serial.println(servo0,DEC); // Pulsewidth in microseconds
Serial.println(servo1,DEC); // Pulsewidth in microseconds
}