help to understand what this code means

int counter_channel_1

if(PINB & B00000001){ //Is input 8 high?
if(last_channel_1 == 0){ //Input 8 changed from 0 to 1
last_channel_1 = 1; //Remember current input state
timer_1 = current_time; //Set timer_1 to current_time
}
}
else if(last_channel_1 == 1){ //Input 8 is not high and changed from 1 to 0.
last_channel_1 = 0; //Remember current input state.
receiver_input[1] = current_time - timer_1; //Channel 1 is current_time - timer_1.
}

comments isn't clear enough for me, can someone illustrate above code please..

last_channel_1 is a variable and its not declared to anything still arduino not showing any error, based on my very basic knowledge its been declared but i cant find where exactly :frowning:

For very obvious reasons from the first line, that code cannot compile, so please post all your code, this time in code tags.

Post the complete program between code tags like:

[code]
Paste 
  code
    here
[/code]

I'm sorry the code is a bit long.. :confused:

//Declaring Variables
byte last_channel_1, last_channel_2, last_channel_3, last_channel_4;
int receiver_input_channel_1, receiver_input_channel_2, receiver_input_channel_3, receiver_input_channel_4;
unsigned long timer_1, timer_2, timer_3, timer_4;

//Setup routine
void setup(){
  //Arduino (Atmega) pins default to inputs, so they don't need to be explicitly declared as inputs
  PCICR |= (1 << PCIE0);    // set PCIE0 to enable PCMSK0 scan
  PCMSK0 |= (1 << PCINT0);  // set PCINT0 (digital input 8) to trigger an interrupt on state change
  PCMSK0 |= (1 << PCINT1);  // set PCINT1 (digital input 9)to trigger an interrupt on state change
  PCMSK0 |= (1 << PCINT2);  // set PCINT2 (digital input 10)to trigger an interrupt on state change
  PCMSK0 |= (1 << PCINT3);  // set PCINT3 (digital input 11)to trigger an interrupt on state change
  Serial.begin(9600); 
}

//Main program loop
void loop(){
  delay(250);
 // print_signals();
}

//This routine is called every time input 8, 9, 10 or 11 changed state
ISR(PCINT0_vect){
  //Channel 1=========================================
  if(last_channel_1 == 0 && PINB & B00000001 ){         //Input 8 changed from 0 to 1
    last_channel_1 = 1;                                 //Remember current input state
    timer_1 = micros();                                 //Set timer_1 to micros()
  }
  else if(last_channel_1 == 1 && !(PINB & B00000001)){  //Input 8 changed from 1 to 0
    last_channel_1 = 0;                                 //Remember current input state
    receiver_input_channel_1 = micros() - timer_1;      //Channel 1 is micros() - timer_1
  }
  //Channel 2=========================================
  if(last_channel_2 == 0 && PINB & B00000010 ){         //Input 9 changed from 0 to 1
    last_channel_2 = 1;                                 //Remember current input state
    timer_2 = micros();                                 //Set timer_2 to micros()
  }
  else if(last_channel_2 == 1 && !(PINB & B00000010)){  //Input 9 changed from 1 to 0
    last_channel_2 = 0;                                 //Remember current input state
    receiver_input_channel_2 = micros() - timer_2;      //Channel 2 is micros() - timer_2
  }
  //Channel 3=========================================
  if(last_channel_3 == 0 && PINB & B00000100 ){         //Input 10 changed from 0 to 1
    last_channel_3 = 1;                                 //Remember current input state
    timer_3 = micros();                                 //Set timer_3 to micros()
  }
  else if(last_channel_3 == 1 && !(PINB & B00000100)){  //Input 10 changed from 1 to 0
    last_channel_3 = 0;                                 //Remember current input state
    receiver_input_channel_3 = micros() - timer_3;      //Channel 3 is micros() - timer_3
  }
  //Channel 4=========================================
  if(last_channel_4 == 0 && PINB & B00001000 ){         //Input 11 changed from 0 to 1
    last_channel_4 = 1;                                 //Remember current input state
    timer_4 = micros();                                 //Set timer_4 to micros()
  }
  else if(last_channel_4 == 1 && !(PINB & B00001000)){  //Input 11 changed from 1 to 0
    last_channel_4 = 0;                                 //Remember current input state
    receiver_input_channel_4 = micros() - timer_4;      //Channel 4 is micros() - timer_4
  }
}

That code you were asking about is executing in the PCINT0 ISR - that is, it's called by the interrupt whenever one of those pins they list changes.

But then you need to figure out which of those pins changed, and what state it's in - and fast, since you're in an ISR. digitalRead() is slow because it has to look up what port and pin within that port a given arduino pin number refers to - presumably too slow for this application (they're reading a pin 8 times in an ISR, so this isn't unresonable). So they read the PINB (port input for port B) register, whose value reflects the state of the pins in port b, and do the usual bitmath on it to check specific pins.

There are a number of guides around on "direct port manipulation" in arduino - that's I think what you want to look up.

It's declared on the first line:
byte last_channel_1, last_channel_2, last_channel_3, last_channel_4;

 if(last_channel_1 == 0 && PINB & B00000001 ){         //Input 8 changed from 0 to 1
    last_channel_1 = 1;                                 //Remember current input state
    timer_1 = micros();                                 //Set timer_1 to micros()
  }
  else if(last_channel_1 == 1 && !(PINB & B00000001)){  //Input 8 changed from 1 to 0
    last_channel_1 = 0;                                 //Remember current input state
    receiver_input_channel_1 = micros() - timer_1;      //Channel 1 is micros() - timer_1

thank you so much for the reply.. really appreciate it :slight_smile:

i understand why digitalRead() is avoided, but my problem is, if you notice the comments [// input 8 changed from 0 to 1 then latter //Input 8 changed from 1 to 0 ] i can't see when exactly its changing from 0 to 1 or from 1 to 0.

if you notice the comments [// input 8 changed from 0 to 1 then latter //Input 8 changed from 1 to 0 ] i can't see when exactly its changing from 0 to 1 or from 1 to 0.

PINB is the B port as INPut. Something is connected to one (or more) of the pins that make up port B. It is that pin that is changing state, because whatever is connected to that pin went from supplying no voltage to supplying voltage, or from supplying voltage to not supplying voltage.