Servo Motor Sweep Once when Button Pressed

Hi,
I want a servo motor sweep once from 0 to 180 degrees when a button is pressed. I have looked over the internet and tried all the sketches I could find, including the sweep sketch included in the arduino IDE. However they all have a continuous sweep whereas I would like when a button is pressed for the servo to execute only one sweep. Could someone give me a general idea of the code, it would help a lot.

Any help would be great thanks.

First confirm this isn't the servo you mentioned in your other thread, which you have modified to be a continuous servo, and which now has no positional control....

Basic servo button test code.

//zoomkat servo button test 7-30-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(160);
  }
  else {
    servo1.write(20);
  }
}

Thanks for the code.
P.S. JimboZA, I have purchased a new servo which has positional control.

Thanks, Ken

Kenn13542:
I have purchased a new servo which has positional control.

Excellent...

I have tried the new code and when i press the button it only moves a couple of degrees and goes back to the same position. I have checked that the pins are connected to the correct pins, the button is correctly wired and I'm using the new servo. Any ideas on what the problem might be.

When you say "the new code", do you mean zoomkat's from this thread? If so did you change it all, and if you did perhaps post it here.

Re connections, have you got the servo on its own power?- not recommended to get its power from the Arduino's 5V... Better to give servo its own power from battery or whatever 5v and ground, and link that servo/power ground to an Arduino ground.

Hi,
Heres the Code:

#include <Servo.h>
int button1 = 2; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;

void setup()
{
pinMode(button1, INPUT);
servo1.attach(9);
digitalWrite(2, HIGH); //enable pullups to make pin high
}

void loop()
{
press1 = digitalRead(button1);
if (press1 == LOW)
{
servo1.write(180);
}
else {
servo1.write(0);
}
}

And it isn't on it's own power source, it didn't even come across my mind as I'm prototyping on a breadboard and have forgot to check that. But however I have allowed for it's own power source on my final circuit diagram when it is transferred onto a PCB. I will check if by adding its own power source, that if that fixes it.

Thanks, Ken

Is that zoomkat's code apart from I see you changed 160 to 180. You should give us a sketch of how you've hooked everything up... Arduino, button, power, all 3 servo wires.

You ought to put the servo on its own supply right now.... all sorts of servo "funnies" go away by doing that.

1 Like

Kenn13542:
I have tried the new code and when i press the button it only moves a couple of degrees and goes back to the same position. I have checked that the pins are connected to the correct pins, the button is correctly wired and I'm using the new servo. Any ideas on what the problem might be.

Perhaps you released the button? As soon as the button is released, the servo returns to the 20 deg position. In the below untested code when the button is pushed and released, the servo will go to the 180 position for 4 sec, then return to the 0 position. Put your servo sweep code where the servowrite and delay code is.

//zoomkat servo button test 7-30-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(180); //PUT YOUR SWEEP CODE HERE
    delay(4000); 
    servo1.write(0);
  }
}

You might try the below (untested). I stuck in the sweep code.

//zoomkat servo button test 7-30-2011

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo myservo;
int pos = 0;    // variable to store the servo position 

void setup()
{
  pinMode(button1, INPUT);
  myservo.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
    {                                  // in steps of 1 degree 
      myservo.write(pos);              // tell servo to go to position in variable 'pos' 
      delay(15);                       // waits 15ms for the servo to reach the position 
    } 
    for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
    {                                
      myservo.write(pos);              // tell servo to go to position in variable 'pos' 
      delay(15);                       // waits 15ms for the servo to reach the position 
    } 
  }
}

I tried both of the sketches before on this forum. However they didn't work for me, so i tried many others until I came across this sketch. It is given under Examples/Digital/DigitallputPullup. I modified it so it will when i press the button it turns pin 13 HIGH, so i can tell if the code is working and then turns pin 13 LOW when the servo has done a 0 to 180 to 0 degree turn, giving me a sweep once when the button is pressed.

Thanks to everyone who helped be along the way, it is greatly appreciated!

/*
 Input Pullup Serial
 
 This example demonstrates the use of pinMode(INPUT_PULLUP). It reads a 
 digital input on pin 2 and prints the results to the serial monitor.
 
 The circuit: 
 * Momentary switch attached from pin 2 to ground 
 * Built-in LED on pin 13
 
 Unlike pinMode(INPUT), there is no pull-down resistor necessary. An internal 
 20K-ohm resistor is pulled to 5V. This configuration causes the input to 
 read HIGH when the switch is open, and LOW when it is closed. 
 
 created 14 March 2012
 by Scott Fitzgerald
 
 http://www.arduino.cc/en/Tutorial/InputPullupSerial
 
 This example code is in the public domain
 
 */

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo
int pos = 0;    // variable to store the servo position 

void setup(){
  //start serial connection
  Serial.begin(9600);
  //configure pin2 as an input and enable the internal pull-up resistor
  pinMode(2, INPUT_PULLUP);
  pinMode(13, OUTPUT); 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object 

}

void loop(){
  //read the pushbutton value into a variable
  int sensorVal = digitalRead(2);
  //print out the value of the pushbutton
  Serial.println(sensorVal);
  
  // Keep in mind the pullup means the pushbutton's
  // logic is inverted. It goes HIGH when it's open,
  // and LOW when it's pressed. Turn on pin 13 when the 
  // button's pressed, and off when it's not:
  if (sensorVal == LOW) {
    digitalWrite(13, HIGH);
    myservo.write(180);           
    delay(300);                                
    myservo.write(8);               
    delay(300);                        
    myservo.write(185);              
    delay(300);                      
  } 
  else {
    digitalWrite(13, LOW);
                
  }
}

PullUp.ino (1.73 KB)