Ok here is the code. I am trying to move a servo round from a home position by 10 degrees at a time, pause, take a photo and so on. The number of photos is set by a variable called panAngle that will eventually be set by the user by a button and an LCD.
It sort of works but usually after a few turns it rotates by about 45 degrees in the wrong direction before carrying on. I have a feeling i am supposed to keep sending a pulse to the servo to get the right angle but I dont know how to do that and go off and fire the camera shutter at the same time. Any help much appreciated. In terms of the circuit I am using a simple opto-isolator for the motor and camera shutter and have referenced the ground of the power supply and arduino board.
//panorama code for HItec 785 and Canon 350
int selectButton = 8;
int panAngle;
int ledPin = 13;
int imageCount;
int shutter = 9;
int servoPin = 1; // Control pin for servo motor
int minPulse = 900; // Minimum servo position
int maxPulse = 2100; // Maximum servo position
int pulse = 0; // Amount to pulse the servo
int refreshTime = 15; // the time needed in between pulses
int servoAngle;
void setup()
{
pinMode (selectButton, INPUT);
pinMode (ledPin, OUTPUT);
pinMode (shutter, OUTPUT);
pinMode(servoPin, OUTPUT); // Set servo pin as an output pin
panAngle = 180;
}
void shoot()
{
digitalWrite(shutter, HIGH);
digitalWrite(ledPin, HIGH);
delay(2000);
digitalWrite(shutter, LOW);
digitalWrite(ledPin, LOW);
}
void homeServo()
{
pulse = minPulse;
digitalWrite(servoPin, HIGH); // Turn the motor on
delayMicroseconds(pulse); // Length of the pulse sets the motor position
digitalWrite(servoPin, LOW); // Turn the motor off
delay(refreshTime);
}
void moveServo()
{
pulse = (((float)servoAngle / (float)1260) * 800) + 900; //work out pulse width - 800 figure arrived at by trial and error but I expected it to be more like 1260 becasuse motor turns 3.5 revs
digitalWrite(servoPin, HIGH); // Turn the motor on
delayMicroseconds(pulse); // Length of the pulse sets the motor position
digitalWrite(servoPin, LOW); // Turn the motor off
delay(refreshTime);
}
void loop()
{
if (digitalRead(selectButton) == HIGH)
{
delay(250); //debounce button
imageCount = panAngle/10;
homeServo();
servoAngle = 0;
while (imageCount>0)
{
moveServo();
delay(500);
shoot();
servoAngle = servoAngle + 10;
imageCount--;
}
}
}