Noob code issue

Hi all,
I’m writing my first code for an Imp - trying to get a stepper motor rotate through a TC4469. Pin1 and Pin2 of the Imp are used as triggers of the TC4469. But I’m having some code issue, the code is (check the comment for the error):
`
n = 0;
//the bit state of the pins 1 & 2 at each state
const patts = “\x00\x02\x03\x01”;

function turn_step(dir)
{
if (dir) {
if (n == 0) {
n = 3;
} else {
–n;
}
} else {
if (n == 3) {
n = 0;
} else {
++n;
}
}
//the issue is here: “stack overflow, cannot resize stack while in a metamethod” after 2-3 loops
hardware.pin1.write((patts[n] >> 0) & 0x01);
hardware.pin2.write((patts[n] >> 1) & 0x01);
// Schedule the next state change
imp.wakeup(0.1, turn_step(dir));
}

hardware.pin1.configure(DIGITAL_OUT);
hardware.pin2.configure(DIGITAL_OUT);

// Register with the server
imp.configure(“Stepper Rotate”, [], []);

// Start rotating CW
turn_step(1);
`

Where is the mistake? also, could someone explain me the difference in the configuration of the pins as DIGITAL_OUT vs. DIGITAL_OUT_OD_PULLUP?
Thank you!

imp.wakeup(0.1, turn_step(dir))

That doesn’t do what you think it does. It doesn’t call turn_step(dir) 0.1s later. It calls turn_step(dir) right away, expecting it to return a function, and then that function gets called 0.1s later.

But turn_step() doesn’t return a function – in fact it never returns at all, because each call to turn_step() includes another call to turn_step() right away, over and over again until the stack runs out.

What you maybe meant was:
imp.wakeup(0.1, function() { turn_step(dir); } ); – though even that isn’t ideal, as you can’t then call turn_step(-1) without having two lots of turn_steps fighting each other. The best thing to do is probably make dir be a global variable.

Peter

Thank you Peter…that fixed the problem - I made dir global.
Another thing - the way I’m extracting the first and second bit of the array item patts[n], is correct? Any better way to do so?