#include const int longDelay = 1500;//longer than the 1ms space, but shorter than the 3ms space between bytes const int rxPin = A0; LiquidCrystal lcd(2,3,4,5,6,7); int rxChars=0; int currentRow=0; const int lcdWidth = 20; const int lcdHeight = 4; const int EOT = 4; void setup() { // put your setup code here, to run once: // open the serial port at 9600 bps: Serial.begin(9600); lcd.begin(lcdWidth, lcdHeight); } void loop() { // put your main code here, to run repeatedly: byte rx = getByte(); //Serial.println(rx, BYTE); // print as a raw byte value //Serial.println(rx, BIN); // print as an ASCII-encoded binary Serial.println(rx); // print as an ASCII-encoded decimal if (rx == EOT) {//if we received the end of transmission bit while(rxChars>0) {//clear out th rest of the line with spaces lcd.print(' '); rxChars++; checkLCDWrap(); } while(currentRow>0) {//clear out the rest of the rows with spaces lcd.print(' '); rxChars++; checkLCDWrap(); } } else {//otherwise print the character we received lcd.print(rx); rxChars++; checkLCDWrap(); } } void checkLCDWrap() { if (rxChars >= lcdWidth) {//check to see if the end of the row was reached rxChars=0;//if it was, wrap to the next row currentRow++; if (currentRow >= lcdHeight) {//check if the last row was reached currentRow = 0;//if it was, wrap to the first row } lcd.setCursor(0, currentRow); } } byte getByte() { unsigned long lastMark = micros(); while (micros()-lastMark maxLength) { maxLength = bitLengths[x]; } if (bitLengths[x] < minLength) { minLength = bitLengths[x]; } } middleLength=(maxLength+minLength)/2;//comput the threshold between the longest and shortest pulses int result=0; for (x=0; x<10; x++) {//make a decision on whether each bit was a one or a zero based on the length of its pulse if (bitLengths[x] < middleLength) { result = result << 1; } if (bitLengths[x] > middleLength) { result = result << 1; result++; } } //This section checks to make sure that the start bits were properly received. // if ((result & (1<<9+1<<8)) == 1<<9) // { // int mask = 0xFF; // result = result & mask; // } // else // { // result = 0; // } return result; } unsigned long getBitLength() { unsigned long length; while (digitalRead(rxPin)==HIGH) { }//wait for a pulse unsigned long startTime = micros(); while (digitalRead(rxPin)==LOW) { }//wait for the pulse to end length=micros()-startTime; return length; //return the length of the pulse }