cannot communicate via serial port with Arduino Uno to control servo motor!!!!

:smiley-f
Hi everybody, i have an Arduino Uno and control 2 servo motor.
I follow the servo wiki . My Arduino code is as below:

#include <Servo.h>

Servo servoPan;
Servo servoTilt;
void setup() { 
                 servoPan.attach(9); // pan servo is on pin 9
                 servoTilt.attach(10); // tilt servo is on pin 10
                 servoPan.write(90); // home both servos to center
                 servoTilt.write(90); // home both servos to center
                }

void loop() {
                 if( Serial.available() >= 2 ) { // two bytes waiting for us
                 int pan = Serial.read(); // 1st byte is Pan position
                 int tilt = Serial.read(); // 2nd byte is Tilt position
                 servoPan.write(pan); // move pan servo
                 servoTilt.write(tilt); // move tilt servo
                 }
                }

Moderator edit: ITALIC TAGS removed and CODE TAGS inserted.

and i use the command line to write byte to /dev/ttyACM0 :
printf '%b' '\x00' > /dev/ttyACM0
printf '%b' '\xa4a4' > /dev/ttyACM0

The problem is whenever i open the serial monitor on Arduino Workspace, this work fine but when i closed the serial monitor, i cannot control servo motor via sending byte to /dev/ttyACM0.

Could anyone show me how can i fix this bug :frowning:
Thanks so much!

hello
If you are specting 2 bytes this code here get all the 2 bytes!!

if( Serial.available() >= 2 ) { // two bytes waiting for us
int pan = Serial.read(); // 1st byte is Pan position
int tilt = Serial.read(); // 2nd byte is Tilt position

I remember you that an Int variable are 2 bytes, then int pan will store the 2 first bytes
See arduino Int Doc

int
Description
Integers are your primary datatype for number storage, and store a 2 byte value. This yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15) - 1).

About the Serial question if or skectks is waiting for Serial infor how you would expect it to work if you close the serial!!?

It could be auto-reset - those command lines open and close the file each time they run and thus may be toggling the reset line via the auto-reset circuit. Try a controlling program that keeps the file open between commands...

MarkT:
It could be auto-reset - those command lines open and close the file each time they run and thus may be toggling the reset line via the auto-reset circuit. Try a controlling program that keeps the file open between commands...

Thanks MarkT, but i don't know how to keep the dev file open between commnad.
Could yould point out for me at this bug.
I'm very thankful :slight_smile:

HugoPT:
hello
If you are specting 2 bytes this code here get all the 2 bytes!!

if( Serial.available() >= 2 ) { // two bytes waiting for us
int pan = Serial.read(); // 1st byte is Pan position
int tilt = Serial.read(); // 2nd byte is Tilt position

I remember you that an Int variable are 2 bytes, then int pan will store the 2 first bytes
See arduino Int Doc

int
Description
Integers are your primary datatype for number storage, and store a 2 byte value. This yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15) - 1).

About the Serial question if or skectks is waiting for Serial infor how you would expect it to work if you close the serial!!?

Thanks HugoPT, it's just only the demo code, in fact i just use one byte to control Pan and Tilt
anyway , could you help me to fix this bug ?

I remember you that an Int variable are 2 bytes, then int pan will store the 2 first bytes

No, it won't. Each call to Serial.read() returns one byte, regardless of the size of the receiving variable.

duc_bkav:

MarkT:
It could be auto-reset - those command lines open and close the file each time they run and thus may be toggling the reset line via the auto-reset circuit. Try a controlling program that keeps the file open between commands...

Thanks MarkT, but i don't know how to keep the dev file open between commnad.
Could yould point out for me at this bug.
I'm very thankful :slight_smile:

Easy answer is to disable the auto reset, several ways of doing it, a 10 uF (1uF to 100uF will work) electrolytic capacitor between GND and Reset works well. It doesn't matter what your software is doing then. You'll need to remove it to upload a new sketch......

Each call to Serial.read() returns one byte, regardless of the size of the receiving variable

Strictly speaking, each call to Serial.read returns one int, but assuming that there is data in the serial rx buffer, that int will contain a value that will fit into a byte, ie. 0..255

AWOL:

Each call to Serial.read() returns one byte, regardless of the size of the receiving variable

Strictly speaking, each call to Serial.read returns one int, but assuming that there is data in the serial rx buffer, that int will contain a value that will fit into a byte, ie. 0..255

True. I looked it up after I'd submitted the post. It's less wrong than the original point though.

pluggy:

duc_bkav:

MarkT:
It could be auto-reset - those command lines open and close the file each time they run and thus may be toggling the reset line via the auto-reset circuit. Try a controlling program that keeps the file open between commands...

Thanks MarkT, but i don't know how to keep the dev file open between commnad.
Could yould point out for me at this bug.
I'm very thankful :slight_smile:

Easy answer is to disable the auto reset, several ways of doing it, a 10 uF (1uF to 100uF will work) electrolytic capacitor between GND and Reset works well. It doesn't matter what your software is doing then. You'll need to remove it to upload a new sketch......

Hi bro,
I have fix this bug when i use qextserialport library on Qt GUI and control smoothly the Pan Tilt Pod.
But now there is other problem when i try to extend the coverage of the Pan Tilt Zoom Camera, is use a 5m USB cable and connect the Pan Titl Pod with PC.
The first time i run program succesfully, but a bit later, i can't control the Pod. Check the dev using ls /dev command, there is another ttyACM1 device.
I guess that the arduino auto reset and register new device. So i can't continue to control my old ttyACM0 device from Qt GUI.
Check the voltage on the board, it's still approximately 7V , 0.5V lower than the voltage on USB interface.

So i don't know what is the problem here. Did the board isn't provided enough power and it auto reset ? What is the cause ?
Could anyone help me . Thank so much ?