Pololu Robotics & Electronics
Menu
My account Comments or questions? About Pololu Contact Ordering information Distributors

Pololu Forum

Arduino trigger Script sequence via button

Hi,

I’ve managed to connect my micro maestro to my Arduino and run all the example codes.

But the one simple thing I want to do does not seem to work and it might be because I am still very new to the scripting for Maestro.

I want to press a button attached to my Arduino that will start a simple Sequence that I have saved and copied to the script of the Meastro Control Center. I can’t figure out why it is not working though. When I press the button I can see that it has started the script in control panel and that it is running through thte stack values that are shown on the left. However, the motors only move to one position and then stop… Please help! I am sure it is a simple issue…but I can’t spot it. I assume that there is something that I might need to add to the maestro script (which I have simply created through the sequencer and copied to the script), but again, I am not finding any info that I really understand to be honest.

Here is the Arduino code:

[code]#include <PololuMaestro.h>

#ifdef SERIAL_PORT_HARDWARE_OPEN
#define maestroSerial SERIAL_PORT_HARDWARE_OPEN
#else
#include <SoftwareSerial.h>
SoftwareSerial maestroSerial(10, 11);
#endif

MicroMaestro maestro(maestroSerial);

const int buttonPin1 = A0;
int buttonState1 = 0;

void setup()
{
maestroSerial.begin(9600);
Serial.begin(9600);
pinMode(buttonPin1, INPUT);

}

void loop()
{

buttonState1 = digitalRead(buttonPin1);

if (buttonState1 == HIGH) {
maestro.restartScript(0);
delay(4000);
}
else {

maestro.stopScript();
}

}

[/code]

And the Script:

[code]# Sequence 3
begin
500 5888 8000 0 0 0 0 frame_0…5 # Frame 0
500 6139 4222 frame_0_2 # Frame 1
500 6238 frame_1 # Frame 2
500 8000 frame_2 # Frame 3
repeat

sub frame_0…5
5 servo
4 servo
3 servo
2 servo
1 servo
0 servo
delay
return

sub frame_0_2
2 servo
0 servo
delay
return

sub frame_1
1 servo
delay
return

sub frame_2
2 servo
delay
return

[/code]

Best
Nelmarie

Hello, Nelmarie.

I briefly looked at your Arduino code and Maestro script, and it looks like you are restarting the script at sub frame_0..5 (subroutine 0). When your script tries to return from that subroutine, it cannot because it has no where to return to, which is probably what you are seeing (“motors only move to one position and then stop”). If you want to start your script from the BEGIN/REPEAT block, you would need to make a subroutine for it. Like so:

SUB main
  BEGIN
    500 5888 8000 0 0 0 0 frame_0..5 # Frame 0
    500 6139 4222 frame_0_2 # Frame 1
    500 6238 frame_1 # Frame 2
    500 8000 frame_2 # Frame 3
  REPEAT

- Amanda

thanks Amanda, this is actually something that I tried and forgot to mention.

I tested it now again as you said, but it gives a ‘Stack overflow/underflow’ error

All the example sketches work so I am sure I have wired everything correctly.

Any other options??

okay no! I have got it working now. I restarted everything and seems to be fine now, except for one thing that I had to change in the Arduino code.

When I would press the button it would run the code, but once the button is released it stopped it, which now thinking about it kind of makes sense.

Now I just need to make it run through the sequence once. OR I will add a second button to stop the script.

Thanks for your help Amanda! I might be back here with another question soon.

Great! I am glad you figured out the issue and fixed it; thanks for letting us know.

By the way, I see from the other post you made here that you decided to use a second button to stop your script. However, if you only want your Maestro script to run through your sequence once, you could alternatively replace REPEAT with QUIT, which would stop the script. For more information about the QUIT command, see the “Command Reference” section in the Maestro’s user’s guide.

- Amanda