/// Script.ino : movement control state machine for StepperShow /// This example shows a sample program structure for programming a pattern of /// autonomous motions which runs entirely on the Arduino. The chief /// complication is that the system needs to poll the motion generators /// continuously, so the scripting functions cannot use any form of delay or /// sleep. /// The script_poll() function below is called from within the main loop() /// function to update the status of the script and immediately return. This /// example is entirely time based, so it keeps track of elapsed time until a /// script event takes place, then issues movement commands and updates the /// internal state for the next event. This scripting system works in parallel /// with the regular serial input and output so external control is still /// possible. //================================================================ // Interval in microseconds between script updates. static unsigned long script_poll_interval = 10000000; // 1 Hz update rate // Script state machine index. static int script_index = 0; // Time counter within the script engine. static long script_seconds = 0; //================================================================ // Transition function to change state. void transition(int new_state) { script_seconds = 0; script_index = new_state; Serial.print("dbg Script entering state "); Serial.println(new_state); } //================================================================ /// This is called once to initialize script state. void script_setup(void) { } /****************************************************************/ /// This should be called from loop() as often as feasible to update the /// movement state machine. Interval is the number of microseconds since the /// last update. void script_poll(unsigned long interval) { static long timer = 0; // wake up at regular intervals (default is once per second) timer -= interval; if (timer < 0) { timer += script_poll_interval; script_seconds += 1; switch(script_index) { case 0: // wakeup transition(1); break; case 1: // initial pause if (script_seconds >= 2) { set_driver_enable(1); x_path.incrementTarget(800); transition(10); } break; case 10: // first movement if (script_seconds >= 10) { y_path.incrementTarget(800); transition(20); } break; case 20: if (script_seconds >= 10) { z_path.incrementTarget(800); transition(30); } break; case 30: if (script_seconds >= 10) { a_path.incrementTarget(800); transition(40); } break; case 40: if (script_seconds >= 10) { x_path.incrementTarget(800); transition(10); } break; } } }