I am working on a project based on Arduino and python3.6,two HC-SR04 sense the distance of palms and process the data using Arduino and send it through serial port to python3.6 ,python3.6 with the help of pyserial ,pyautogui based on data from arduino controls a game named supertuxcart,a racing game,basically the thought behind this is controlling game with palms instead of keyboard or mouse .But the problem am facing is Arduino takes the distance measurement data from two HC-SR04(one for each hand) ultrasonic sensors, processes according and sends processed data through serial port to python3.6 ,there while running the my python script i open the game to check if its working ,it works, but there is lag in data received from arduino thus turn the game messy.
here is arduino code am using with NewPing library
#include <NewPing.h>
int trig1=9;
int echo1=6;
int trig2=7;
int echo2=5;
int R;
int L;
NewPing right(9,6,100);
NewPing left(7,5,100);
void setup() {
pinMode(9,OUTPUT);
pinMode(8,OUTPUT);
pinMode(6,INPUT);
pinMode(5,INPUT);
}
void loop() {
Serial.begin(9600);
//Serial.println("hi,am arduino");
R=right.ping_cm();
L=left.ping_cm();
//Serial.println(R);
//Serial.println(L);
delay(0000);
if (R==0 && L==0){
Serial.println('0');
}
else if(R<L&&L-R>5){
Serial.println('1');
}
else if(R>L&&R-L>5){
Serial.println('2');
delay(3000);
}
}
Here is python3.6 code am using
import serial
import pyautogui
import time
Arduino = serial.Serial('/dev/ttyACM0', 9600)
time.sleep(0)
print(Arduino.readline())
pyautogui.keyDown('up')
while 1:
Data = Arduino.readline()
print(Arduino.readline())
if Data == b'0\r\n':
pyautogui.keyUp('up')
pyautogui.keyUp('right')
pyautogui.keyUp('left')
break
elif Data == b'1\r\n':
pyautogui.keyUp('left')
pyautogui.keyDown('right')
elif Data == b'2\r\n':
pyautogui.keyUp('right')
pyautogui.keyDown('left')
Serial.flush()
, or Majenko's #3 suggestion would eliminate the lag caused by buffering. – Gerben Mar 9 '18 at 15:00