// no motor library but blocking loop
#define stepsPerRev 200
#define buttonPin 4
byte motPin[] = {2, 3}; // dir, step
byte motPins = sizeof(motPin) / sizeof(motPin[0]);
byte rgbPin[] = {8, 11}; // blu, red
byte rgbPins = sizeof(rgbPin) / sizeof(rgbPin[0]);
bool buttonState = 0;
int maxrot = 5, delayTime = 2000;
void setup() {
randomSeed(analogRead(A0));
Serial.begin(115200);
for (int i = 0; i < motPins; i++) {
pinMode(motPin[i], OUTPUT);
pinMode(rgbPin[i], OUTPUT);
}
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == LOW)
stepSetup();
}
void stepSetup() {
int speed = random(200, 2000); // motor speed (minimum, maximum)
int rotations = random(maxrot) + 1; // number of rotations +1 for non-zero
int direction = random(2); // direction 0 = CW or 1 = CCW
displayRotation(speed, rotations, direction);
Serial.print(" (pause ");
Serial.print(delayTime);
Serial.print("ms) ");
delay(delayTime);
displayRotation(speed, rotations, !direction);
Serial.println();
}
void rotate(int spd, int rot, bool dir) {
digitalWrite(motPin[0], dir);
for (int i = 0; i < rot * stepsPerRev; i++) {
digitalWrite(motPin[1], HIGH);
delayMicroseconds(spd);
digitalWrite(motPin[1], LOW);
delayMicroseconds(spd);
}
}
void displayRotation(int spd, int rot, bool dir) {
Serial.print("DIRECTION: ");
Serial.print((dir == 0) ? "CCW" : " CW");
Serial.print(" ROTATIONS: ");
Serial.print(rot);
Serial.print(" SPEED: ");
if (spd < 1000) Serial.print(" ");
Serial.print(spd);
digitalWrite(rgbPin[dir], HIGH); // LED on
rotate (spd, rot, dir);
digitalWrite(rgbPin[dir], LOW); // LED off
}
/*
+-------------------------+
LOW=DSBL | ENABLE A4988 VMOT | 8-35vdc ---+ 100uf electro cap
microstep | MS1 GND | gnd -------+ pwr spike protect
microstep | MS2 2B | stepper 2B
microstep | MS3 2A | stepper 2A
RST | RESET 1A | stepper 1A
SLP | SLEEP 1B | stepper 1B
Pin 3 | STEP VDD | 5vdc
Pin 2 | DIR GND | gnd
+-------------------------+
MS1 MS2 MS3 Step resolution
Low Low Low Full step
High Low Low 1/2 step
Low High Low 1/4 step
High High Low 1/8 step
High High High 1/16 step
SET DRIVER CURRENT LIMIT
1. Power A4988 driver (Vdd, GND)
2. Connect RST to SLP
3. Disconnect motor
4. Apply USB power
5. Calculate current limit: Current Limit = Vref ÷ (8 × Rcs)
6. A current limit of 1A (standard) needs Vref of 540mV
7. Adjust POT while measuring Vref at GND to POT (metal screw)
8. If the motor is making a lot of noise, lower the current limit
*/
PRESS