pulses_get holding buffer values?

  • I am reading a frequency input using pulses_get. When pulses_get is called the first time, it reads 128 values and calculates the frequency correctly. However, when I change the input frequency, the next time pulses_get is called it reads values that are from the previous frequency. I call pulses_get the 3rd time and it reads the NEW frequency correctly. Is there a buffer my program should be clearing? What am I missing? Any help would be much appreciated!

    Here is my code:

    #setup imports
    from machine import Pin
    from pycom import pulses_get
    import time
    p_in        = Pin('P13', mode=Pin.IN)
    p_out_pwr   = Pin('P4', mode=Pin.OUT)
    #turn on sensor
    pulses = pulses_get(p_in, 500)
    #turn off sensor
    #print length of pulses tuple
    print("Number of pulses: {}".format(len(pulses)))
    for i, (level, duration) in enumerate(pulses):
        #calculate pulse ON and OFF time
        if level == 1:
            pulse_on_time = duration
        elif level == 0:
            pulse_off_time = duration 
        if pulse_on_time > 0 and pulse_off_time > 0:
            period = pulse_on_time + pulse_off_time 
            frequency = 1000000.0/period #Hz
            print("i = {:2} f: {} Hz {} khz".format(i, frequency, frequency*.001))```

  • @pycom9 pycom.pulses_get() has no means of managing what it is doing. It uses the RMT device. So you could try to use the RMT module API, allowing to deinit and init the channel again. That may reset the internal ringbuffer of the RMT peripheral.

  • Does pulses_get clear the pulses dict each time you call it? If not & you are appending into the dict then there is a chance that the pulses dict is still holding old values. Dicts written to in a function, unlike variables, are persistent between calls to the function.

Log in to reply

Pycom on Twitter