1

I am working on object tracking robot. I am using python and OpenCV to detect the object and send the proper data to the arduino that controls two servo motors.The data which should be sent to the arduino are servo motors angles ranging between 0-180. I am using sample codes to understand how python and arduino communicate using serial bus.When I send a single digit, the arduino receives it and work as intended, but when I send more than one digit nothing happens. This is the arduino code:

#include <Servo.h>int data;Servo myservo;  // create servo object to control a servo// twelve servo objects can be created on most boardsint pos = 0;    // variable to store the servo positionvoid setup() {   Serial.begin(9600); //initialize serial COM at 9600 baudrate  pinMode(LED_BUILTIN, OUTPUT); //make the LED pin (13) as output  digitalWrite (LED_BUILTIN, LOW);  myservo.attach(9);  Serial.println("Hi!, I am Arduino");}void loop() {while (Serial.available()){ //to receive more than one character  char buffer[] = {' ',' ',' ',' ',' ',' ',' '}; // Receive up to 7 bytes while (!Serial.available()); // Wait for characters Serial.readBytesUntil('n', buffer, 7); data = atoi(buffer);}myservo.write(data);}

And here is the python code:

import serialimport time  # Required to use delay functionsarduinoSerialData = serial.Serial('com14', 9600)  # Create Serial port object called arduinoSerialDatatime.sleep(2)  # wait for 2 secounds for the communication to get establishedprint arduinoSerialData.readline()  # read the serial data and print it as lineprint ("Enter 1 to turn ON LED and 0 to turn OFF LED")while 1:  # Do this forever    var = raw_input()  # get input from user    print "you entered", var  # print the intput for confirmation    arduinoSerialData.write(var)
askedSep 8, 2018 at 10:52
Mohammad Nur's user avatar
1
  • the only debugging code that you have isSerial.println("Hi!, I am Arduino"); .... put in more debugging code to determine what is being receivedCommentedSep 8, 2018 at 18:11

1 Answer1

1

If the python Serial write function sends byte, then you should read it in Arduino with Serial.read().

void loop() {  if (Serial.available()){    byte data = Serial.read();    myservo.write(data);  }}

If python sends the number as text, then you can read it withparseInt().

void loop() {  if (Serial.available()){    int data = Serial.parseInt();    myservo.write(data);  }}

TheparseInt is one of the blocking functions and will by default wait one second for next char after the last sent digit was received. But the number is sent at once from python so the delays between digits are small. Set insetup()Serial.setTimeout(10);

answeredSep 8, 2018 at 13:29
Juraj's user avatar
8
  • I need to update the servo's angle continuously. Is there something wrong with that?CommentedSep 8, 2018 at 19:17
  • 1
    I don't know. I never controlled servos.CommentedSep 8, 2018 at 19:20
  • How arduino saves the received data? in what data type?CommentedSep 8, 2018 at 19:26
  • I edited the answerCommentedSep 8, 2018 at 19:48
  • please check this post:arduino.stackexchange.com/questions/55986/…CommentedSep 10, 2018 at 12:39

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.