# Troubles delaying motor steps

Hi, I have some troubles trying to step my stepper motor with arduino
My project is a barndoor star tracker, it looks like this :
https://cdn.instructables.com/FC4/PKNE/HOHYJG7D/FC4PKNEHOHYJG7D.MEDIUM.jpg

In order to rotate the board with at all time, a linear rotational speed, I need to control the time between each steps because with the angle decreasing, the time must increase.

Here’s the code i made int motorPin[4] = {6, 8, 10, 12};int ccw[8][4] ={{HIGH, LOW, LOW, HIGH}, - Pastebin.com

You have a variable called 'beam' in setup() which you initialize but never use.

You are using 'dt' before giving it a value.

Why "while (micros() > T2 + t)"?!? Did you mean "if (micros() - T2 >= t)" to do something every 't' microseconds? Always subtract to get an interval and test the interval so you don't run into trouble with integer overflows. You should make 't' an unsigned long rather than a double.

If you want consistent timing, use "T2 += t;" instead of "T2 = micros();". That way if the step gets delayed by a few microseconds the next step will be shorter by that number of microseconds.

Be warned that 'double' on the Arduino has no more resolution than 'float'. Expect between 6 and 7 digits of precision.

In case anyone else is interested, this is the sketch the way it was SUPPOSED to be posted. You might want to ask a moderator to move this whole thread to Programming Questions.

``````int motorPin[4] = {6, 8, 10, 12};
int ccw[8][4] =
{
{HIGH, LOW, LOW, HIGH},  // Half Step
{LOW , LOW, LOW, HIGH},  // Half Step
{LOW , LOW, HIGH, HIGH},  // Half Step
{LOW , LOW, HIGH, LOW},  // Half Step
{LOW , LOW, HIGH, LOW},   // No step  ?!?
{LOW , HIGH, HIGH, LOW},  // Half Step
{HIGH, HIGH, LOW , LOW},  // Full step ?!?
{HIGH, LOW, LOW , LOW}  // Half Step
};

long unsigned T1, T2;
long unsigned ip, np, npMin;
double dt, t, phi, phinow, dphi;

void setup() {
// constantes
double inch2cm = 2.54; // 1 pouce = 2.54 cm

// moteur
int ppr = 4096; // inpulsions par revolution (reduction = 64)

// geometrie
double tpi; // pas par pouce
double beam; // charnière - vis
double y0; // depassement de vis initial en cm
double y1; // depassement de vis final en cm

tpi = 25.4;
beam = 17.75;
y0 = 19.5;
y1 = 1.0;
phi = atan2(195, 177.5) * 180 / 3.14159265; //angle 0 en deg

np = ((y0 - y1) / inch2cm) * tpi * ppr; // impulsions de y0 à y1
ip = 0; // conpteur d'impulsions

// pin moteur en sortie
for (int j = 0; j < 4; j++)
pinMode(motorPin[j], OUTPUT);

analogReference(DEFAULT);

T1 = millis(); // temps en µs, ms
T2 = micros();
}

void loop() { //--------------------------------------------------------------------------------
Serial.begin(9600);
Serial.println(np);

while (ip <= np) {
int ic = ip % 8; // utilise une ligne de la matrice a chaque pas, modulo 8  / We use one matrix line per step

// stepping
t = 1000000 * dt;
Serial.println("zone 1 pass"); //----- ZONE 1
while (micros() > T2 + t) {
T2 = micros();
Serial.println("zone 2 pass"); //----- ZONE 2
for (int j = 0; j < 4; j++) {
digitalWrite(motorPin[3 - j], ccw[ic][j]);
}
}
ip = ip + 1;
// Determine phinow et delta phi

phinow = atan2(195 - (ip / 512), 177.5) * 180 / 3.14159265; // phi apres phasage en deg
dphi = phi - phinow;

// Determine dt
dt = dphi * 86164.1 / 360; //temps que met la terre pour effectuer dphi / Time the earth take to rotate dphi

//affichage
while (millis() > T1 + 500) {
T1 = millis();
Serial.print("ip = ");
Serial.println(ip);
Serial.print("dphi = ");
Serial.println(dphi);
Serial.print("dt= ");
Serial.println(dt);
Serial.print("phinow= ");
Serial.println(phinow);
Serial.print("phi= ");
Serial.println(phi);
Serial.println(" ");
}
phinow = phi;
}
}
``````