0

I have a dust sensor, and I have to measure a sum of inactive time in 30 seconds cycles.

For DigitalInputDevice gpiozero provides cool functionality, I mean two properties, and two callbacks:

  • properties - active_time (return float if pin is active or None) and inactive_time (return float if pin is inactive or None),
  • callbacks - when_activated and when_deactivated

I thought that inside those callbacks I would have access to the digit input in the previous state, which would allow me to grab active time when pin change state to inactive, and inactive time when pin change state to active, but I was wrong.
So my question is: There is any possibility in this library to grab a whole active time when pin turns to inactive and vice versa?

Thank you!

Clarification: what I want is described in the image below

image of chart my sensor docs: https://botland.com.pl/index.php?controller=attachment&id_attachment=1565

I do it now in this way:

from gpiozero import DigitalInputDevice
from datetime import timedelta
import time


sensor_one_micrometer = DigitalInputDevice(20)
while True:
    active_time = 0
    inactive_time = 0

    sensor_one_micrometer.wait_for_active()
    while active_time + inactive_time < 30:
        watch_start = time.time()
        sensor_one_micrometer.wait_for_inactive()
        active_time += time.time() - watch_start
        watch_start = time.time()
        sensor_one_micrometer.wait_for_active()
        inactive_time += time.time() - watch_start
    percent = inactive_time/(active_time+inactive_time)
    print("Active Time: ",  active_time)
    print("Inactive Time: ", inactive_time)
    print(
        inactive_time/(active_time+inactive_time)
    )
  • Try to give some more details and code examples as it is quite hard to understand what exactly you are trying to achieve – J.Paravicini Dec 27 '19 at 17:14
  • Hello, thank you for the response, I added some details. – Bartłomiej Dec 27 '19 at 22:03
  • Thanks, the code will completely block your thread, but if that's not a problem for you,this should theoretically work. What is the error or result you get? You should in fact be able to use the callbacks to get the events when they changed state. Its hard to try it out myself since i have no matching hardware. – J.Paravicini Dec 27 '19 at 22:28
  • Blocking thread it's not a problem for me. It's a point, I don't know how to get callbacks which I need. So I calculate time manually :/ – Bartłomiej Dec 28 '19 at 16:21

0 Answers0