copy&paste is often plagued with problems
consider
struct Axis {
const char *desc;
int zero;
int dead;
int potMax;
byte potPin;
byte dirPin;
byte stepPin;
unsigned long usecMin;
unsigned long usecMax;
unsigned long usecLst;
int period;
};
#define MyHW
#ifdef MyHW
Axis axis [] = {
{ "x", 512, 10, 20, A0, 13, 12, 100000, 1000000L },
{ "y", 200, 100, 800, A2, 10, 11, 100000, 1000000L },
};
#else
# define DRIVER_STEP_PIN 2
# define DRIVER_DIR_PIN 3
Axis axis [] = {
{ "x", 512, 10, A0, 3, 2, 50, 3000 },
{ "y", 512, 500, A1, 5, 4, 50, 3000 },
};
#endif
#define N_AXIS 2
unsigned long usec = millis ();
char s[80];
// -----------------------------------------------------------------------------
void
operate (
Axis *p )
{
int pot = analogRead (p->potPin) - p->zero;
digitalWrite (p->dirPin, 0 < pot);
int delta;
delta = abs (pot) - p->dead;
delta = 0 > delta ? 0 : delta;
unsigned long period = map (delta, 0, p->potMax, p->usecMax, p->usecMin);
sprintf (s, " axis %s, pot %5d, del %3d, per %6ld",
p->desc, pot, delta, period );
Serial.print (s);
if (delta && (usec - p->usecLst) > period) {
p->usecLst = usec;
digitalWrite (p->stepPin, ! digitalRead (p->stepPin));
}
}
// -----------------------------------------------------------------------------
void
loop ()
{
usec = micros ();
operate (& axis [0]);
operate (& axis [1]);
Serial.println ();
}
// -----------------------------------------------------------------------------
void setup () {
Serial.begin (9600);
Axis *p = axis;
for (unsigned n = 0; n < N_AXIS; n++, p++) {
pinMode (p->dirPin, OUTPUT);
pinMode (p->stepPin, OUTPUT);
}
}