Created
October 24, 2022 20:14
-
-
Save kachurovskiy/051c59d85bc0dd34c6e6f83eb4a837cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define STEP 12 | |
void setup() { | |
// 200 step per revolution motor driver on this pin. | |
pinMode(STEP, OUTPUT); | |
} | |
void topSpeed_1Turn() { | |
for (int i = 0; i < 200; i++) { | |
// Issue 1 step. | |
digitalWrite(STEP, LOW); | |
digitalWrite(STEP, HIGH); | |
// 0.5ms, 2k steps per sec. = 10 rotations per sec. = 600 RPM | |
delayMicroseconds(500); | |
} | |
} | |
void loop() { | |
// Infinite acceleration, just go! | |
// Doesn't work, motor usually stalls. | |
topSpeed_1Turn(); | |
delay(1000); | |
// Reduce delay by N every step. Acceleration sharply increases. | |
for (int i = 0; i < 187; i++) { | |
// Issue 1 step. | |
digitalWrite(STEP, LOW); | |
digitalWrite(STEP, HIGH); | |
// 600 RPM after 187 steps in 0.23 sec. | |
delayMicroseconds(max(500, 2000 - i * 8)); | |
} | |
topSpeed_1Turn(); | |
delay(2000); | |
// Start slow. | |
long delayUs = 2000; | |
// Constant acceleration. | |
long acceleration = 20; | |
for (int i = 0; i < 94; i++) { | |
digitalWrite(STEP, LOW); | |
// digitalWrite() is slow enough that we don't need to wait. | |
digitalWrite(STEP, HIGH); | |
// 600 RPM after 94 steps in 0.07 sec. | |
delayUs = min(2000, max(500, 1000000 / (1000000 / delayUs + acceleration * delayUs / 1000))); | |
delayMicroseconds(delayUs); | |
} | |
topSpeed_1Turn(); | |
delay(3000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment