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
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.
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
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.