Wireless Follow Focus

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);
}

}

Serial.print(message);

On the sender, this is converting your integer into a string.

servovalue = Serial.read();
 myservo.write(servovalue); //send recieved value to Servo

On the receiver, you are reading the first character and treating it as the complete value. It is not.

You have two choices. Since the sender has mapped the value into a byte-sized value, send the data as a byte. Or, receive the data as a string, and convert the string to an int that is used to position the servo.

Could you show me how to do that, or direct me to somewhere I can learn how to? I'm not really much of a programmer! Thanks :)

You have two choices.

Could you show me how to do that, or direct me to somewhere I can learn how to?

http://shazam.econ.ubc.ca/flip/

Haha, ok! The first option please!

On the sender, you can change the variable type for message (which is a lousy name, by the way) to byte. Then, use Serial.write() in place of Serial.print().

On the receiver, no changes are needed.

I've made the changes and now the Serial Monitor for the sender shows Garbled AASCI characters instead of numerical values. The servo doesn't respond to potentiometer movement. Any more ideas?

[u]SENDER[/u]

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; byte 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.write(position); lastSensorReading = sensorReading; }

delay(15);

}

I’ve made the changes and now the Serial Monitor for the sender shows Garbled AASCI characters instead of numerical values.

That’s because you are not sending ASCII characters, but the serial monitor is trying to interpret them as such.

The serial monitor is not the intended recipient of the serial data, so it doesn’t matter whether it displays it correctly, not not.

What does the receiver do?

You can add the BYTE optional argument to the Serial.print command, to tell the serial monitor that the number is a number, not an ASCII value, for debugging purposes, if you NEED to see the value sent.