Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #define HDC 0x40 //B1000000 MSB
- #define cociente 65536 //2^16
- uint16_t humrawvalue; //Raw data value, MS byte = valmsb, LS byte = vallsb
- uint16_t temrawvalue; //Raw data value, MS byte = valmsb, LS byte = vallsb
- uint8_t humvalmsb, humvallsb; //Raw 8-bit byte of humidity
- uint8_t temvalmsb, temvallsb; //Raw 8-bit byte of temperature
- int incomingByte = 0; //Action input
- float humidity, temperature;
- void setup() {
- delay(100);
- Serial.begin(9600);
- delay(100);
- Serial.println("Hello");
- //Wire.begin(12,16);
- Wire.begin();
- Wire.setClockStretchLimit(1500);
- }
- void loop() {
- if (Serial.available() > 0) { //User presses 1 to start sensor reading
- // read the incoming byte:
- incomingByte = Serial.read();
- yield();
- // say what you got:
- Serial.print("I received: ");
- Serial.println(incomingByte, DEC);
- }
- Serial.println("---g-"); //Just guideline
- if (incomingByte == 49){
- Serial.println("IIIII"); //Just guideline
- Wire.beginTransmission(HDC);
- Wire.write(0x00);
- Wire.endTransmission(true);
- delay(200);
- Wire.requestFrom(HDC,4,true); //It asks for 4 bytes, 2 bytes per register
- if(Wire.available()){
- Serial.println(" ");
- Serial.println("Measuring: ");
- temvalmsb = Wire.read();
- temvallsb = Wire.read();
- humvalmsb = Wire.read();
- humvallsb = Wire.read();
- Serial.println("====TEMPERATURE====");
- Serial.print("MSB: "); Serial.println(temvalmsb, BIN);
- Serial.print("LSB: "); Serial.println(temvallsb, BIN);
- temrawvalue = temvalmsb <<8| temvallsb; //Locates each byte in its proper place
- Serial.print("Temp: "); Serial.println(temrawvalue, BIN);
- temperature = ((float)temrawvalue/(float)cociente)*165.0-40.0;
- Serial.print("Temperatura [C]: "); Serial.println(temperature);
- Serial.println("====HUMIDITY====");
- Serial.print("MSB: "); Serial.println(humvalmsb, BIN);
- Serial.print("LSB: "); Serial.println(humvallsb, BIN);
- humrawvalue = humvalmsb <<8| humvallsb;
- Serial.print("Hum: "); Serial.println(humrawvalue, BIN);
- humidity = ((float)humrawvalue/(float)cociente)*100.0;
- Serial.print("Humidity [%]: "); Serial.println(humidity);
- Serial.println(" ");
- delay(3000);
- }
- }
- Serial.println("++++"); //Just guideline
- delay(3000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement