0

I am working on a project based onArduino andpython3.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 ofpyserial ,pyautogui based on data from arduino controls a game namedsupertuxcart,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 isarduino code am using withNewPing 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 ispython3.6 code am using

import serialimport pyautoguiimport timeArduino = 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')
gre_gor's user avatar
gre_gor
1,6824 gold badges18 silver badges30 bronze badges
askedMar 9, 2018 at 14:08
Samdare's user avatar
5
  • You could try using a higher baudrate, instead of just 9600.CommentedMar 9, 2018 at 14:48
  • Possible explanation for the lag. Since serial data is being buffered; if you send data faster, than the serial connection can handle, you'd get lag. As the latest data has to wait in line to be send. Using a tiny delay, or betterSerial.flush(), or Majenko's #3 suggestion would eliminate the lag caused by buffering.CommentedMar 9, 2018 at 15:00
  • NewPing right(9,6,100); NewPing left(7,5,100); Is the 100 the timeout for no return? Can you make that shorter?CommentedMar 9, 2018 at 15:30
  • its maximum distance ,100cmCommentedMar 9, 2018 at 15:58
  • Don't edit the question and invalidate an existing answer.CommentedMar 9, 2018 at 16:05

1 Answer1

1

A couple of things jump out at me:

  1. You only need to runSerial.begin(9600) once insetup() - never inloop() like that.
  2. Yourdelay(3000) will be causing a 3 second delay whenR>L&&R-L>5.
  3. You don't need to constantly send the data over and over again. You just need to send it once and once only when the state changes.

At the moment you're sending something like:

0000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111111111111111111111111100000000000000000000000000000000000000000000000000000000000000000002222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222200000000000000000000000000000000000000000000000000000000000000

when all you really need to do is send01020. Remember what the state is and only send anything when that state changes from one iteration to the next. And never ever usedelay().

answeredMar 9, 2018 at 14:15
Majenko's user avatar
2
  • The code is using Serial.println, so it's more like0\r\n0\r\n1\r\n1\r\n. So three bytes, for every byte of actual data.CommentedMar 9, 2018 at 14:47
  • Yeah, but still it's overkill. And I didn't want to make my post 6 miles long to illustrate a point...CommentedMar 9, 2018 at 14:49

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.