Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4859

Python • Re: Rpm,tachometer, refine

$
0
0
Did some refactoring of the code
- not tested
- there was already a constant "SENSOR_PIN", reused this throughout the code
- made the variable names more consistent
- the decision is now on the positive edge only
- used format strings f"..." for the printouts; hope you use a current python interpreter which supports this
- and removed some of the brackets in the calculations.

Code:

import RPi.GPIO as GPIOimport time# import datetime no longer neededSENSOR_PIN = 17GPIO.setmode(GPIO.BCM)# according to the schema provided, there is an external pullup resistor.# so use also an internal pullup.GPIO.setup(SENSOR_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)vorige_status = GPIO.input (SENSOR_PIN)teller = 0vorige_tijd = time.monotonic()try:    while True:        sensor_status = GPIO.input (SENSOR_PIN)        current_tijd = time.monotonic()        # check for an edge 0-->1 on gpio pin        if (sensor_status == 1) and (vorige_status == 0):            teller = teller + 1            delta = current_tijd - vorige_tijd                         print ( f"rotation time     {delta} sec")            print ( f"freq              {1/delta} Hz")             print ( f"toeren per minuut {60 / delta}")             print ( f"Afronden          {round (60 / delta)}")            # could you help: what is meaning of this value is ?            print ( f"einden            {round (4 * 60 / delta)}")            print ("------------------")                        vorige_tijd = current_tijd            # for debouncing            time.sleep(0.7)        # for edge detection, it is needed to        # track each status difference        vorige_status = sensor_statusexcept KeyboardInterrupt:    GPIO.cleanup()

Statistics: Posted by ghp — Wed Nov 20, 2024 9:15 am



Viewing all articles
Browse latest Browse all 4859

Trending Articles