servo angle

Hello! i want to control a servo angle with a push of button. when i press it, the angle will be a value, and if it is released, it will write another value. i have written a sketch,but it does not work. can anybody lend a hand?

#include <Servo.h>
#include <Servo.h>
Servo myServo1;

const int button = 2;
void setup(){
  Serial.begin(9600);
  myServo1.attach(7);
  pinMode(button, INPUT);
}

void loop (){
  if(digitalRead(button) == HIGH) {
    myServo1.write(70);
  }
  if(digitalRead(button) == LOW) {
    myServo1.write(120);
  }
}

Thanks!!

You include <Servo.h> twice. Remove one.

Do you have a pull-up resistor for the button ?
You could use the internal pull-up with: pinMode(button, INPUT_PULLUP);

hello, thanks for the reply. however i dont really understand, i should use a pull-up resistor? how will the sketch be like? thanks!

On this page, http://www.pighixxx.com/abc-arduino-basic-connections/
Open the pdf file: "Set 1 (Card 1,2,3)".
The button with pull-up is on the left. The orange label to "IN" is to the digital input (pin 2 in your sketch).

Hi thanks again for the reply. my wiring is similar to that.btw what is wrong with this sketch? i want it to function like what i have previously mentioned above

#include <Servo.h>
Servo myServo1;
int buttonState = 0;   
const int  buttonPin = 2; 
void setup(){
  Serial.begin(9600);
  myServo1.attach(7);
 pinMode(buttonPin, INPUT);
}

void loop (){
   buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
    myServo1.write(70);
  }
 if (buttonState == LOW) {
    myServo1.write(120);
  }
}

Thanks!

what is wrong with this sketch?

How should we know?
You're the one with the hardware.

Why haven't you enabled the pullup? Have you got an external one?

Ya I have an external one and it's connected to pin 2 on the Arduino.

There were three questions.

Oh sorry , isn't using pinmode enabling pull up? I referred to te link above

isn't using pinmode enabling pull up?

This one? pinMode(buttonPin, INPUT);
No.

You still haven't described your perceived problem.

If you have an exteranl pull-up, the sketch is okay.
So perhaps there is something with the button or the connection witht the servo.
For example if you use a breadboard, the contacts could be bad.
Can you check both seperately ?
Can you make the servo turn with a sketch ?
Can you write the state of the button to the serial port ?

.....
buttonState = digitalRead(buttonPin);
Serial.print("buttonState = ");
Serial.println( buttonState );
.....

hi, i just checked what u asked, and i realsied that the button is not working, servo is. when i press the button, the serial monitor still read s 0. when i change the switch the situation is still the same. to be sure, the switch is connected to pin 2 and Gnd right? thanks

Yes, the button to pin2 and ground.

Where is the pull-up resistor ?
Can you make a photo of it in the circuit so we can actually see the pull-up resistor ?
Or use the INPUT_PULLUP.

hello, this is how i connected it .. please correct me if im wrong.. thanks

(uncompiled, untested)

const byte switchPin = 2;
void setup ()
{
  Serial.begin (9600);
  pinMode (switchPin, INPUT);
  digitalWrite (switchPin, HIGH);

}

void loop ()
{
  Serial.println (digitalRead (switchPin));
}

What does that do?

You don't have a pull-up resistor !

You should add a pull-up resistor of 10k from digital pin to 5V.
You could also use the internal pull-up resistor: pinMode (switchPin, INPUT_PULLUP);
A pull-up resistor pulls the digital input to 5V if the button is not pressed. If the button is pressed, the digital input is pulled low.

A drawing : http://arduino.cc/en/tutorial/button
A photo : http://www.arduino.cc/en/Tutorial/Pushbutton

Oh, but i remembered the last time, i used my method and it worked...but thanks for the help! you are awesome!!