Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
This post is a summary of how to control the speed of a NEMA-17 stepper motor using a trimpot
(variable resistor). The code is done without using the delay function for reason illustrated clearly at
the very beginning of the Arduino Blink Without Delay page (see below quote).
"Sometimes you need to do two things at once. For example you might want to blink an LED while
reading a button press. In this case, you can't use delay(), because Arduino pauses your program
during the delay(). If the button is pressed while Arduino is paused waiting for the delay() to pass,
your program will miss the button press."
The Schematic
The Code
--------------------------------------------------------------------------------------------------------------
-------
int motorState = LOW; // motorState used to set the motor
unsigned long previousMillis = 0; // Store the last time the stepper motor direction was
changed
void setup() {
Serial.begin(9600);
void loop() {
unsigned long currentMillis = millis(); // Read the current time
int x = analogRead(A3); // Read the trimpot (speed control) value on pin A3 into x
//Serial.print("Analog value: "); // Display the trimpot value via serial monitor
//Serial.print(x);
//Serial.print(" ");
int d = (x/50) * 1; // RPM range is adjusted by changing the multiplier, was 100
// * 1 -> RPM Range: 60 (counter clockwise) ~ 3 (clockwise)
// * 2 -> RPM Range: 60 (counter clockwise) ~ 1 (clockwise)
// Serial.println(d);
https://www.printfriendly.com/p/g/Api4VZ 1/2
11/5/2018 Controlling NEMA-17 stepper motor speed with trimpot without using Delay()
if (d <= 1) d = 1; // Get rid of d = 0 to give enough delay for stepper motor to function properly
//Serial.print("Delay: ");
//Serial.println(d);
// Check to see if it's time to change the motor step state (HIGH -> LOW, LOW -> HIGH). If the
difference between the current time and the last time
// the state was changed is greaer than the interval, it's time to change the state.
digitalWrite(7, motorState);
}
}
--------------------------------------------------------------------------------------------------------------
-------
Reference:
https://www.printfriendly.com/p/g/Api4VZ 2/2