Browse Source

Ensure enough delay b/w samples

pull/17/head
destroyedlolo 7 years ago
parent
commit
08d8346954
  1. 15
      examples/DS18B20_DHT22/DS18B20_DHT22.ino

15
examples/DS18B20_DHT22/DS18B20_DHT22.ino

@ -41,6 +41,7 @@ enum DeviceState {
}; };
volatile DeviceState state = DeviceState::WAIT4RESET; volatile DeviceState state = DeviceState::WAIT4RESET;
volatile unsigned long conversionStartTime = 0;
void owRcv( OneWireSlave::ReceiveEvent evt, byte cmd ){ void owRcv( OneWireSlave::ReceiveEvent evt, byte cmd ){
switch( evt ){ switch( evt ){
@ -53,9 +54,11 @@ void owRcv( OneWireSlave::ReceiveEvent evt, byte cmd ){
case OneWireSlave::RE_Byte: case OneWireSlave::RE_Byte:
switch( cmd ){ switch( cmd ){
case OW_Cmd::START_CONVERSION: case OW_Cmd::START_CONVERSION:
// Do a new request only if enough time passed
if( millis() < conversionStartTime || millis() > conversionStartTime + 2000 ){
state = DeviceState::CONVERTING; state = DeviceState::CONVERTING;
OWSlave.beginWriteBit(0, true); // send zeros as long as the conversion is not finished OWSlave.beginWriteBit(0, true); // send zeros as long as the conversion is not finished
// consequently, we don't have to take care of race condition as no other command can arrive }
break; break;
case OW_Cmd::READ_SCRATCHPAD: case OW_Cmd::READ_SCRATCHPAD:
state = DeviceState::WAIT4RESET; state = DeviceState::WAIT4RESET;
@ -96,21 +99,25 @@ void setup(){
void loop(){ void loop(){
delay(10); delay(10);
// No need to have a local copy of "state" as we are looking
// for only one value
if(state == DeviceState::CONVERTING){ // start conversion if(state == DeviceState::CONVERTING){ // start conversion
float temperature = 0; float temperature = 0;
float humidite = 0; float humidite = 0;
int err; int err;
conversionStartTime = millis();
if((err = DHT.read2(pinDHT, &temperature, &humidite, NULL)) != SimpleDHTErrSuccess){ if((err = DHT.read2(pinDHT, &temperature, &humidite, NULL)) != SimpleDHTErrSuccess){
#ifdef DEBUG #ifdef DEBUG
Serial.print("\nError while reading, err="); Serial.print("\nError while reading, err=");
Serial.println(err); Serial.println(err);
#endif #endif
temperature = 85; temperature = 85;
} else } else {
#ifdef DEBUG
Serial.print("Temperature :");
Serial.println(temperature);
#endif
setTemperature( temperature ); setTemperature( temperature );
}
state = DeviceState::TEMPERATUREREADY; state = DeviceState::TEMPERATUREREADY;
OWSlave.beginWriteBit(1, true); OWSlave.beginWriteBit(1, true);
} }

Loading…
Cancel
Save