1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| #include <AccelStepper.h>
#define HALFSTEP 8 // 28BYJ-48 needs halfstep 8 to run properly
#define LED A3
#define LEDSTP A1
int CurrentFilter = 0;
// Motor pin definitions
#define motorPin1 8 // IN1 on UL2003
#define motorPin2 9 // IN2 on UL2003
#define motorPin3 10 // IN3 on UL2003
#define motorPin4 11 // IN4 on UL2003
// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
void setup() {
stepper1.setAcceleration(1000.0);
stepper1.setMaxSpeed(1800.0);
Serial.flush();
Serial.begin(9600); // Baud rate, make sure this is the same as ASCOM driver
analogWrite(LEDSTP,1023);
analogWrite(LED,1023);
delay(500);
analogWrite(LEDSTP,0);
analogWrite(LED,0);
}
void loop() {
String cmd;
if (Serial.available() >0) {
cmd = Serial.readStringUntil('#'); // Terminator so arduino knows when the message ends
if (cmd=="GETFILTER") {
Serial.print(CurrentFilter); Serial.println("#"); // Similarly, so ASCOM knows
}
else if (cmd=="FILTER0") MoveFilter(0);
else if (cmd=="FILTER1") MoveFilter(1);
else if (cmd=="FILTER2") MoveFilter(2);
else if (cmd=="FILTER3") MoveFilter(3);
else if (cmd=="FILTER4") MoveFilter(4);
}
}
void MoveFilter(int pos) {
CurrentFilter = pos; // Note that the position is always 0 when powered on so have filters oriented properly
switch (CurrentFilter)
{
case 0:
analogWrite(LEDSTP,0);
analogWrite(LED,1023);
stepper1.run();
stepper1.runToNewPosition(0);
Serial.print(CurrentFilter); Serial.println("#");
analogWrite(LED,0);
analogWrite(LEDSTP,1023);
break;
case 1:
analogWrite(LEDSTP,0);
analogWrite(LED,1023);
stepper1.run();
stepper1.runToNewPosition(6250);
Serial.print(CurrentFilter); Serial.println("#");
analogWrite(LED,0);
analogWrite(LEDSTP,1023);
break;
case 2:
analogWrite(LEDSTP,0);
analogWrite(LED,1023);
stepper1.run();
stepper1.runToNewPosition(12500);
Serial.print(CurrentFilter); Serial.println("#");
analogWrite(LED,0);
analogWrite(LEDSTP,1023);
break;
case 3:
analogWrite(LEDSTP,0);
analogWrite(LED,1023);
stepper1.run();
stepper1.runToNewPosition(18750);
Serial.print(CurrentFilter); Serial.println("#");
analogWrite(LED,0);
analogWrite(LEDSTP,1023);
break;
case 4:
analogWrite(LEDSTP,0);
analogWrite(LED,1023);
stepper1.run();
stepper1.runToNewPosition(25000);
Serial.print(CurrentFilter); Serial.println("#");
analogWrite(LED,0);
analogWrite(LEDSTP,1023);
break;
}
} |