Skip to content

Instantly share code, notes, and snippets.

@bryan-lunt
Last active August 29, 2015 14:02
Show Gist options
  • Save bryan-lunt/03e755c1f498068a539b to your computer and use it in GitHub Desktop.
Save bryan-lunt/03e755c1f498068a539b to your computer and use it in GitHub Desktop.
Electric focus for my telescope
#include <Servo.h>
/*
The buttons switch in a voltage divider so that the left and right buttons both use the same pin.
The speed controller is a potentiometer.
/*
const int DIRECTION_BUTTONS = A5;
const int SPEED_DIAL = A6;
const int FOCUS_SERVO_PIN = 2;
const int FOCUS_SERVO_CENTER_PULSE_uS = 1500;
const int FOCUS_SERVO_FULL_CLOCKWISE_PULSE_uS = 1300;
const int FOCUS_SERVO_FULL_ANTICLOCK_PULSE_uS = 1700;
const float CENTER = 90.0;
Servo focus_servo;
void setup(){
focus_servo.attach(FOCUS_SERVO_PIN,FOCUS_SERVO_FULL_CLOCKWISE_PULSE_uS,FOCUS_SERVO_FULL_ANTICLOCK_PULSE_uS);
//For exact calibration of the servo
//focus_servo.writeMicroseconds(FOCUS_SERVO_CENTER_PULSE_uS);
focus_servo.write(CENTER);
}
int checkButton(int which){
int retval = 0;
int buttonVoltage = analogRead(which);
if( buttonVoltage > 300 && buttonVoltage < 600){
retval = 1;
}
if( buttonVoltage > 700){
retval = 2;
}
return retval;
}
void loop(){
int buttonState = 0;
float speedDial = 0;
float maxSpeed = 5.0;
float servoSpeed = 0.0;
buttonState = checkButton(DIRECTION_BUTTONS);
speedDial = (1024 - analogRead(SPEED_DIAL))/1023.0;
if(buttonState == 0){
focus_servo.write(CENTER);
}else if(buttonState == 1 || buttonState == 2){
servoSpeed = speedDial*maxSpeed;
if( buttonState == 2){
servoSpeed *= -1.0;
}
if( buttonState == 1){
servoSpeed += 1.0;
}
focus_servo.write(CENTER + servoSpeed);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment