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)- the only debugging code that you have is
Serial.println("Hi!, I am Arduino");.... put in more debugging code to determine what is being receivedjsotola– jsotola2018-09-08 18:11:48 +00:00CommentedSep 8, 2018 at 18:11
1 Answer1
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);
- I need to update the servo's angle continuously. Is there something wrong with that?Mohammad Nur– Mohammad Nur2018-09-08 19:17:53 +00:00CommentedSep 8, 2018 at 19:17
- 1I don't know. I never controlled servos.2018-09-08 19:20:28 +00:00CommentedSep 8, 2018 at 19:20
- How arduino saves the received data? in what data type?Mohammad Nur– Mohammad Nur2018-09-08 19:26:58 +00:00CommentedSep 8, 2018 at 19:26
- please check this post:arduino.stackexchange.com/questions/55986/…Mohammad Nur– Mohammad Nur2018-09-10 12:39:09 +00:00CommentedSep 10, 2018 at 12:39
Explore related questions
See similar questions with these tags.
