#include <AccelStepper.h>
// Pins configuration
const int LED_SIDEREAL_TRACK = 3;
const int LED_SUN_TRACK = 4;
const int LED_MOON_TRACK = 5;
const int BUTTON_SELECT_TRACKING = 6;
const int BUTTON_EAST = 7;
const int BUTTON_WEST = 8;
const int BUTTON_SPEED = 9;
const int LED_SPEED = 10;
const int STEP_PIN = 11;
const int DIR_PIN = 12;
// Tracking rates (steps/s)
// SIDEREAL_RATE = 15.041 arcseconds per second
// SOLAR_RATE = 15.000 arcseconds per second
// MOON_RATE = 14.685 arcseconds per second
const float SIDEREAL_RATE = 13.59266936;
const float SOLAR_RATE = 13.55555556;
const float MOON_RATE = 13.27088885;
float trackSpeed[] = {SIDEREAL_RATE, SOLAR_RATE, MOON_RATE};
// Movement speed
// aprox: x2, x5, x10, x50 sidereal rate
float moveSpeeds[] = {27, 72.5, 145, 725};
int SPEED_MOVE=0;
int SPEED_TRACK=0;
int buttonState=0;
AccelStepper myStepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN);
void setup() {
pinMode(LED_SIDEREAL_TRACK, OUTPUT);
pinMode(LED_SUN_TRACK, OUTPUT);
pinMode(LED_MOON_TRACK, OUTPUT);
pinMode(LED_SPEED, OUTPUT);
pinMode(BUTTON_SELECT_TRACKING, INPUT);
pinMode(BUTTON_EAST, INPUT);
pinMode(BUTTON_WEST, INPUT);
pinMode(BUTTON_SPEED, INPUT);
digitalWrite(LED_SPEED, HIGH);
digitalWrite(LED_SIDEREAL_TRACK, HIGH);
digitalWrite(LED_SUN_TRACK, LOW);
digitalWrite(LED_MOON_TRACK, LOW);
myStepper.setMaxSpeed(10000);
myStepper.setSpeed(trackSpeed[SPEED_TRACK]);
}
void loop() {
for (int i=0; i<=SPEED_MOVE; i++) {
digitalWrite(LED_SPEED, HIGH);
myDelay(millis(), 150);
digitalWrite(LED_SPEED, LOW);
myDelay(millis(), 300);
}
myDelay(millis(), 2000);
}
void myDelay (long initTime, long duration) {
while (millis() - initTime < duration) {
myStepper.runSpeed();
// East movement
while (digitalRead(BUTTON_EAST)) {
myStepper.setSpeed(moveSpeeds[SPEED_MOVE]+trackSpeed[SPEED_TRACK]);
myStepper.runSpeed();
myStepper.setSpeed(trackSpeed[SPEED_TRACK]);
}
// West movement
while (digitalRead(BUTTON_WEST)) {
myStepper.setSpeed(-moveSpeeds[SPEED_MOVE]-trackSpeed[SPEED_TRACK]);
myStepper.runSpeed();
myStepper.setSpeed(trackSpeed[SPEED_TRACK]);
}
// Movement speed selection
buttonState = digitalRead(BUTTON_SPEED);
if (buttonState == HIGH) {
SPEED_MOVE=(SPEED_MOVE+1)%4;
long timePress=millis();
while (millis() - timePress < 500) {
myStepper.runSpeed();
delay(10);
}
break;
}
// Tracking speed selection
if (digitalRead(BUTTON_SELECT_TRACKING) == HIGH) {
SPEED_TRACK = (SPEED_TRACK+1)%3;
myStepper.setSpeed(trackSpeed[SPEED_TRACK]);
switch(SPEED_TRACK) {
case 0:
digitalWrite(LED_SUN_TRACK, LOW);
digitalWrite(LED_MOON_TRACK, LOW);
digitalWrite(LED_SIDEREAL_TRACK, HIGH);
break;
case 1:
digitalWrite(LED_SIDEREAL_TRACK, LOW);
digitalWrite(LED_MOON_TRACK, LOW);
digitalWrite(LED_SUN_TRACK, HIGH);
break;
case 2:
digitalWrite(LED_SIDEREAL_TRACK, LOW);
digitalWrite(LED_SUN_TRACK, LOW);
digitalWrite(LED_MOON_TRACK, HIGH);
break;
}
long timePress2=millis();
while (millis() - timePress2 < 500) {
myStepper.runSpeed();
delay(10);
}
}
}
}