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

Pololu Forum

Programming help for two buttons

I could use some help with coding buttons. I would like the green button to run the sequence once and the red button to run the sequence 5 times. I’m not a programmer. I’m looking for any examples I may use.
Thank you for any help.

# Robotic Arms Loop

begin
		button_green if sequence_a endif
		button_red if sequence_b endif

repeat


sub button_green
		4 get_position 500 less_than
		return
sub button_red
		6 get_position 500 less_than 
		return		


sub sequence_a
sub sequence_b


  500 0 5600 0 7200 3600 4640 
  0 5600 0 7200 0 4992 
  0 4144 6972 6800 frame_0..3_5_7..17 # Frame 0
  500 9400 9158 frame_1_9 # Frame 1
  500 4213 frame_5 # Frame 2
  500 3591 3882 4203 3239 frame_5_7_13_15 # Frame 3
  500 3570 4232 3911 4086 frame_5_7_13_15 # Frame 4
  500 9304 9443 frame_16_17 # Frame 6
  500 3968 3852 frame_7_15 # Frame 7
  500 2970 3853 2700 frame_5_7_13 # Frame 8
  500 5068 3076 3968 5165 3152 9600 frame_3_5_7_11_13_16 # Frame 9
  500 3999 frame_13 # Frame 15
  500 6400 2188 7707 2276 9600 frame_5_7_13_15_17 # Frame 10
  1500 2569 2569 7600 2363 6800 6800 frame_1_9_13_15..17 # Frame 11
  500 2246 frame_7 # Frame 11(B)
  500 5959 2244 6133 frame_3_7_11 # Frame 12
  500 2188 frame_7 # Frame 13
  500 3656 3837 frame_1_9 # Frame 14
  500 5600 5600 frame_1_11 # Frame 16
  500 4640 4144 frame_7_15 # Frame 17
  500 3600 4992 frame_5_13 # Frame 18
       return

sub frame_0..3_5_7..17
  17 servo
  16 servo
  15 servo
  14 servo
  13 servo
  12 servo
  11 servo
  10 servo
  9 servo
  8 servo
  7 servo
  5 servo
  3 servo
  2 servo
  1 servo
  0 servo
  delay
  return

sub frame_1_9
  9 servo
  1 servo
  delay
  return

sub frame_5
  5 servo
  delay
  return

sub frame_5_7_13_15
  15 servo
  13 servo
  7 servo
  5 servo
  delay
  return

sub frame_16_17
  17 servo
  16 servo
  delay
  return

sub frame_7_15
  15 servo
  7 servo
  delay
  return

sub frame_5_7_13
  13 servo
  7 servo
  5 servo
  delay
  return

sub frame_3_5_7_11_13_16
  16 servo
  13 servo
  11 servo
  7 servo
  5 servo
  3 servo
  delay
  return

sub frame_13
  13 servo
  delay
  return

sub frame_5_7_13_15_17
  17 servo
  15 servo
  13 servo
  7 servo
  5 servo
  delay
  return

sub frame_1_9_13_15..17
  17 servo
  16 servo
  15 servo
  13 servo
  9 servo
  1 servo
  delay
  return

sub frame_7
  7 servo
  delay
  return

sub frame_3_7_11
  11 servo
  7 servo
  3 servo
  delay
  return

sub frame_1_11
  11 servo
  1 servo
  delay
  return

sub frame_5_13
  13 servo
  5 servo
  delay
  return

Hello.

Thank you for posting what you have tried so far. There are probably a number of ways you could accomplish something like you described, but probably the easiest is by having a while loop in your sequence and putting the number of times you want it to loop on the stack before calling the sequence.

For example, your main BEGIN/REPEAT loop can look something like this:

begin
  button_green if 1 sequence_a endif    #run sequence_a 1 time
  button_red if 5 sequence_a endif         #repeat sequence_a 5 times
repeat

Then, you could make your sequence_a look something like this:

sub sequence_a
  begin dup while
    
    #code for sequence goes here

    1 minus
  repeat
  drop
return

Note that if you do it this way, you should remove your declaration of sequence_b.

Brandon

That worked wonderfully. I did notice that all the movement was slower after the code change though. Is that normal.

Thanks again for the help.

I would not expect that change to slow anything down. Are you saying that the sequence is taking longer between movements now or that the servos are now moving slower than they were before during those movements?

Brandon