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: http://scottrharris.blogspot.fr/2010/01/reading-servo-pwm-with-arduino.html
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,
// 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 http://creativecommons.org/licenses/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)
count0=micros(); // we got a positive edge
servo0=micros()-count0; // Negative edge: get pulsewidth
count1=micros(); // we got a positive edge
servo1=micros()-count1; // Negative edge: get pulsewidth
attachInterrupt(0,handleInterrupt,CHANGE); // Catch up and down
Serial.println(servo0,DEC); // Pulsewidth in microseconds
Serial.println(servo1,DEC); // Pulsewidth in microseconds