Arduino Follow Focus

This is a repost of a post from about 6 months ago. I forgot about the project when I couldn't get it to work and am trying to pick it up again. Any help would be greatly appreciated!

Hi Everyone!

I'm trying to make a wireless follow focus system for a DSLR using two Arduino boards linked with XBEE modules.

Parts List:
2x Arduino Duemilanove
2x XBEE Series 2 Modules with XBEE Shields
1x 10K Potentiometer
1x HiTec HS-422 Servo

I have managed to get the two Arduinos connected and I used the physical pixel application to confirm they were transmitting/receiving data.

I've written/bodged code to transfer the potentiometers position wirelessly to the second Arduino. However, although the transmitter is sending the potentiometers position correctly, the servo usually just jitters rather than responding to the position changes (although it will occasionally complete a full rotation).

I was wondering if anyone had any experience of this? Or if there are any glaring mistakes in my code?

Thanks :slight_smile:

Sender:

int potpin = 0; // analog pin used to connect the potentiometer
int sensorReading; // variable to read the value from the analog pin
int lastSensorReading;
int threshold = 1;
int message;

void setup()
{
Serial.begin(9600); // Open serial Port
}

void loop()
{
sensorReading = analogRead(potpin); //Read Pot Position
sensorReading = map(sensorReading, 0, 1023, 0, 179); //Map Pot Position to 180 degrees

if (abs(sensorReading - lastSensorReading) > threshold)
{
message = sensorReading; // Only send serial data if change in position is significant
Serial.print(message);
lastSensorReading = sensorReading;
}

delay(15);

}

Reciever:

#include <Servo.h>

Servo myservo;
int servovalue;

void setup() {
Serial.begin(9600); //Open Serial Port
myservo.attach(9); // Servo - Digital Pin 9
}

void loop() {

if (Serial.available() > 0) //Check if data is being recieved
{
servovalue = Serial.read();
myservo.write(servovalue); //send recieved value to Servo
delay(15);
}

}

On the sender, you have

Serial.print(message);

Since there is only one argument, the Serial.print function sends the value as a string. So, a value of 47 is sent as '4' and '7'. The receiver is then reading the '4' as an angle, which corresponds to 52, then '7' as an angle, which corresponds to 55.

If you add the optional second argument, BYTE, the value 47 will be sent as a number, so the receiver will receive 47.

Since the serial data being sent by the sender comes in 15 millisecond increments at the quickest, the delay in the receiver is unnecessary.

Thanks for the advice, I've tried that and now I have:

Sender:

int potpin = 0; // analog pin used to connect the potentiometer
int sensorReading; // variable to read the value from the analog pin
int lastSensorReading;
int threshold = 1;
int position;

void setup()
{
Serial.begin(9600); // Open serial Port
}

void loop()
{
sensorReading = analogRead(potpin); //Read Pot Position
sensorReading = map(sensorReading, 0, 1023, 0, 179); //Map Pot Position to 180 degrees

if (abs(sensorReading - lastSensorReading) > threshold)
{
position = sensorReading; // Only send serial data if change in position is significant
Serial.print(position, BYTE);
lastSensorReading = sensorReading;
}

}

Receiver:

#include <Servo.h>

Servo myservo;
int servovalue;

void setup() {
Serial.begin(9600); //Open Serial Port
myservo.attach(9); // Servo - Digital Pin 9
}

void loop() {

if (Serial.available() > 0) //Check if data is being recieved
{
servovalue = Serial.read();
myservo.write(servovalue); //send recieved value to Servo
}

}

When I move the potentiometer, the servo either doesn't respond, or will occasionally move an arbitrary number of degrees and then stop. Any ideas?

I have just added Serial.print(servovalue); to the receiver so that I can use serial monitor to watch what its sending to the servo. It doesn't seem to be continuous, it looks like a full rotation sends the numbers 127-001, but there is delay between turning the pot and the numbers showing up. I think the XBees are connected ok though??

Having retried Physical pixel, the LED is not switching on and off at regular second intervals. It will stay on for two seconds, flash, then stay off for 2 seconds. Serial monitor on the transmitter shows LHLHLH at regular 1 second intervals. Is this an issue with the Xbee connectivity?

int position;

position holds the angle to send the servo to. It's type should be byte.

Is this an issue with the Xbee connectivity?

How could it be? Clearly the two are talking.

Post the code for both ends.

Ok, changing that variable to byte seems to have sorted it! Thanks for you help :slight_smile: