Scenario
I've plugged 6 pins from my RC receiver into my Arduino in order to read the incoming values.
Problem
I'm using Digital PWM~ pins 2-6. Oddly the Arduino only reads odd channels:
1480, 0, 1175, 0, 991, 0
I've that if I physically reverse the cables (2-7, 3-6, 4-5) going into the Arduino I get ALL values:
988, 983, 1511, 1174, 1490, 1487
I've found that if I go back to my initial pin setup and merely flip neighbors (2-3, 4-5, 6-7) that works as well.
Question
What could possibly be causing this?
Code
Here is the current code I've written
int ch1, ch2, ch3, ch4, ch5, ch6;
int ch1Pin = 2;
int ch2Pin = 3;
int ch3Pin = 4;
int ch4Pin = 5;
int ch5Pin = 6;
int ch6Pin = 7;
void setup() {
pinMode(ch1Pin, INPUT);
pinMode(ch2Pin, INPUT);
pinMode(ch3Pin, INPUT);
pinMode(ch4Pin, INPUT);
pinMode(ch5Pin, INPUT);
pinMode(ch6Pin, INPUT);
Serial.begin(9600);
}
void loop() {
ch1 = pulseIn(ch1Pin, HIGH, 25000);
ch2 = pulseIn(ch2Pin, HIGH, 25000);
ch3 = pulseIn(ch3Pin, HIGH, 25000);
ch4 = pulseIn(ch4Pin, HIGH, 25000);
ch5 = pulseIn(ch5Pin, HIGH, 25000);
ch6 = pulseIn(ch6Pin, HIGH, 25000);
String p = (String)ch1;
p += ", " + (String)ch2;
p += ", " + (String)ch3;
p += ", " + (String)ch4;
p += ", " + (String)ch5;
p += ", " + (String)ch6;
Serial.println(p);
// delay(100);
}
Photo
EDIT
Since posting this question I've found that reversing the chXpin
values accomplishes the same effect as reversing the physical pins. This makes me thing it's a software issue so I'm digging in further.
TEMPORARY SOLUTION
I'm not sure why this works but flipping the neighbor order of pulseIn
fixed the issue:
/* OLD
ch1 = pulseIn(ch1Pin, HIGH, 25000);
ch2 = pulseIn(ch2Pin, HIGH, 25000);
ch3 = pulseIn(ch3Pin, HIGH, 25000);
ch4 = pulseIn(ch4Pin, HIGH, 25000);
ch5 = pulseIn(ch5Pin, HIGH, 25000);
ch6 = pulseIn(ch6Pin, HIGH, 25000);
*/
ch2 = pulseIn(ch2Pin, HIGH, 25000);
ch1 = pulseIn(ch1Pin, HIGH, 25000);
ch4 = pulseIn(ch4Pin, HIGH, 25000);
ch3 = pulseIn(ch3Pin, HIGH, 25000);
ch6 = pulseIn(ch6Pin, HIGH, 25000);
ch5 = pulseIn(ch5Pin, HIGH, 25000);
Why did that work? It makes no sense to me.