使用arduino和python自动锁定我的电脑

我使用一个简单的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()

相关文章

功能概要:(目前已实现功能)公共展示部分:1.网站首页展示...
大体上把Python中的数据类型分为如下几类: Number(数字) ...
开发之前第一步,就是构造整个的项目结构。这就好比作一幅画...
源码编译方式安装Apache首先下载Apache源码压缩包,地址为ht...
前面说完了此项目的创建及数据模型设计的过程。如果未看过,...
python中常用的写爬虫的库有urllib2、requests,对于大多数比...