complementing inputs

Hi all-

My digital line following LED sensors return a LOW when they are on the line and a HIGH when they are off.

For mathematical reasons, I want a 1 when I am over the line. Is there an easy way to flip these bits?

In a past micro's class, we used COMX (where X is the port name) to flip bits, but these are not all on the same port because of placement issues. I'll probably need to flip each input individually.

Any ideas?



What does "over the line" mean, please?

To flip a bit subject it to an exclusive OR operation with a logic 1.

So to flip bit 0 in a variable then use:- variable = variable ^ 0x01; or better yet variable ^= 0x01;

To invert a Boolean variable like you get from a digitalRead() use the pling operator ! variable = digitalRead(pin); inversevariable = !variable;

AWOL: What does "over the line" mean, please?

Sorry.. more specifically: There are 8 LED sensors fed into 8 digital inputs on the micro. They are used for tracking a line and following it around a course. They detect the line (which is actually a bright colored piece of tape) and return a LOW value to the micro if it is detected. In other words, if the sensor is physically over/on top of the line, it returns a LOW. If not, it returns a HIGH.

If I understand your post correctly, you are assuming that LOW equals '0 and HIGH equals '1'. That's a fundamentally bad assumption to make. If you're storing digital input values as bits, you need to explicitly define what bit value you're using for HIGH and LOW. It sounds as if you need to use LOW=1 and HIGH=0, but it's up to your code to define that.

If you’re storing digital input values as bits, you need to explicitly define what bit value you’re using for HIGH and LOW. I

HIGH and LOW are byte values, if you want to convert them into bit values simply do a bit wise and operation (&) with a mask of one:-
variableOne = HIGH & 0x01;
variableZero = LOW & 0x01;
So for a digital read you want to invert, returned as a single bit do:-
variable = (digitalRead(pin) & 0x01) ^ 0x01;