Hardware Reference
In-Depth Information
The readByte() method is called
every time a new byte comes in from
the RFID reader. If the incoming byte is
02 , it starts a new current tag string. If
the byte is 03 , it checks the current tag
against the list of known tags using a
method called checkTags() . If it's any
other byte, it checks to see whether the
current tag string is long enough; if not,
it adds the new byte to that string.
8
void readByte() {
char thisChar = rfid.read();
// depending on the byte's value,
// take different actions:
switch(thisChar) {
// if the byte = 02, you're at the beginning
// of a new tag:
case 0x02:
currentTag = "";
break;
// if the byte = 03, you're at the end of a tag:
case 0x03:
checkTags();
break;
// other bytes, if the current tag is less than
// 10 bytes, you're still reading it:
default:
if (currentTag.length() < 10) {
currentTag += thisChar;
}
}
}
8
The checkTags() method checks
the current tag string against the
known list. When it finds the tag, it
checks the state of the corresponding
X10 unit from a list of unit states. Then
it sends that unit a message to change
its state from ON to OFF, or vice versa.
void checkTags() {
// iterate over the list of tags:
for (int thisTag = 0; thisTag < numTags; thisTag++) {
// if the current tag matches the tag you're on:
if (currentTag.equals(tag[thisTag])) {
// unit number starts at 1, but list position starts at 0:
Serial.print("unit " + String(thisTag +1));
// start transmission to unit:
x10.beginTransmission(A);
x10.write(unit[thisTag]);
// change the status of the corresponding unit:
if (unitState[thisTag] == ON) {
unitState[thisTag] = OFF;
Serial.println(" turning OFF");
}
else {
unitState[thisTag] = ON;
Serial.println(" turning ON");
}
// send the new status:
x10.write(unitState[thisTag]);
// end transmission to unit:
x10.endTransmission();
}
}
}
 
Search WWH ::




Custom Search