Go Down

Topic: Servo "Loop"? (Read 527 times) previous topic - next topic

kcstokes

Hi I'm new to the arduino, and completely new to programming, but I do have some experience with circuits and wiring. I'm trying to create a simple button controlled servo that would go to a set position of 90 degrees, wait 2 seconds, then go back to position 0. I've tried a few methods of programming this. One result I get is that the servo will go to its position only if the button is held, then go back down when the button is released. The other result is that the servo will go to the set position on a single press, but never go back. I've been using the If command, but I don't know if that's the right one to use.
Here's the code that I originally thought would do it. As I said, I want a momentary button press to set a servo to a preset position, wait two seconds, then go back to it's original position.

Code: [Select]

#include <Servo.h>
int button1 = 2;
int press1 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  servo1.attach(9);
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == HIGH)
  {
  servo1.write(90);
  delay(2000)
  servo1.write(0);
  }
}


Any help would be greatly appreciated.
Casey

AWOL

Do you have a pull-down resistor on the input pin?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

kcstokes

Ahh thank you so much! I have it working now. I was following a pushbutton tutorial that used a pull up resistor, so I mistakenly used that on this setup. It's all working exactly how I wanted it to now. I plan to attach shooting targets to servos down a range, and be able to remotely bring individual targets into view. Trying to learn as much as I can as I go.

AWOL

You can save yourself the cost of a pull down by using the built-in pullups, and simply inverting your button presslogic.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up