Combine Servo and Button

Hi.

Is there anyone who can help us combine the function of button and a servo?
We want to make the servo go forth and back when you pusch the button. We tried to combine the original examples that comes with the program but we didn't get it to work. There is obvioulsy some part we missed, and we can't figure it out. Any help is appreciated.

Thanks!

We tried to combine the original examples that comes with the program but we didn't get it to work. There is obvioulsy some part we missed, and we can't figure it out.

Without seeing your code, neither can we.

If so, we'll send the code tomorrow!

This is our failed project to combine the button and servo program :roll_eyes:

#include <Servo.h>

Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0; // variable to store the servo position

void setup()
{
myservo.attach(13); // attaches the servo on pin 9 to the servo object
}
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
// initialize the servo pin as an output:
pinMode(Servo myservo, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn servo on:
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);
}
else {
// turn LED off:
return loop
}
}

we attached the program itself below

sweep_and_button_FAIL.pde (2.04 KB)

It didn't compile, so it can't really be said to "fail".

for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 179 degrees
    for(int pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 1 degree

Code that doesn't even compile rarely warrants posting, unless the problem is that you can't figure out why it won't compile.

Your setup function:

void setup()
{
  myservo.attach(13);  // attaches the servo on pin 9 to the servo object
}

Some other random stuff:

  // initialize the servo pin as an output:
  pinMode(Servo myservo, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);    
}

Some of this stuff belongs in setup(). Some of it makes no sense. The first argument to the pinMode command is a pin number, not a Servo object that is created on the fly. The Servo library manages the mode of the pin that the servo is attached to. You don't need to.

  else {
    // turn LED off:
    return loop
  }

All statements need to end with a ;. What exactly do you think you are doing here? The loop function does not return a value. If the return statement is used, it should not be followed by a value. Certainly no the name of a function.

Thanks for replies!
We have acually no idea what we're doing, lol. Since this is our first time using arduino in a project like this, the area is very alien for us. The point was to make the program by ourselves, but apparently that didn't go well.
The program was supposed to act like nothing would happen if the button wasn't pressed and when it was, the servo should sweep forth one time and then go back again and then the program should reset.
If you could help us in a easy way without giving us too much (as we still want to achieve this by our own, in a way), just pointing us in the right direction, we would be grateful!!

An if/else statement implies that one thing should happen if the condition is true, and something else should happen if the condition is false.

What you want is for something to happen if the condition is true, and for nothing to happen if the condition is not true. So, just get rid of the whole else block.

#include <Servo.h>
 
Servo myservo;  
 
int pos = 0;    // variable to store the servo position
const int buttonPin = 2;     // the number of the pushbutton pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup()
{
  myservo.attach(13);  // attaches the servo on pin 13 to the servo object

  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);    
}

void loop()
{
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH)
  {    
    // turn servo on:    
    for(pos = 0; pos < 180; pos++)  // 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--)     // goes from 180 degrees to 0 degrees
    {                                
      myservo.write(pos);              // tell servo to go to position in variable 'pos'
      delay(15);
    }
  }
}

Thanks for the reply PaulS. This helped us a a lot in our development. The only thing we need to figure out now is the wiring, but it should not be any problem!

Thank you for all your help, especially PaulS. It has been very useful and we now understand the ways of basic programming, which were the goal of this assignment
]:smiley: