Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. #include <Wire.h>
  2.  
  3. #define HDC 0x40 //B1000000 MSB
  4. #define cociente 65536 //2^16
  5.  
  6. uint16_t humrawvalue; //Raw data value, MS byte = valmsb, LS byte = vallsb
  7. uint16_t temrawvalue; //Raw data value, MS byte = valmsb, LS byte = vallsb
  8. uint8_t humvalmsb, humvallsb; //Raw 8-bit byte of humidity
  9. uint8_t temvalmsb, temvallsb; //Raw 8-bit byte of temperature
  10. int incomingByte = 0; //Action input
  11. float humidity, temperature;
  12.  
  13. void setup() {
  14. delay(100);
  15. Serial.begin(9600);
  16. delay(100);
  17. Serial.println("Hello");
  18. //Wire.begin(12,16);
  19. Wire.begin();
  20. Wire.setClockStretchLimit(1500);
  21. }
  22.  
  23. void loop() {
  24. if (Serial.available() > 0) { //User presses 1 to start sensor reading
  25. // read the incoming byte:
  26. incomingByte = Serial.read();
  27. yield();
  28. // say what you got:
  29. Serial.print("I received: ");
  30. Serial.println(incomingByte, DEC);
  31. }
  32.  
  33. Serial.println("---g-"); //Just guideline
  34.  
  35. if (incomingByte == 49){
  36. Serial.println("IIIII"); //Just guideline
  37. Wire.beginTransmission(HDC);
  38. Wire.write(0x00);
  39. Wire.endTransmission(true);
  40. delay(200);
  41.  
  42. Wire.requestFrom(HDC,4,true); //It asks for 4 bytes, 2 bytes per register
  43. if(Wire.available()){
  44. Serial.println(" ");
  45. Serial.println("Measuring: ");
  46. temvalmsb = Wire.read();
  47. temvallsb = Wire.read();
  48. humvalmsb = Wire.read();
  49. humvallsb = Wire.read();
  50.  
  51. Serial.println("====TEMPERATURE====");
  52. Serial.print("MSB: "); Serial.println(temvalmsb, BIN);
  53. Serial.print("LSB: "); Serial.println(temvallsb, BIN);
  54. temrawvalue = temvalmsb <<8| temvallsb; //Locates each byte in its proper place
  55. Serial.print("Temp: "); Serial.println(temrawvalue, BIN);
  56. temperature = ((float)temrawvalue/(float)cociente)*165.0-40.0;
  57. Serial.print("Temperatura [C]: "); Serial.println(temperature);
  58.  
  59. Serial.println("====HUMIDITY====");
  60. Serial.print("MSB: "); Serial.println(humvalmsb, BIN);
  61. Serial.print("LSB: "); Serial.println(humvallsb, BIN);
  62. humrawvalue = humvalmsb <<8| humvallsb;
  63. Serial.print("Hum: "); Serial.println(humrawvalue, BIN);
  64. humidity = ((float)humrawvalue/(float)cociente)*100.0;
  65. Serial.print("Humidity [%]: "); Serial.println(humidity);
  66.  
  67. Serial.println(" ");
  68. delay(3000);
  69. }
  70. }
  71. Serial.println("++++"); //Just guideline
  72. delay(3000);
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement