HI everyone, I am gaining a really good understanding of how to drive steppers here (I think).
My goal is drive a dolly along a track with variable speed using a Potentionmeter and the EasyDiver, its all wired and can run the stepper in both forward and reverse at any speed right now - If I hard Code it!
What I would like is to control the delay between steps using a Potentiomenter. WIth the Sketch below I am able to control the delay with the POT nicely, although I am having some trouble with the math - at its slowest I would like a 500th of second between steps and at its fastest almost no delay.
I am also not able to change the direction of the stepper, how would I reverse the direction with the second if statement?
If someone could take a look at it and let me know If im going in the right direction, I would really appreciate your help.
//////////// Drive Stepper with Pot at 1600 steps per rev using the EasyDriver
//////////// Stepper full rotation is 200steps / 1600 with easy driver
/// Pot defines delay between steps while running at 1600 steps per revolution
// 0-512 > forward with 0 being the shortest delay and 510 being the longest
// < 513-1023 reverse with 512 having the longest delay and 1023 shortest delay between steps
// shortest delay 1000ms
// longest delay 100ms
///////// What I need to code
//////// var for delay based on POT reading 100ms-1000ms
//////// int stepDelay = pot value converted to micro seconds
// int sensorReading = analogRead(A0);
// int knobReading = map(sensorReading, 0, 1023, 0, 100);
///////// convert POT value to microseconds
// the conversion math???
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
#define DIR_PIN 2
#define STEP_PIN 3
void setup() {
lcd.begin(16, 2);
pinMode(DIR_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
}
void loop(){
// read the sensor value:
int sensorReading = analogRead(A0);
int knobReading = map(sensorReading, 0, 1023, 10, 1000);
int stepDelay = knobReading * 10;
lcd.setCursor(0, 0);
if (knobReading < 500) {
// display pot value
lcd.setCursor(0, 0);
lcd.print("Fwd:" );
lcd.setCursor(5, 0);
lcd.print(knobReading);
// display delay
lcd.setCursor(0, 1);
lcd.print("Delay:" );
lcd.print(stepDelay);
// drive the stepper FORWARD using stepDelay
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(stepDelay);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(stepDelay);
}else{
if (knobReading > 500) {
// display pot value
lcd.setCursor(0, 0);
lcd.print("Rev:" );
lcd.setCursor(5, 0);
lcd.print(knobReading);
// display delay
lcd.setCursor(0, 1);
lcd.print("Delay:" );
lcd.print(stepDelay);
// drive the stepper REVERSE using stepDelay
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds(stepDelay);
digitalWrite(STEP_PIN, LOW);
delayMicroseconds(stepDelay);
}
}
}