Hi,
I a newbie in programing and the Arduino world.
I'm trying to get a Pan & Tilt to work with Arduino and a Joystick by sending the commands in ASCII using a RS232 Shield, as per the technical information from the Pan&Tilt manufacturer.
But I'm running into some troubles and I'm trying to figure what I'm doing wrong.
First I've tested the program by using a simple Serial.print function and verifying the results on the serial monitor. So far so good: When I move the joystick the serial monitor displays the right command according to the position of the joystick.
Then I replaced the Serial.print for mySerial.print and connected and then connect the Pan & Tilt, nothing happens.
I've been doing a few test but nothing seems to work.
From what I understand I can not have USB connected to the Computer, so I did that.
Since the manufacturer says Data + and Data - instead of Rx and Tx. I tried that also both ways.
Nothing
I want to share the information of what I'm doing and maybe some one can catch what I'm doing wrong.
Thank you!!
The code
#include <SoftwareSerial.h>
// Defines new serial Port not to interfeer with USB Digital Pin 2 = RX, Digital Pin 3 = TX
SoftwareSerial mySerial = SoftwareSerial(2, 3);
int Xpin = A0; // Joystick X (horizontal)
int Ypin = A1; // Joystick Y (vertical)
int Xval;
int Yval;
int restX = 512; // Joystick X rest position value
int restY = 512; // Joystick Y rest position value
int deadZoneX = 40; // Dead zone for X (without movement)
int deadZoneY = 40; // Dead zone for Y (without movement)
int dt = 200; // Delay time, This should be 200 (ms)
void setup() {
// open serial comunication
Serial.begin(9600);
// Define pin modes for Xpin and Ypin
pinMode(Xpin, INPUT);
pinMode(Ypin, INPUT);
// Define pin modes for TX and RX
pinMode(2, INPUT);
pinMode(3, OUTPUT);
// Set the baud rate for the SoftwareSerial object
mySerial.begin(9600);
}
void loop() {
Xval = analogRead(Xpin);
Yval = analogRead(Ypin);
delay(dt);
// Set Pan Tilt Speed
mySerial.println("!BMSP0001W\r\n"); // Sets motor speed value 1 (slowest); change this value up to 12 for faster speed
// if Joystick is moved left or right, move Horizontal stepper (Pan)
if (Xval > (restX + deadZoneX)) { // Xval < 512 + 40 = 552
mySerial.print("#AMMF0000W\r\n"); // Pan clockwise
}
else if (Xval < (restX - deadZoneX)) { // Xval < 512 - 40 = 472
mySerial.print("#AMMB0000W\r\n"); // Pan anti-clockwise
}
else {
mySerial.print("#AMST0000W\r\n"); // Pan Motor Stop
}
// if Joystick is moved up or down, move Vertical stepper (Tilt)
if (Yval > (restY + deadZoneY)) { // Yval < 512 + 40 = 552
mySerial.print("$AMMF0000W\r\n"); // Tilt Forward
}
else if (Yval < (restY - deadZoneY)) { // Yval < 512 - 40 = 472
mySerial.print("$AMMB0000W\r\n"); // Tilt Backward
}
else { // YVal >= 472 $$ YVal <= 552
mySerial.print("$AMST0000W\r\n"); // Tilt Motor Stop
}
}
Pan & Tilt Information
Pin 1 = GND
Pin 2 = +24VDC
Pin 3 = Spare
Pin 4 = DATA +
Pin 5 = DATA -
Pan&Tilt RS232/RS485 Communications Protocol
- Supports device addressing, up to 52 instances of each of 4 types of equipment.
- All communications in ASCII characters (dumb terminal compatible).
- All commands and responses are same length- 12 bytes.
- All commands and data are echoed by the device being addressed.
- Devices never respond unless prompted first by host.
- RS485 hardware interface.
- No error correction except for echo from addressed device.
- Only a single command is allowed per command string.
- Default to 9600,N,8,1.
| Movement description | ASCII | Hex |
I Pan Motor I # I 0x23h I
I Tilt Motor I $ I 0x24h I
I Pan Motor I ! I 0x21h I
Examples of strings
(These may be entered using a ‘dumb’ terminal with line feeds and carriage returns enabled.)
Desired Action / Command String
Pan motor ‘A’ forward / #AMMF0000W
Schematics