-
Notifications
You must be signed in to change notification settings - Fork 2
Temp humid sensor #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
af3edcf
dd533aa
0148438
d3f940c
a56c69d
da277c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,48 @@ | ||
#include "temp_humidity.h" | ||
#define REG 0x00 | ||
#define ADDR 0x40 | ||
|
||
TempHumiditySensor::TempHumiditySensor() : sensor(&Wire, 0x44, 4) { } | ||
void TempHumiditySensor::readReg(){ | ||
Wire.beginTransmission(ADDR); | ||
// Rewrote with overloaded method so that you don't need to use & on a literal | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So the reason we used In the past, we've found it tricky to switch back and forth (eg for testing or debugging) because we were using |
||
Wire.write(REG); | ||
uint8_t code = Wire.endTransmission(); | ||
if(code != 0){ | ||
// Wiping buffer if there is an error | ||
memset(buf, 0, 4); | ||
Serial.print("Error: Could not read temperature/humidity (Error code "); | ||
Serial.print(code); | ||
Serial.println(")"); | ||
return; | ||
} | ||
|
||
void TempHumiditySensor::setup() { | ||
delay(20); | ||
|
||
// Requesting 4 bytes to get data from registers 0x00 and 0x01 and putting it in buf[0:4] | ||
Wire.requestFrom(ADDR, 4); | ||
for(int i = 0; i < 4; i++){ | ||
buf[i] = Wire.read(); | ||
} | ||
return; | ||
} | ||
|
||
void TempHumiditySensor::setup(){ | ||
Serial.print("Initializing temp/humidity sensor..."); | ||
while (sensor.begin() != 0) { | ||
while (Wire.begin() != 0) { | ||
nturner01 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Serial.print("."); | ||
delay(500); | ||
} | ||
Serial.println(" Done!"); | ||
if (!sensor.softReset()) { | ||
Serial.println("Failed to Initialize the chip...."); | ||
} | ||
} | ||
|
||
float TempHumiditySensor::getTemperature() { | ||
return sensor.getTemperatureC(); | ||
float TempHumiditySensor::getTemperature(){ | ||
readReg(); | ||
uint16_t temp = buf[0] << 8 | buf[1]; | ||
return ((float) temp * 165 / 65535.0) - 40.0; | ||
Comment on lines
+61
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definitely a style choice, but consider this: Our other classes have a It's a bit of a trade-off from efficiency vs real-time accuracy, so your call. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for all the feedback! I will implement all of these changes. As for the .begin() error, my code is an adaptation of example code that was provided to me, so I definitely should've looked into the function definitions a little more thoroughly! |
||
} | ||
|
||
float TempHumiditySensor::getHumidity() { | ||
return sensor.getHumidityRH(); | ||
float TempHumiditySensor::getHumidity(){ | ||
readReg(); | ||
uint16_t hum = buf[2] << 8 | buf[3]; | ||
return ((float) hum / 65535.0) * 100; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
#pragma once | ||
#ifndef TEMP_HUMIDITY_H | ||
#define TEMP_HUMIDITY_H | ||
|
||
#include <Arduino.h> | ||
#include "dfrobot/DFRobot_SHT3x.h" | ||
#include "Wire.h" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not important for this PR, but in general, you can have two "categories" of includes:
Basically, think of it as "do others who use this file also need this library?" If so, the include goes into the header file. If not, then it can just be in the ,cpp file. So in this case, you can move the include to the .cpp file. Again, it doesn't actually break or change any functionality, but it's good practice as it means that including one header gives you the least amount of transitive dependencies. |
||
|
||
class TempHumiditySensor { | ||
class TempHumiditySensor{ | ||
private: | ||
DFRobot_SHT3x sensor; | ||
|
||
uint8_t buf[4] = {0}; | ||
void readReg(); | ||
|
||
public: | ||
TempHumiditySensor(); | ||
|
||
void setup(); | ||
float getTemperature(); | ||
float getHumidity(); | ||
}; | ||
|
||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try adding
.DS_STORE
to your global gitignore