Datasheet

Chapter 3: EEPROM Tricks and Program Tips ยท Page 129
โˆš Repeat by using this feature to change qtiStateLeft to qtiLF.
โˆš Some of the comments may now be out of alignment. Go through the program
and insert spaces to line the side comments back up to column 46.
โˆš Save your modified program; you will need it in the next couple activities.
ACTIVITY #7: ORGANIZING SENSORS WITH FLAG BITS
Storing each sensor value as an individual bit is great for decision making, especially if
you want to isolate only one or two variables. In other cases, it's better to have all your
bits in a larger variable. It makes it easier for your program to analyze patterns in the
sensor flags. With PBASIC, you can have it both ways. This activity demonstrates how
to declare a sensors variable, and then declare individual flag bits within that variable.
Bit Declarations inside a Byte
By storing all your flag bits in a larger variable, they are still accessible as individual
values, but they are also accessible as a group for pattern analysis. It doesn't make
programming any more difficult. In fact, with PBASIC all you have to do is declare a
byte variable, and then declare the names of each individual bit in the byte.
For example, here are the 7 bit-variable declarations from TestAllSensorsYourTurn.bs2.
irLS VAR Bit ' State of Left Side IR
irLF VAR Bit ' State of Left Front IR
irRF VAR Bit ' State of Right Front IR
irRS VAR Bit ' State of Right Side IR
qtiLF VAR Bit ' Stores snapshot of QtiSigLeft
qtiRF VAR Bit ' Stores snapshot of QtiSigRight
pushbutton VAR Bit ' Stores pushbutton state
To arrange these in a byte, simply declare the byte variable, and then declare each bit as a
member of the byte variable. For example, if the byte is named sensors, irRS can be
sensors.BIT0, irRF
can be sensors.BIT1, and so on. Here is the entire series of
variable declarations within a single
sensors byte variable:
sensors VAR Byte ' Sensor flags byte
pushbutton VAR sensors.BIT6 ' Stores pushbutton state
qtiLF VAR sensors.BIT5 ' Stores snapshot of QtiSigLeft
qtiRF VAR sensors.BIT4 ' Stores snapshot of QtiSigRight