OpenCV, Arduino and serial communication

Hi everybody,

I'm trying to send some data from a program in python to my Arduino UNO. It's a number between 0 and 500, and the idea is to receive the data in order to move a servo.

The problem is that the data is "decomposed", for example if the number is 278, sometimes it will indeed receive the right number, but sometimes it only receives 2, or 78.

I don't know where the problem may be... I've already tried different methods but I can't resolve the problem. I anyone can help me, I would be great!

Thank you!

That's how I send data with Python:

x, y, w, h = face.faceRect
ser.write(str(x))
ser.write("\n")

Here's the Arduino's code:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#include <Servo.h>
#define servomaxx 160 // max degree servo horizontal (x) can turn
#define screenmaxx 500 // max screen horizontal (x)resolution
#define distancex 10 // x servo rotation steps

int x;
int posx = 0;
int incx = 10; // significant increments of horizontal (x) camera movement
Servo myServo;

void setup(){
lcd.begin(16,2);

myServo.attach(9);
Serial.begin(9600);
myServo.write(90); //Servo 90°
}

void loop(){
if (Serial.available() > 0) {

x = Serial.parseInt();
lcd.print(x);
moveTurret();
}
while (Serial.available() > 0) {
Serial.read();
}
}

void moveTurret() {

// read last servos positions
posx = myServo.read();
Serial.println(posx);

//Find out if the X component of the face is to the right of the middle of the screen.
if(x < (screenmaxx/2 - incx)){
if( posx >= incx )
posx -= distancex; //Update the pan position variable to move the servo to the left.
}

//Find out if the X component of the face is to the left of the middle of the screen.
else if(x > screenmaxx/2 + incx){
if(posx <= servomaxx-incx)
posx +=distancex; //Update the pan position variable to move the servo to the right.
}

myServo.write(posx);
/*

int servoX = map(x, 0, 500, 20, 160); // x varie de 0 à 500 (écran, OpenCV) et nous le transformons en angle de 20° à 160° (capacité servo arduino)
servoX = min(servoX, 170); //Maybe useless
servoX = max(servoX, 20); //Maybe useless
*/

delay(2000);
lcd.clear();

}

What is wrong with presenting your code using the standard code tags (the # button) so it looks like this and is easy to read as explained in How to use the forum

This demo shows how to use Python to communicate with an Arduino.

I suspect your Arduino code is not gathering all of the characters of the number you send - probably because the Arduino has no way to tell the start or the end.

...R

what also might help is use baudrate 115200 iso 9600.
Note that it takes time before all chars are on the other side...

Sorry Robin, won't happen again.

And thank you for your answers!

Hello again,

I couldn't solve the problem.

.

Robin2:
probably because the Arduino has no way to tell the start or the end.

How can I do that? I checked your code ComArduino and I tried to "copy" wht you did there but I didn't worked... Do you know any other way?

I also changed the baud rate but there was no difference...

Anyways, than you for your help!

Flat:
I checked your code ComArduino and I tried to "copy" wht you did there but I didn't worked... Do you know any other way?

As you have not explained in detail what you tried I cannot give you an answer.

But these may be useful questions ...

Did you get my code to work unchanged (apart from putting in the correct seral port reference)?
Did you get your own code to work with my Arduino code - without any changes to the Arduino code?

This sort of problem needs to be tackled one small step at a time starting from something that works.

...R