android - 名称错误 : name 'TRUE' is not defined PIR sensors

标签 android raspberry-pi sensors gpio

当用户将双手放在 PIR 传感器上时,我无法对 2 个 PIR 传感器的逻辑进行编程以在控制台中打印消息。我已经成功地使用 GPIO、GND 将 PIR 传感器连接到树莓派,并且5v端口。我目前拥有的代码确实会在有人挥手时在控制台中打印出一条消息,但我很难修改代码以在有人在两个 PIR 传感器上挥手时打印出错误消息。

enter image description here

我们可以使用 GP4 和 GP17 从传感器读取输入

这是我在运行代码时收到的错误消息。

 Traceback (most recent call last):
  File "peter.py", line 22, in <module>
    if current_state2(TRUE) and current_state(TRUE) != previous_state2(FALSE) and previous_state(FALSE):
NameError: name 'TRUE' is not defined

这是代码

 import RPi.GPIO as GPIO
  import time

  sensor = 4
  sensor2 = 17
  GPIO.setmode(GPIO.BCM)
  GPIO.setup(sensor, GPIO.IN, GPIO.PUD_DOWN)
  GPIO.setup(sensor2, GPIO.IN, GPIO.PUD_DOWN)

  previous_state = False
  current_state = False

  previous_state2 = False
  current_state2 = False

  while True:
      time.sleep(0.1)
      previous_state = current_state
      previous_state2 = current_state2
      current_state = GPIO.input(sensor)
      current_state2 = GPIO.input(sensor2)
      if current_state2(TRUE) and current_state(TRUE) != previous_state2(FALSE) and previous_state(FALSE):

          new_state = "HIGH" if current_state else "LOW"
          new_state2 = "HIGH" if current_state2 else "LOW"

          print("GPIO pin %s is %s" % (sensor, new_state, sensor2, new_state2))

程序非常简单。 Raspberry Pi GPIO 引脚允许我们使用引脚 4 作为输入;然后它可以检测 PIR 模块何时发送电源。该引脚不断检查是否有任何更改,为此使用 while True 循环。这是一个无限循环,因此程序将连续运行,除非我们使用 Ctrl + C 手动停止它。然后使用两个 bool 变量(True 或 False)表示引脚的先前状态和当前状态,先前状态就是当前状态循环之前的时间

最佳答案

在 Python 中使用“True”,而不是“TRUE”:

if True:
        print("True")
else:
        print("False")

同时改变:

if current_state2(TRUE) and current_state(TRUE) != previous_state2(FALSE) and previous_state(FALSE):

收件人:

if current_state2==True and current_state != previous_state2 and previous_state==False:

关于android - 名称错误 : name 'TRUE' is not defined PIR sensors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37382525/

相关文章:

Android 非法参数异常 : Wrong state class if the app was background for a period of time

opencv - Raspberry Pi 4中的mp4编解码器:未将帧写入视频

android - 在 Android 的 GridView 中水平滚动

linux - 由于 crontab 被锁定在 Raspberry Pi 之外

raspberry-pi - qemu-system-arm 与 Raspberry Pi 2 镜像一起挂起

networking - 使用 Motes 在 WSN 上广播

android - 从服务访问传感器

安卓 AllJoyn : Connection with second machine gives error of BusAttachement

java - CursorLoader 通知数据变化

java - 写入 SD 卡 "java.io.FileNotFoundException: Permission denied"