Servo control sketch issues

I’m currently working on a program for an Arduino-controlled rubber band gun. For the firing mechanism, I have an X-shaped horn on the servo, each one holding a rubber band (I know, I only have 180º to use).

Here’s what’s supposed to happen:
load (move 180º back)
wait (in this time, I can manually load the bands onto the servo horn)
button is pressed
fire (servo moves forward in 90º increments about 500ms apart to fire off the bands)
LOOP back to top

Here’s what is happening:
wait
button is pressed
load
fire (immediately after the 180º load sequence)
LOOP back to top

This is my code:
I think most of it is correct, but I’m obviously doing something wrong. Forgive me as I am not very experience coding in the Arduino IDE :-/

#include <Servo.h>

Servo shootservo;

int pos = 0;
int buttonPin = 13;
int buttonState = 1;

void setup()
{
  shootservo.attach(9);
  pinMode(buttonPin, INPUT);
}

void loop()
{
  for(pos = 0; pos < 180; pos +=180)
  {
    shootservo.write(pos);
    delay(1000);
  }
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) 
  {
  for(pos = 180; pos>=1; pos -=90)
    {
      shootservo.write(pos);
      delay(1000);
    }
  }
}

You are not enabling the internal pull-up resistor on the switch pin. This implies that you have external pull-up or pull-down resistors wired. Do you?

If I understand you correctly, then yes, I have the switch hooked up with a resistor from the switch to ground. That doesn’t seem to be the problem, though. It read the state of the switch just fine, it’s just that the steps are out of order, and changing them in the code seems to have no effect on the order of them in “real life.”
Thanks for your help, though!

  for(pos = 0; pos < 180; pos +=180)
  {
    shootservo.write(pos);
    delay(1000);
  }

How many times is this loop going to execute? What value is going to be written to the servo?

There is no purpose served by the for loop. Remove it, but not the code in the loop body.

Then, examine the code and see if you can see why it does not work (I can).

Oh ok, I see where you’re going with this. If I’m just telling it to go to a position, I don’t need to tell it each step to take, right? I’ve removed the ‘for’ loop and replaced the ‘pos’ in the write command with 180.
I also did some other tweaks to the firing chunk of the code.

Now it works mostly properly, thanks!
The only thing is that when I tell it to fire, it just goes straight to the 0º spot, not stopping at 90º on the way there. This isn’t a critical component, but I’d like to get it to work…

#include <Servo.h>

Servo shootservo;

int pos = 0;
int buttonPin = 13;
int buttonState = 1;

void setup()
{
  shootservo.attach(9);
  pinMode(buttonPin, INPUT);
}

void loop()
{
  {
    shootservo.write(180);
  }
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) 
  {
  pos = 180; pos = 0; pos -=90;
  {
    shootservo.write(pos);
    delay(1000);
  }
  }
}

never mind, I just looked this all up in the reference section and I feel pretty dumb. I guess I can just write 90º and the 0º to the servo with a delay in between when I press the button. Thanks for your help!

  pos = 180; pos = 0; pos -=90;
  {
    shootservo.write(pos);
    delay(1000);
  }

For this part, you DO need a for loop. Otherwise, you are just changing the value of pos three time for the hell of it.

As a matter of fact, that’s exactly what I’m trying to do :smiley:
I just want it to move 90º as fast as possible, wait, then do it again.
That code is ‘old,’ here’s my current code, does just what I need it to do:

#include <Servo.h>

Servo shootservo;

int buttonPin = 13;
int buttonState = 1;

void setup()
{
  shootservo.attach(9);
  pinMode(buttonPin, INPUT);
}

void loop()
{
  {
    shootservo.write(180);
  }
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) 
  {
    shootservo.write(90);
    delay(500);
    shootservo.write(0);
    delay(500);
  }
}

The next step for this turret is a left and right tact switch to control the way it points with a second servo in the base. Before I get myself into something I don’t know about again, can I use a ‘while’ loop for the switch? I want to make it so that for as long as I press the left button, the turret will slowly turn left (same with right).

can I use a ‘while’ loop for the switch? I want to make it so that for as long as I press the left button, the turret will slowly turn left (same with right)

As long as the value of the switch state is read on every iteration of the loop, then, yes you can.

While you are adding buttons, you should give some serious thought to adding an “arm” button that will move the servo launch servo into the load position, rather than moving it on every pass through loop.