Giter Site home page Giter Site logo

Comments (16)

RobTillaart avatar RobTillaart commented on May 28, 2024

Thank you for reporting plus providing a simple solution.

from dhtnew.

RobTillaart avatar RobTillaart commented on May 28, 2024

Which processor do you use?
Which IDE version/ develop platform?
Thanks

from dhtnew.

RobTillaart avatar RobTillaart commented on May 28, 2024

There is a related issue with the delayMicroseconds(18000).
Some applications using RTOS need to call yield() for context switching, e.g. to handle a Wifi incoming buffer etc.
So the solution will be something more "complicated" than just splitting the call.

from dhtnew.

RobTillaart avatar RobTillaart commented on May 28, 2024

@arnowa
Created a Pull Request #73 with a rewrite of the wake-up delay.
Please verify if this fix works for you.

Replaced the delayMicroseconds(_wakeupDelay * 1100UL); with a loop that calls yield() every iteration.
For a DHT22 alike it will be called only once, for the DHT11 alike it will be called 18 times.

  // WAKE UP - add 10% extra for timing inaccuracies in sensor.
  uint32_t startWakeup = micros();
  do
  {
    // HANDLE PENDING IRQ
    yield();
    delayMicroseconds(1100UL);
  }
  while((micros() - startWakeup) < (_wakeupDelay * 1100UL));

Non-RTOS environments (e.g. UNO) implement yield() as an empty function which will be optimized away.

from dhtnew.

arnowa avatar arnowa commented on May 28, 2024

from dhtnew.

arnowa avatar arnowa commented on May 28, 2024

from dhtnew.

arnowa avatar arnowa commented on May 28, 2024

from dhtnew.

RobTillaart avatar RobTillaart commented on May 28, 2024

Good point, could make it 550.

from dhtnew.

RobTillaart avatar RobTillaart commented on May 28, 2024

@arnowa

Which board do you use?
Which IDE?

from dhtnew.

arnowa avatar arnowa commented on May 28, 2024

plain cheapduino Uno R3 clone (i.e. ATMega 328) and latest Arduino IDE 1.8.19 on 64 bit Linux.

from dhtnew.

RobTillaart avatar RobTillaart commented on May 28, 2024

Thanks.
Did some math and every 360 us would match 18 x 1100 = 19.800 exactly. Given the loop has some overhead it would at most 'overflow' 359 us.
3x better than 1100.

from dhtnew.

arnowa avatar arnowa commented on May 28, 2024

from dhtnew.

RobTillaart avatar RobTillaart commented on May 28, 2024

plain cheapduino Uno R3 clone (i.e. ATMega 328) and latest Arduino IDE 1.8.19 on 64 bit Linux.

Ok that allows me to recreate/test a bit

from dhtnew.

RobTillaart avatar RobTillaart commented on May 28, 2024

Did some more math/testruns and came to 180.

For DHT11 ==> 19808 us
For DHT22 ==> 1104 us

So quite a perfect (including loop overhead).

  // WAKE UP - add 10% extra for timing inaccuracies in sensor.
  uint32_t startWakeup = micros();
  do
  {
    // HANDLE PENDING IRQ
    yield();
    delayMicroseconds(180UL);
  }
  while((micros() - startWakeup) < (_wakeupDelay * 1100UL));

from dhtnew.

RobTillaart avatar RobTillaart commented on May 28, 2024

The problem seems to be calling delayMicroseconds() with a value larger than 16383: https://www.arduino.cc/reference/en/language/functions/time/delaymicroseconds/

The parameter of delayMicros() is unsigned int that implies one could go up to 65535
(it is hand-tuned code, so I stop digging)


parameter us is multiplied by 4 here causing overflow...

	// the following loop takes 1/4 of a microsecond (4 cycles)
	// per iteration, so execute it four times for each microsecond of
	// delay requested.
	us <<= 2; // x4 us, = 4 cycles

from dhtnew.

RobTillaart avatar RobTillaart commented on May 28, 2024

Solution merged in master

from dhtnew.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.