void setup() {
Serial.begin(115200);
Serial.println("Hello World!");
pinMode(3, INPUT_PULLUP);
turnMachineStep(2); // leave it as if it had run once? I give up
}
void loop() {
if (!turnMachineStep(1) and !digitalRead(3)) {
turnMachineStep(0);
}
}
// command 0 - reset this machine.
// command 1 - normal step this machine, return 0 when done/no longer busy
// command 2 - initialise this machine - mystery? kludged here
unsigned char turnMachineStep(unsigned char command)
{
static byte frame = 0;
static unsigned long lastTime = 0;
static unsigned long tween = 1;
static unsigned char busy = 0; // not busy!
static unsigned char returnValue = 0; // not done - will change if not
// kludge ahead. I give up, pound a nail in it.
if (command == 2) {
tween = 1;
frame = 0;
returnValue = 1; // we done
busy = 0; // we not
// initialised = true;
return returnValue;
}
if (command == 0) {
frame = 0;
lastTime = millis();
tween = 200; // next call gets immediate enough attention.
busy = 1;
return (1);
}
if (!busy)
return (0);
if (millis() - lastTime < tween)
return (1);
lastTime = millis();
switch (frame) {
case 0 :
Serial.println(" render frame 0");
tween = 200;
frame++;
break;
case 1 ... 8 : /* case 1 - 21 elided here */
Serial.print(" render frame ");
Serial.println(frame);
tween = 200;
frame++;
break;
case 9 :
Serial.println(" render frame 9. Done?");
tween = 15;
frame = 0;
returnValue = 1; // we done
busy = 0; // we not
break;
}
return returnValue;
}