////////////////////////////////////////////////////////// // // STEPPER MOTOR CONTROLLER FOR MASTWERKS ROTOR // HB9MTN // 31.01.2023 // ////////////////////////////////////////////////////////// // A good tutorial to start with Big Easy Driver (BED): // http://www.schmalzhaus.com/EasyDriver/Examples/EasyDriverExamples.html // // and for the NEOPIXEL Ring: // https://github.com/adafruit/Adafruit_NeoPixel // // // LET'S GO: #include #ifdef __AVR__ #include #endif #define PIN 6 // LED Ring Data pin #define NUMPIXELS 16 // Number of LEDs in array Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); #define DELAYVAL 500 // Time (in milliseconds) to pause between pixels const int TargetSteps = 677; // Target 22.5° per Button Push, to be adjusted [677] int Del = 2000; int StepCounter = 0; int Stepping = false; int Azimuth = 0; int Dirsign = 0; // 16 LED Ring int i = 0; // LED address in array int j = 30; // Offset to keep the math allways > 0 void setup() { Serial.begin(112500); pinMode(2, INPUT); pinMode(5, INPUT); pinMode(8, OUTPUT); pinMode(9, OUTPUT); pinMode(10, OUTPUT); pinMode(11, OUTPUT); pinMode(12, OUTPUT); pinMode(13, OUTPUT); digitalWrite(2, HIGH); // CW BUTTON digitalWrite(5, HIGH); // CCW BUTTON digitalWrite(8, LOW); // DIR digitalWrite(9, LOW); // STEP digitalWrite(10, LOW); // M3 digitalWrite(11, LOW); // M2 digitalWrite(12, HIGH); // M1 digitalWrite(13, HIGH); // DISABLE BED #if defined(__AVR_ATtiny85__) && (F_CPU == 16000000) clock_prescale_set(clock_div_1); #endif pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED) pixels.clear(); // Set all pixel colors to 'off' pixels.setPixelColor(i, pixels.Color(0, 80, 0)); delay(1000); // allow time to start up } void loop() { pixels.show(); // Send the updated pixel colors to the hardware. if (digitalRead(2) == LOW && Stepping == false && digitalRead(5) == HIGH) { digitalWrite(8, HIGH); // DIR CW // if rotation is not CW, change to "LOW" Stepping = true; if ((j == 15) && (i == 1)) // check for limit { Dirsign = 1; Stepping = false; } else { Dirsign = -1; Stepping = true; } } if (digitalRead(5) == LOW && Stepping == false && digitalRead(2) == HIGH) { digitalWrite(8, LOW); // DIR CCW // if rotation is not CCW, change to "HIGH" Stepping = true; if ((j == 45) && (i == 15)) // check for limit { Dirsign = -1; Stepping = false; } else { Dirsign = 1; Stepping = true; } } if (Stepping == true) { digitalWrite(13, LOW); digitalWrite(9, HIGH); delayMicroseconds(Del); digitalWrite(9, LOW); delayMicroseconds(Del); StepCounter = (StepCounter + 1); Serial.println(StepCounter); if (StepCounter == TargetSteps) { StepCounter = 0; Stepping = false; digitalWrite(13, HIGH); j = (j + 1 * Dirsign); //i = (i + 1 * Dirsign); //pixels.clear(); // Set all pixel colors to 'off' pixels.setPixelColor(i, pixels.Color(0, 0, 80)); pixels.show(); // Send the updated pixel colors to the hardware. calculateLED(); Serial.println("i = "); Serial.println(i); Serial.println("j = "); Serial.println(j); Serial.println("Dirsign = "); Serial.println(Dirsign); } } } void calculateLED() { if ((j > 29) && (j < 42)) // between 30 and 41 { i = (j - 30); pixels.clear(); // Set all pixel colors to 'off' pixels.setPixelColor(i, pixels.Color(0, 0, 150)); // blue } if ((j > 18) && (j < 30)) // between 19 and 29 { i = (j - 14); pixels.clear(); // Set all pixel colors to 'off' pixels.setPixelColor(i, pixels.Color(0, 0, 150)); // blue } else if ((j < 19) || (j > 41)) // below 19 or above 41 { if (j < 19) { i = (j - 14); // below 19 pixels.clear(); // Set all pixel colors to 'off' pixels.setPixelColor(i, pixels.Color(80, 0, 0)); // red } else if (j > 41) // above 41 { i = (j - 30); pixels.clear(); // Set all pixel colors to 'off' pixels.setPixelColor(i, pixels.Color(80, 0, 0)); // red } } else { // do nothing } }