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 boards
int pos = 0; // variable to store the servo position
void 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 serial
import time # Required to use delay functions
arduinoSerialData = serial.Serial('com14', 9600) # Create Serial port
object called arduinoSerialData
time.sleep(2) # wait for 2 secounds for the communication to get
established
print arduinoSerialData.readline() # read the serial data and print it as
line
print ("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)
1 Answer 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 with parseInt()
.
void loop() {
if (Serial.available()){
int data = Serial.parseInt();
myservo.write(data);
}
}
The parseInt
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 in setup()
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:00Commented Sep 8, 2018 at 19:17
-
1I don't know. I never controlled servos.2018年09月08日 19:20:28 +00:00Commented Sep 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:00Commented Sep 8, 2018 at 19:26
-
-
please check this post: arduino.stackexchange.com/questions/55986/…Mohammad Nur– Mohammad Nur2018年09月10日 12:39:09 +00:00Commented Sep 10, 2018 at 12:39
Serial.println("Hi!, I am Arduino");
.... put in more debugging code to determine what is being received