Skip to content

Instantly share code, notes, and snippets.

@benrules2
Created June 6, 2017 11:43
Show Gist options
  • Save benrules2/f1a78eb1230b7415632585b560aaa2ec to your computer and use it in GitHub Desktop.
Save benrules2/f1a78eb1230b7415632585b560aaa2ec to your computer and use it in GitHub Desktop.
Arduino code for star tracking
// Controls a threaded stepper motor to open levered board at same rate the earth spins
// for astrophotograph.
byte directionPin = 3;
byte stepPin = 2;
byte ledPin = 13;
float adjacent = 0.24;
float earth_rads_per_s = 0.00007272205;
float current_step = 0;
float min_step_size = 0.0001 ;
float stepsize = 0.00001 / 8;
float start_time;
int pulseWidthMicros = 50; // microseconds
int microsbetweenSteps = 500; // milliseconds
float take_step_distance(float distance)
{
float step_distance = 0;
int steps = 0;
while (step_distance < distance)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(pulseWidthMicros);
digitalWrite(stepPin, LOW);
delayMicroseconds(microsbetweenSteps);
step_distance += stepsize;
steps++;
digitalWrite(ledPin, !digitalRead(ledPin));
}
return step_distance;
}
float earth_tracking_adjustment(float adjacent, float elapsed)
{
float tan_ = sin(earth_rads_per_s * elapsed);
return adjacent * tan_;
}
void setup()
{
Serial.begin(9600);
Serial.println("Starting startracker");
digitalWrite(ledPin, LOW);
pinMode(directionPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(ledPin, OUTPUT);
start_time = millis();
digitalWrite(directionPin, LOW);
//take_step_distance(0.01);
}
void loop()
{
float elapsed = millis() - start_time;
float total_step = earth_tracking_adjustment(adjacent, elapsed/1000);
float step_required = total_step - current_step;
if ( step_required >= min_step_size)
{
current_step += take_step_distance(step_required);
}
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment