Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. delay(2000);
  2.  
  3. // Calcuate weather parameters for serial and web
  4. calcWeather(); // Go calc all the various sensors
  5.  
  6. // send weather out to serial port
  7. if (prevtemplm34_1 == templm34_1) // 2 in a row must be the same to send
  8. {
  9. Serial.write(0);
  10. Serial.write(templm34_1);
  11. Serial.write(templm34_1 ^ 0);
  12. }
  13.  
  14. // send weather out to serial port
  15. if(prevtemplm34_2 == templm34_2) // 2 in a row must be the same to send
  16. {
  17. Serial.write(1);
  18. Serial.write(templm34_2);
  19. Serial.write(templm34_2 ^ 1);
  20. }
  21.  
  22. // send weather out to serial port
  23. if(prevtemplm34_3 == templm34_3) // 2 in a row must be the same to send
  24. {
  25. Serial.write(2);
  26. Serial.write(templm34_3);
  27. Serial.write(templm34_3 ^ 2);
  28. }
  29. delay(2000);
  30.  
  31. int readData ()
  32. {
  33. // There must be at least three available bytes to satisfy the protocol
  34. // Otherwise, wait until three bytes have arrived to read anything
  35. if (Serial.available() >= 3) {
  36.  
  37. // Read the next three byte in the serial buffer
  38. incomingByteOne = Serial.read();
  39. Serial.write("Byte One: ");
  40. Serial.write(incomingByteOne);
  41. Serial.println();
  42. incomingByteTwo = Serial.read();
  43. Serial.write("Byte Two: ");
  44. Serial.write(incomingByteTwo);
  45. Serial.println();
  46. incomingByteThree = Serial.read();
  47. Serial.write("Byte Three: ");
  48. Serial.write(incomingByteThree);
  49. Serial.println();
  50.  
  51. // XOR the first and second incoming bytes so that the result can be checked against the third error checking byte
  52. byte xorValue = incomingByteOne ^ incomingByteTwo;
  53. Serial.write("XOR VALUE: ");
  54. Serial.write(xorValue);
  55. Serial.println("Number of Errors: ");
  56. Serial.println(numberOfErrors);
  57. Serial.println();
  58. Serial.println();
  59. if(xorValue != incomingByteThree) {
  60. numberOfErrors++; // Add one to the total count of number of errors in transmission
  61. }
  62. else {
  63. // Mask for retrieving the five rightmost bits from incomingByteOne (00011111)
  64. byte maskForSensorNumber = 0x1f;
  65.  
  66. // Shift incomingByteOne right 5 times in order to obtain the three bits representing the unit number
  67. int unitNumber = incomingByteOne >> 5;
  68.  
  69. // AND incomingByteOne with the mask in order to retrieve only the five righmost bits representing the sensor number
  70. int sensorNumber = incomingByteOne & maskForSensorNumber;
  71.  
  72. // Store the data for the correct sensor on the correct unit
  73. data[unitNumber][sensorNumber] = incomingByteTwo*1;
  74. }
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement