// For: https://forum.arduino.cc/t/problem-with-nema-17-and-potentiometer/1031505
//
// Formatted the text a little
#define stepPin 6
#define dirPin 3
#define POT A6
int pot;
int prev;
int delta;
int maxim;
int sensorValue = 0; // the sensor value
int sensorMin = 250; // minimum sensor value
int sensorMax = 800; // maximum sensor value
void setup()
{
// Sets the two pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
Serial.begin (9600);
delay (1000);
}
void loop()
{
pot = analogRead (POT);
pot = constrain(pot, sensorMin, sensorMax);
pot = map(pot, sensorMin, sensorMax, 0, 1000);
pot = pot / 4 * 4;
delta = pot - prev;
prev = pot;
Serial.println (pot);
// Serial.print ("ACTUAL: ");
if (abs(delta) > 1)
{
//Serial.println ("VARIACION");
if (delta >= 0)
{
digitalWrite(dirPin, HIGH);
for (int x = 0; x < abs(delta); x++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(150);
digitalWrite(stepPin, LOW);
delayMicroseconds(150);
}
}
else if (delta <= 0)
{
digitalWrite(dirPin, LOW);
for (int x = 0; x < abs(delta); x++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(150);
digitalWrite(stepPin, LOW);
delayMicroseconds(150);
}
}
}
}