#include <Stepper.h>
const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution for your motor
// Initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
const int potPin = A0; // the potentiometer is connected to analog pin A0
const int relayPin = 7; // the relay is connected to digital pin 7
int potValue = 0; // variable to store the value coming from the potentiometer
int stepCount = 0; // number of steps the motor has taken
void setup() {
// set the speed at 60 rpm:
myStepper.setSpeed(60);
// initialize the serial port:
Serial.begin(9600);
// initialize the relay pin as an output:
pinMode(relayPin, OUTPUT);
}
void loop() {
// read the value of the potentiometer:
potValue = analogRead(potPin);
// map it to a range from 0 to 100:
int motorSpeed = map(potValue, 0, 1023, 0, 100);
// if the potentiometer reading is above a certain threshold, turn the relay on
if (motorSpeed > 50) {
digitalWrite(relayPin, HIGH);
} else {
digitalWrite(relayPin, LOW);
}
// set the motor speed based on the potentiometer:
if (motorSpeed > 0) {
myStepper.setSpeed(motorSpeed);
// move a number of steps equal to the motorSpeed:
myStepper.step(motorSpeed);
}
// print the potentiometer value and the motor speed to the serial monitor:
Serial.print("Potentiometer Value: ");
Serial.print(potValue);
Serial.print("\t Motor Speed: ");
Serial.println(motorSpeed);
delay(10); // short delay to reduce noise
}