Can someone please help me with the following code, it was written to read 4 channels from a RC receiver, I have tried to extend the code to read 8 channels but it won't read past the fifth channel.
The author of the original code pointed to Chapter 13 of the atMega328 data sheet for help with the PINB & B00000010 part of the code where I think my problem lies, but I haven't been able to work it out.
Any help would be gratefully appreciated.
Ken
//code by Kamran A on you tube
unsigned long timer[9];
byte last_channel[8];
int input[8];
void setup() {
PCICR |= (1 << PCIE0);
PCMSK0 |= (1 << PCINT0);//pin 8...Rx Channel 1
PCMSK0 |= (1 << PCINT1);//pin 9...Rx Channel 2
PCMSK0 |= (1 << PCINT2);//pin 10...Rx Channel 3
PCMSK0 |= (1 << PCINT3);//pin 11...Rx Channel 4
PCMSK0 |= (1 << PCINT4);//pin 12...Rx Channel 5
PCMSK0 |= (1 << PCINT5);//pin 5...Rx Channel 6
PCMSK0 |= (1 << PCINT22);//pin 6...Rx Channel 7
PCMSK0 |= (1 << PCINT23);//pin 7...Rx Channel 8
Serial.begin(9600);
}
void loop() {
print();
}
ISR(PCINT0_vect) {
timer[0] = micros();
// channel 1 ---------------
if(last_channel[0] == 0 && PINB & B00000001 ) {
last_channel[0] = 1;
timer[1] = timer[0];
}
else if(last_channel[0] == 1 && !(PINB & B00000001) ) {
last_channel[0] = 0;
input[0] = timer[0] - timer[1];
}
// channel 2 ---------------
if(last_channel[1] == 0 && PINB & B00000010 ) {
last_channel[1] = 1;
timer[2] = timer[0];
}
else if(last_channel[1] == 1 && !(PINB & B00000010) ) {
last_channel[1] = 0;
input[1] = timer[0] - timer[2];
}
// channel 3 ---------------
if(last_channel[2] == 0 && PINB & B00000100 ) {
last_channel[2] = 1;
timer[3] = timer[0];
}
else if(last_channel[2] == 1 && !(PINB & B00000100) ) {
last_channel[2] = 0;
input[2] = timer[0] - timer[3];
}
// channel 4 ---------------
if(last_channel[3] == 0 && PINB & B00001000 ) {
last_channel[3] = 1;
timer[4] = timer[0];
}
else if(last_channel[3] == 1 && !(PINB & B00001000) ) {
last_channel[3] = 0;
input[3] = timer[0] - timer[4];
}
// channel 5 ---------------
if(last_channel[4] == 0 && PINB & B00010000 ) {
last_channel[4] = 1;
timer[5] = timer[0];
}
else if(last_channel[4] == 1 && !(PINB & B00010000) ) {
last_channel[4] = 0;
input[4] = timer[0] - timer[5];
}
// channel 6 ---------------
if(last_channel[5] == 0 && PINB & B00100000 ) {
last_channel[5] = 1;
timer[6] = timer[0];
}
else if(last_channel[5] == 1 && !(PINB & B00100000) ) {
last_channel[5] = 0;
input[5] = timer[0] - timer[6];
}
// channel 7 ---------------
if(last_channel[6] == 0 && PINB & B01000000 ) {
last_channel[6] = 1;
timer[7] = timer[0];
}
else if(last_channel[6] == 1 && !(PINB & B01000000) ) {
last_channel[6] = 0;
input[6] = timer[0] - timer[7];
}
// channel 8 ---------------
if(last_channel[7] == 0 && PINB & B10000000 ) {
last_channel[7] = 1;
timer[8] = timer[0];
}
else if(last_channel[7] == 1 && !(PINB & B10000000) ) {
last_channel[7] = 0;
input[7] = timer[0] - timer[8];
}
}
void print() {
Serial.print(input[0]);
Serial.print(" - ");
Serial.print(input[1]);
Serial.print(" - ");
Serial.print(input[2]);
Serial.print(" - ");
Serial.print(input[3]);
Serial.print(" - ");
Serial.print(input[4]);
Serial.print(" - ");
Serial.print(input[5]);
Serial.print(" - ");
Serial.print(input[6]);
Serial.print(" - ");
Serial.println(input[7]);
}