Hi all, I have stepper moter + Easy Driver and Potentiometer all wired here and am having a little bit of trouble making the speed a variable to use with the potentionmeter
I can - display the pot value on the LCD while turning the POT as well as change direction <50 Rev / 50> forward ![]()
What I would like have happen is the have the POT define the speed of the stepper, so the closer you get to 50, the slower speed, and at the other end of the dial the faster 50 always being almost a stop.
Any ideas? here is the code I currently have
//////////////////////////////////////////////////////////////////
//©2011 bildr
//Released under the MIT License - Please reuse change and share
//Using the easy stepper with your arduino
//use rotate and/or rotateDeg to controll stepper motor
//speed is any number from .01 -> 1 with 1 being fastest -
//Slower Speed == Stronger movement
/////////////////////////////////////////////////////////////////
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
//const int stepsPerRevolution = 200;Â // change this to fit the number of steps per revolution
#define DIR_PIN 2
#define STEP_PIN 3
int steps = 0;Â Â Â Â // number of steps the motor has taken
void setup() {
 pinMode(DIR_PIN, OUTPUT);
 pinMode(STEP_PIN, OUTPUT);
 lcd.begin(16, 2);
}
void loop(){
 // read the sensor value:
 int sensorReading = analogRead(A0);
 // map it to a range from 0 to 100:
 int knobReading = map(sensorReading, 0, 1023, 0, 100);
 // set the motor speed:
Â
 if (knobReading > 50) {
  //////////// REVERSE /////////////////
  lcd.setCursor(0, 0);
  lcd.print("Rev:" );
  lcd.print(knobReading);
  steps++;
  lcd.setCursor(8, 0);
  lcd.print("SPD:");
  lcd.setCursor(0, 1);
  lcd.print("STEPS:");
  lcd.setCursor(6, 1);
  lcd.print(steps);
  rotate(-1600., .1);
 } else {
  if (knobReading < 50){
   ////////////// FORWARD /////////////////
   lcd.setCursor(0, 0);
   lcd.print("Fwd:" );
   lcd.print(knobReading);
   lcd.setCursor(8, 0);
   lcd.print("SPD:");
   steps++;
   lcd.setCursor(0, 1);
   lcd.print("STEPS:");
   lcd.setCursor(6, 1);
   lcd.print(steps);
   rotate(1600., 1);
  }
 }Â
}
void rotate(int steps, float speed){
 //rotate a specific number of microsteps (8 microsteps per step) - (negitive for reverse movement)
 //speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
 int dir = (steps > 0)? HIGH:LOW;
 steps = abs(steps);
 digitalWrite(DIR_PIN,dir);
 float usDelay = (1/speed) * 70;
 for(int i=0; i < steps; i++){
  digitalWrite(STEP_PIN, HIGH);
  delayMicroseconds(usDelay);
  digitalWrite(STEP_PIN, LOW);
  delayMicroseconds(usDelay);
 }
}
void rotateDeg(float deg, float speed){
 //rotate a specific number of degrees (negitive for reverse movement)
 //speed is any number from .01 -> 1 with 1 being fastest - Slower is stronger
 int dir = (deg > 0)? HIGH:LOW;
 digitalWrite(DIR_PIN,dir);
 int steps = abs(deg)*(1/0.225);
 float usDelay = (1/speed) * 70;
 for(int i=0; i < steps; i++){
  digitalWrite(STEP_PIN, HIGH);
  delayMicroseconds(usDelay);
  digitalWrite(STEP_PIN, LOW);
  delayMicroseconds(usDelay);
 }
}