Arduino Акселерометр до Python Выход - PullRequest
1 голос
/ 22 февраля 2020

Я пытаюсь взять мой код arduino, чтобы он вывел символ, а затем мой код python взял этот символ и выполнил функцию (нажмите вверх, вниз, влево или вправо). Прямо сейчас мой код arduino дает мне вывод нужной буквы, и я вижу этот вывод, когда я запускаю python, но python все еще не нажимает кнопки, как мне нужно. Любая помощь будет отличной!

Код Arduino:


int  xmin=250,xmax=330;
int  ymin=264,ymax=330;
int  zmin=268,zmax=400;
int n=0, nx=0, ny=0, nz=0, a=0;
int x,y,z;
String cad="";

void setup() {
  Serial.begin(115200);
}


void loop() {


  nx=1;ny=1;nz=1;a=1;
  x=analogRead(1);
  y=analogRead(2);
  z=analogRead(3);
  x=9-map(x,xmin,xmax,0,9);
  y=map(y,ymin,ymax,0,9);
  z=map(z,zmin,zmax,0,9);


  if (x<3) nx=0;
  if (x>5) nx=2;
  if (y<3) ny=0;
  if (y>5) ny=2;
  if (z<3) nz=0;
  if (z>5) nz=2;


  a=1+9*nz+nx+3*ny;

  if (a==25){
    Serial.write('A'); //Release
  }
  else if (a==18){
    Serial.write('B'); //Duck
  }
  else if (a==26){
    Serial.write('C'); //Move right
  }
  else if (a==10){
    Serial.write('D'); //Left
  }
  else if (a==22){
    Serial.write('D');
  }
  else if (a==16){
    Serial.write('E'); //Jump
  }
  delay(200);  

}

Python Код:

import serial
import time
import pyautogui


str_up="up"    
str_down="down"
str_left="left"
str_right="right"


ArduinoSerial = serial.Serial(port='/dev/tty.usbmodem14501', baudrate=115200)
time.sleep(2)

while 1:
  incoming =  str (ArduinoSerial.read(1)) #Read one byte from serial port
  print(incoming)

  if 'D' == incoming:
    pyautogui.keyDown(str_left)  # Press left and ...
    pyautogui.keyUp(str_left)    # release
  elif 'A' in incoming:
   pyautogui.keyUp(str_right) ## release and ...
   pyautogui.keyUp(str_right)   # release
  elif 'C' == incoming:
    pyautogui.keyDown(str_right) # Press right and ...
    pyautogui.keyUp(str_right)   # release
  elif 'B' == incoming:
    pyautogui.keyDown(str_down) ## Press down and ...
    pyautogui.keyUp(str_down)   # release
  elif 'E' == incoming:
    pyautogui.keyDown(str_up) ## Press up and ...
    pyautogui.keyUp(str_up)   # release 


  ArduinoSerial.reset_input_buffer()  # Flush the serial port

1 Ответ

1 голос
/ 22 февраля 2020

В соответствии с документами: https://pyautogui.readthedocs.io/en/latest/keyboard.html#the -press-keydown-and-keyup-functions вы можете использовать метод "press ('up')" вместо keyDown и keyUp. Может быть, это быстро, чтобы зарегистрироваться? Вы можете попробовать время ожидания между ними.

Пример:

>>> pyautogui.keyDown('shift')  # hold down the shift key
>>> pyautogui.press('left')     # press the left arrow key
>>> pyautogui.press('left')     # press the left arrow key
>>> pyautogui.press('left')     # press the left arrow key
>>> pyautogui.keyUp('shift')    # release the shift key
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...