FreddyCruger

Untitled

Mar 24th, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include <SoftwareSerial.h>;
  2.  
  3. SoftwareSerial mySerial(A0, A1); // A0 - к TX сенсора, A1 - к RX
  4.  
  5. byte cmd[9] = {0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79};
  6. unsigned char response[9];
  7.  
  8. void setup() {
  9.   Serial.begin(9600);
  10.   mySerial.begin(9600);
  11. }
  12.  
  13. void loop()
  14. {
  15.   mySerial.write(cmd, 9);
  16.   memset(response, 0, 9);
  17.   mySerial.readBytes(response, 9);
  18.   int i;
  19.   byte crc = 0;
  20.   for (i = 1; i < 8; i++) crc+=response[i];
  21.   crc = 255 - crc;
  22.   crc++;
  23.  
  24.   if ( !(response[0] == 0xFF && response[1] == 0x86 && response[8] == crc) ) {
  25.     Serial.println("CRC error: " + String(crc) + " / "+ String(response[8]));
  26.   } else {
  27.     unsigned int responseHigh = (unsigned int) response[2];
  28.     unsigned int responseLow = (unsigned int) response[3];
  29.     unsigned int ppm = (256*responseHigh) + responseLow;
  30.     Serial.println(ppm);
  31.   }
  32.   delay(10000);
  33. }
Add Comment
Please, Sign In to add comment