Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Pages: [1]   Go Down
Author Topic: Servo "Loop"?  (Read 510 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 318
Posts: 26927
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Do you have a pull-down resistor on the input pin?
Logged

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 318
Posts: 26927
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can save yourself the cost of a pull down by using the built-in pullups, and simply inverting your button presslogic.
Logged

"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.

Pages: [1]   Go Up
Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Jump to: