Arduino Trigger Sequences

Hello!

I am trying to write a fairly simple program with Arduino and Pololu Maestro servo controller. I want ‘sequence 2’ to loop and ‘sequence 0’ and ‘sequence 1’ to play when their associated buttons are pressed. I can get the button presses to function correctly but when I try to introduce an if statement to play sequence 2 when nothing is pressed I get some unexpected results. Could someone please look this over and let me know what I am doing wrong. All help is greatly appreciated!
Thank you!

#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);
//MiniMaestro maestro(maestroSerial);
const int buttonPin = 2;
const int buttonPin2 = 3;
int buttonState = HIGH;
int buttonState2 = HIGH;
int MotorControl = 5;  

void setup()
{
  maestroSerial.begin(9600);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
   pinMode(MotorControl, OUTPUT);
}

void loop()
{
  
  buttonState = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);
  
  if(buttonState == HIGH && buttonState2 == HIGH){
    //digitalWrite(MotorControl,LOW);
  maestro.restartScript(02);
  //delay(4000);
  }
    if (buttonState == LOW) {
      //maestro.stopScript();
   maestro.restartScriptWithParameter(0, 0);
  digitalWrite(MotorControl,HIGH);
  //delay(4000);
 } 
else {
  digitalWrite(MotorControl,LOW);
}
 
if (buttonState2 == LOW) {
  maestro.restartScriptWithParameter(01, 01);
  //digitalWrite(MotorControl,HIGH);
  //delay(4000);
}

}
0000:                      -- 
0000:                      -- 
0000:                      -- ### Sequence subroutines: ###
0000:                      -- 
0000:                      -- # Sequence 0
0000:                      -- sub Sequence_0
0000: 031AF401B4135E18800F800F800F7516401FAB190000000000000000 --   500 5044 6238 3968 3968 3968 5749 
001C: 83                   --   8000 6571 0 0 0 0 frame_0..11 # Frame 0
001D: 0304F401800F84       --   500 3968 frame_6 # Frame 1
0024: 00                   --   quit
0025:                      -- # Sequence 1
0025:                      -- sub Sequence_1
0025: 031AF401B4135E18800F800F800F7516800F800F0000000000000000 --   500 5044 6238 3968 3968 3968 5749 
0041: 83                   --   3968 3968 0 0 0 0 frame_0..11 # Frame 0
0042: 0304F401DB1384       --   500 5083 frame_6 # Frame 1
0049: 0304F401800F84       --   500 3968 frame_6 # Frame 2
0050: 0304F401C61484       --   500 5318 frame_6 # Frame 3
0057: 0304F401800F84       --   500 3968 frame_6 # Frame 4
005E: 00                   --   quit
005F:                      -- # Sequence 2
005F:                      -- sub Sequence_2
005F:                      -- begin
005F: 031AF401B4135E18441C8E126414BA0F800F800F0000000000000000 --   500 5044 6238 7236 4750 5220 4026 
007B: 83                   --   3968 3968 0 0 0 0 frame_0..11 # Frame 0
007C: 0304F401401F85       --   500 8000 frame_2 # Frame 1
0083: 0304F401800F85       --   500 3968 frame_2 # Frame 2
008A: 0304F401841985       --   500 6532 frame_2 # Frame 3
0091: 0304F401F31D85       --   500 7667 frame_2 # Frame 4
0098: 0304F401800F85       --   500 3968 frame_2 # Frame 5
009F: 0304F401401F85       --   500 8000 frame_2 # Frame 6
00A6: 065F00               --   repeat
00A9:                      -- 
00A9:                      -- sub frame_0..11
00A9: 020B2A               --   11 servo
00AC: 020A2A               --   10 servo
00AF: 02092A               --   9 servo
00B2: 02082A               --   8 servo
00B5: 02072A               --   7 servo
00B8: 02062A               --   6 servo
00BB: 02052A               --   5 servo
00BE: 02042A               --   4 servo
00C1: 02032A               --   3 servo
00C4: 02022A               --   2 servo
00C7: 02012A               --   1 servo
00CA: 02002A               --   0 servo
00CD: 08                   --   delay
00CE: 05                   --   return
00CF:                      -- 
00CF:                      -- sub frame_6
00CF: 02062A               --   6 servo
00D2: 08                   --   delay
00D3: 05                   --   return
00D4:                      -- 
00D4:                      -- sub frame_2
00D4: 02022A               --   2 servo
00D7: 08                   --   delay
00D8: 05                   --   return
00D9:                      -- 

Subroutines:
Hex Decimal Address Name
00  000     0000    SEQUENCE_0
01  001     0025    SEQUENCE_1
02  002     005F    SEQUENCE_2
03  003     00A9    FRAME_0..11
04  004     00CF    FRAME_6
05  005     00D4    FRAME_2

Hello.

The way your Arduino code is written, it will constantly be restarting the script at the second subroutine when no button is pressed (i.e. even if the Maestro is currently running subroutine 2, sending a restart script at subroutine command will interrupt it and start it over again).

It looks like you already have your subroutine set up to loop by itself once it is called, so you can just set up your Arduino sketch to only send the restartScript(2) command once. There are a few ways to do this. For example, you could use a state variable to keep track of whether or not it has been started. However, the simplest approach would probably be to remove your first if statement (that checks whether or not both button inputs are high), and just have the other if statements start subroutine 2 when they are done. For example:

void loop()
{  
  buttonState = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);

  if (buttonState == LOW) {
    maestro.restartScriptWithParameter(0, 0); 
    digitalWrite(MotorControl,HIGH);
    delay(4000); //Wait long enough for subroutine 0 to complete
    maestro.restartScript(02); //Then start subroutine 2
    } 
  else {
    digitalWrite(MotorControl,LOW);
  }
 
  if (buttonState2 == LOW) {
    maestro.restartScriptWithParameter(01, 01);
    delay(4000);  //Wait long enough for subroutine 0 to complete
    maestro.restartScript(02);  //Then start subroutine 2
    }
}

Brandon

Thanks again Brandon for the help! Its working!

1 Like