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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
#include <AccelStepper.h>
// General definitions
int CurrentFilter = 0; // filter integer
int HallValue; // Hall switch variable
// Colors (RGB format)
const byte COLOR_BLACK = 0b000;
const byte COLOR_RED = 0b100;
const byte COLOR_GREEN = 0b010;
const byte COLOR_BLUE = 0b001;
// Color pins
const byte PIN_LED_R = 9;
const byte PIN_LED_G = 10;
const byte PIN_LED_B = 11;
// Filter steps defintions
int CalibrationOffset = 70; // Steps required to move to first position after calibration is done
int FilterSteps = 3244.5;//819.2; 3186.5 // Steps required to move the filter to next location adjust as required to suit motor wheel combination
int FilterSteps0 = 0; // Set filter steps for home position from calibration
int FilterSteps1 = -FilterSteps; // Set filter steps for 1st position
int FilterSteps2 = -FilterSteps * 2; // Set filter steps for 2nd position
int FilterSteps3 = -FilterSteps * 3; // Set filter steps for 3rd position
int FilterSteps4 = -FilterSteps * 4; // Set filter steps for 4th position
// Hall pin defintion
const int out = 12; // PIN 12 = Hall effect switch
// Motor definitions
#define STEPS 8 // 28BYJ-48 steps 4 or 8
// Motor pin defintions
#define motorPin1 8 // IN1 on UL2003
#define motorPin2 9 // IN2 on UL2003
#define motorPin3 10 // IN3 on UL2003
#define motorPin4 11 // IN4 on UL2003
// Initializa with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper1(STEPS, motorPin1, motorPin3, motorPin2, motorPin4);
void setup() {
// Initialization of color pins
pinMode(PIN_LED_R, OUTPUT);
pinMode(PIN_LED_G, OUTPUT);
pinMode(PIN_LED_B, OUTPUT);
displayColor(COLOR_BLACK);
// Intialize the blinking LED
//pinMode(PIN_LED_BLNK, OUTPUT);
Serial.flush();
Serial.begin(9600); // Baud rate, make sure this is the same as ASCOM driver
stepper1.setMaxSpeed(1000.0);
stepper1.setAcceleration(1000.0); // Acceleration
stepper1.setSpeed(1000.0);
stepperHome(); // runs routine to home position
Serial.println("1#");
}
void loop() {
String cmd;
displayColor(COLOR_BLACK);
if (Serial.available() > 0) {
cmd = Serial.readStringUntil('#'); // Teerminator so arduino knows when the message ends
if (cmd == "GETFILTER") {
Serial.print(CurrentFilter); Serial.println("#"); // Similary, so ASCOM knows
}
else if (cmd == "FILTER0") MoveFilter(0); // Move Filter Routine
else if (cmd == "FILTER1") MoveFilter(1); // Move Filter Routine
else if (cmd == "FILTER2") MoveFilter(2); // Move Filter Routine
else if (cmd == "FILTER3") MoveFilter(3); // Move Filter Routine
else if (cmd == "FILTER4") MoveFilter(4); // Move Filter Routine
else if (cmd == "FILTER5") MoveFilter(5); // Move Filter Routine
}
}
void MoveFilter(int pos) {
CurrentFilter = pos; // Note that the position is always 0 when powered on due to calibration
displayColor(COLOR_BLACK);
switch (CurrentFilter) {
case 0:
digitalWrite(13, HIGH); // flash LEDs for move
stepper1.runToNewPosition(FilterSteps0); // Goto new position
Serial.print(CurrentFilter); Serial.println("#"); // Notify that we sent the command
digitalWrite(13, LOW); // Disable LEDs after Move
// Black Color
displayColor(COLOR_BLACK);
break;
case 1:
displayColor(COLOR_BLACK);
digitalWrite(13, HIGH); // flash LEDs for Move
stepper1.runToNewPosition(FilterSteps1); // Goto new position
Serial.print(CurrentFilter); Serial.println("#"); // Notify rhat we sent the command
digitalWrite(13, LOW); // Disable LEDs after Move
//Red Color
displayColor(COLOR_RED);
break;
case 2:
displayColor(COLOR_BLACK);
digitalWrite(13, HIGH); // flash LEDs for Move
stepper1.runToNewPosition(FilterSteps2); // Goto new position
Serial.print(CurrentFilter); Serial.println("#"); // Notify that we sent the command
digitalWrite(13, LOW); // Disable LEDs after Move
// Green Color
displayColor(COLOR_GREEN);
break;
case 3:
displayColor(COLOR_BLACK);
digitalWrite(13, HIGH); // flah LEDs for Move
stepper1.runToNewPosition(FilterSteps3); // Goto new position
Serial.print(CurrentFilter); Serial.println("#"); // Notify that we sent the command
digitalWrite(13, LOW); // Disable LEDs after Move
// Blue Color
displayColor(COLOR_BLUE);
break;
case 4:
displayColor(COLOR_BLACK);
digitalWrite(13, HIGH); // Flash LEDs for Move
stepper1.runToNewPosition(FilterSteps4); // Goto new position
Serial.print(CurrentFilter); Serial.println("#"); // Notify that we sent the command
digitalWrite(13, LOW);
// Black Color
displayColor(COLOR_BLACK);
break;
case 5:
stepperHome(); // Calibration Routine
Serial.print(CurrentFilter); Serial.println("#"); // Notify that we sent the command
// Black Color
displayColor(COLOR_BLACK);
break;
}
}
void stepperHome()
{
HallValue = digitalRead(out); // read the Hall sensor value
digitalWrite(13, HIGH); // Flash LEDs for Move
while (HallValue == HIGH)
{
stepper1.setAcceleration(1000.0); // Acceleration
stepper1.setSpeed(1000.0);
stepper1.move(20);
stepper1.run();
HallValue = digitalRead(out); // read the Hall sensor value
}
stepper1.setCurrentPosition(0);
stepper1.runToNewPosition(CalibrationOffset);
stepper1.setCurrentPosition(0);
digitalWrite(13, LOW);
displayColor(COLOR_BLACK);
}
// Display one color
void displayColor(byte color) {
// Assigne states of pins
// Commune Cathode version
digitalWrite(PIN_LED_R, bitRead(color, 2));
digitalWrite(PIN_LED_G, bitRead(color, 1));
digitalWrite(PIN_LED_B, bitRead(color, 0));
} |
Partager