2014-09-09 1 views
2

Мне нужно записать детектор движения «pir sensor» COUNT в текстовый файл.PIR-датчик Число обнаружения движения Запись в текстовый файл в python

Я пробовал с этим кодом, и это работа без записи в текстовый файл. когда я помещаю запись в файл, она дает ошибку. file = open("textFile.txt", "w") IndentationError: unindent does not matchanyouter indentation level. Ожидаемый результат - это последнее количество отсчетов в текстовом файле.

код

# Import required Python libraries 
    import time 
    import RPi.GPIO as GPIO 
    GPIO.setmode(GPIO.BCM) 
    # Define GPIO to use on Pi 
    GPIO_PIR = 7 
# Set pin as input 

    GPIO.setup(GPIO_PIR,GPIO.IN) 


    Current_State = 0 

Previous_State = 0 

    # I put Variable= 0 for the motion Count 
Variable= 0 
     try: 
       print "Waiting for PIR to settle ..." 

     # Loop until PIR output is 0 
     while GPIO.input(GPIO_PIR)==1: 
     Current_State = 0 
      print " Ready" 
# Loop until users quits with CTRL-C 
    while True : 

    # Read PIR state 
    Current_State = GPIO.input(GPIO_PIR) 

    if Current_State==1 and Previous_State==0: 
     # PIR is triggered 
     start_time=time.time() 
     print " Motion detected!" 

     # here I need to write numbers for the text file. 
     file = open("textFile.txt", "w") 
     file.write(Variable) 
     file.close() 
     Variable+=1 

     # Record previous state 
     Previous_State=1 
    elif Current_State==0 and Previous_State==1: 
     # PIR has returned to ready state 
     stop_time=time.time() 
     print " Ready ", 
     elapsed_time=int(stop_time-start_time) 
     print " (Elapsed time : " + str(elapsed_time) + " secs)" 
     Previous_State=0 

except KeyboardInterrupt: 
    print " Quit" 
    # Reset GPIO settings 
    GPIO.cleanup() 
+0

если ваш код на самом деле разнесен таким образом - тогда да ошибки отступа вызовут проблемы - используете ли вы вкладки и пробелы или просто пробелы в отступ? – gkusner

ответ

1
import RPi.GPIO as GPIO 
import time 

# Use BCM GPIO references 
# instead of physical pin numbers 
GPIO.setmode(GPIO.BCM) 

# Define GPIO to use on Pi 
GPIO_PIR = 7 

print "PIR Module Test (CTRL-C to exit)" 

# Set pin as input 
GPIO.setup(GPIO_PIR,GPIO.IN)  # Echo 

Current_State = 0 
Previous_State = 0 
Variable=0 

try: 

    print "Waiting for PIR to settle ..." 

    # Loop until PIR output is 0 
    while GPIO.input(GPIO_PIR)==1: 
    Current_State = 0  

    print " Ready"  

    # Loop until users quits with CTRL-C 
    while True : 

    # Read PIR state 
    Current_State = GPIO.input(GPIO_PIR) 

    if Current_State==1 and Previous_State==0: 
     # PIR is triggered 
     print " Motion detected!" 
     # Record previous state 
     Previous_State=1 
     file = open("textFile.txt", "w") 
     file.write(Variable) 
     file.close() 
     Variable+=1 
    elif Current_State==0 and Previous_State==1: 
     # PIR has returned to ready state 
     print " Ready" 
     Previous_State=0 

    # Wait for 10 milliseconds 
    time.sleep(0.01)  

except KeyboardInterrupt: 
    print " Quit" 
    # Reset GPIO settings 

Это код моего друга. (Я считаю, что с вашим кодом ничего не случилось, но несколько ошибок отступа (например, ошибка интервала). Для этого вы можете использовать какой-то текстовый редактор (я использую возвышенный текст)

0

Вам нужно использовать строку с file.write, здесь это пример:

file = open("textfile.txt", "rw+") 
file.write(str(Variable)) 
file.close() 
Смежные вопросы