简体   繁体   中英

python loop only print values once

Below is a short piece of python code.. it's for a sensor reading light or darkness. What I want it to do when the value drops below 500 print "light" and when it's above 500 print "Dark". This pretty much works but the text is repeated for every reading.. while I only want it to be printed upon a change.. anyone has any idea how to do this? I did quite a bit of shell programming.. but somehow this simple issue I can't get it done in python..

#!/usr/bin/python

import RPi.GPIO as GPIO, time

GPIO.setmode(GPIO.BCM)

# Define function to measure charge time
def RCtime (PiPin):
  measurement = 0
  # Discharge capacitor
  GPIO.setup(PiPin, GPIO.OUT)
  GPIO.output(PiPin, GPIO.LOW)
  time.sleep(0.1)

  GPIO.setup(PiPin, GPIO.IN)
  # Count loops until voltage across
  # capacitor reads high on GPIO

  last = 9
  while (GPIO.input(PiPin) == GPIO.LOW):
    measurement += 1

  if measurement < 500:
     print last
     if last == 0:
      print "light"
      last = 1
  if measurement >500:
   print "dark"
   last = 0
   print last
  return measurement

# Main program loop
while True:
  print RCtime(4) # Measure timing using GPIO4

I think you have the idea right in the if measurement < 500: condition, printing "light" only when last was different. You just have to repeat similar logic in the > 500 condition. But the real problem is that last here is a local variable, so the value will get reset to 9 on every call. So you need to remove the last=9, and define last outside of the function and declare it as global inside the function:

#in main program
last = 9
def RCTime ...:
    global last
    ....

Since you're returning value on each function execution I think the best idea would be to compare it in the while loop and keep the function only for getting the data.

Something along these lines:

previous, current = None, None

def RCtime (PiPin):
    [...]

while True:
    measurement = RCtime(4)
    previous, current = current, measurement<500
    if current != previous:
        # value changed...
        # `current` is bool and True if there is light.
        # or you can just deal with the value itself for logic

    print measurement

Otherwise returning the value of last and then passing it on when function is next time called is also an acceptable solution.

I simulate the sensor's readings using a list named readings , the rest of the code can be directly translated to your use case

readings = [ 600, 600, 550, 501, 450, 400, 400, 460, 520, 600, 600]
dark = None

for i, reading in enumerate(readings):
    dark_now = 'Dark' if reading < 500 else 'Light'
    if dark_now != dark:
        dark = dark_now
        print i, reading, dark

Output

0 600 Light
4 450 Dark
8 520 Light

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM