我使用一个简单的arduino脚本来控制一个运动传感器,该传感器可以通过按钮控制打开或关闭,以通过串行发送消息:“检测到运动”。我对python不太了解,我使用下面的代码触发了一个锁定我的PC的批处理文件。问题是,当一切运行时,如果传感器检测到运动并在串口上打印消息,则批处理文件将被触发3次,因此我必须输入3次密码。有什么解决办法吗?有更好的方法吗?我用的是arduino mega btw。
Arduino代码:
int calibrationTime = 30; // The time we give the sensor to calibrate (10-60 secs according to the datasheet)
long unsigned int lowIn; // The time when the sensor outputs a low impulse
long unsigned int pause = 5000; // The amount of milliseconds the sensor has to be low,before we assume all motion has stopped.
boolean lockLow = true;
boolean takeLowTime;
const int buttonPin = 6; // The digital pin connected to the button input.
int pirPin = 3; // The digital pin connected to the PIR sensors output.
int detectionPin = 34; // The digital pin connected to the LED output.
int armPin = 38; // The digital pin connected to the LED output.
int disarmPin = 36; // The digital pin connected to the LED output.
int buttonState = 0; // Initialize state of button
void setup()
{
Serial.begin(9600); // Begin serial communication for python.
M
pinMode(pirPin,INPUT); // Declare sensor as input.
pinMode(detectionPin,OUTPUT); // Declare LED as output.
pinMode(armPin,OUTPUT); // Declare LED as output.
pinMode(disarmPin,OUTPUT); // Declare LED as output.
pinMode(buttonPin,INPUT_PULLUP); // Declare button as input and use the internal pull up resistor.
digitalWrite(pirPin,LOW); // Declare starting state of sensor.
for(int i = 0; i < calibrationTime; i++); // Give the sensor some time to calibrate
}
void loop()
{
int buttonState = digitalRead(buttonPin); // Read the state of the pushbutton value.
if (buttonState == LOW)
{
digitalWrite(disarmPin,LOW); // Turn off the green (safe) LED.
digitalWrite(armPin,HIGH); // Turn on the red (arm) LED.
if(digitalRead(pirPin) == HIGH)
{
digitalWrite(detectionPin,HIGH); // Turn on the blue (detection) LED.
if(lockLow)
{
Serial.println("Motion Detected!"); //Print to serial port for python software detection.
}
lockLow = false; // Makes sure we wait for a transition to LOW before any further output is made.
takeLowTime = true;
}
if(digitalRead(pirPin) == LOW)
{
digitalWrite(detectionPin,LOW); // Turn on the blue (detection) LED.
if(takeLowTime)
{
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
if(!lockLow && millis() - lowIn > pause) //if the sensor is low for more than the given pause,we assume that no more motion is going to happen
{
lockLow = true; //makes sure this block of code is only executed again after a new motion sequence has been detected
delay(50);
}
}
}
if (buttonState == HIGH)
{
digitalWrite(disarmPin,HIGH); // Turn on the green (safe) LED.
digitalWrite(armPin,LOW); // Turn off the red (arm) LED.
}
}
Python代码:
import serial
import subprocess
from pynput.keyboard import Key,Controller
keyboard = Controller()
ser = serial.Serial('COM7',9600)
print(ser.name)
while (True):
line = ser.readline().decode('UTF-8')
for char in line:
subprocess.call([r'D:\00 DATA C\Documents\Arduino Projecten\Autolock PC\runlockpc.bat'])
ser.close()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。