Controlling a servo position with a button code

Hi! I am working on a project that simply controls a servo with the push of a button. I am having some difficulty perfecting the code. My major issue currently is when the button is pressed it does not always activate the movement, sometimes takes two or three presses. The other issue is the servo does not always make a full complete movement. My main objective is to have the servo rotate from 0 to 180 with the press of a button and then with the second press to rotate it back from 180 to 0. I’ve read a few other posts and tweaked my code accordingly but still seem to be having trouble. I am using an Arduino Uno and v.2 Motor Shield with the servo.

I am very new to Arduino and coding and still learning. However, any help at all will be greatly appreciated! Thanks so much!

My code is as follows:

#include<Servo.h>
const int button = 8; //button attached to digital pin 8
int press = 0;
Servo servo;
boolean toggle = true;

void setup()
{
pinMode(button, INPUT);
Servo.attach(10); // pin for servo control via motor shield
digitalWrite(button, HIGH);
}

void loop()
{
  press = digitalRead(button);
  if(press==LOW) 
  {
   if(toggle)
      {
          servo.write(190);
          toggle = !toggle; 
       }
  else 
  {
   servo.write(10);
   toggle = !toggle;
  }
}
delay(500);
}
Servo = servo;

Not sure you wanted the "=" there.

You need your code to detect when the button becomes pressed not simply when it is pressed. You may think you are only pressing your button for a moment, but in that time, the Arduino may read it as pressed hundreds of times over.

You also need to debounce your button. You may only think you pressed the button once, but actually the contacts bounce open and closed several times before they settle.

Here's a question for you. In this code

if(press==LOW) {
   if(toggle) {
          servo.write(190);
          toggle = !toggle; }
else {
   servo.write(10);
   toggle = !toggle; }}

which "if" did you intend the "else" to belong? Your indentation indicates the first "if" but your {} braces indicate it belongs to the second "if", and it's the braces that are important (this is C, not Python). Use Tools-->Auto Format to fix the indentation.

PaulRB:
Here's a question for you. In this code

if(press==LOW) {

if(toggle) {
          servo.write(190);
          toggle = !toggle; }
else {
  servo.write(10);
  toggle = !toggle; }}



which "if" did you intend the "else" to belong? Your indentation indicates the first "if" but your {} braces indicate it belongs to the second "if", and it's the braces that are important (this is C, not Python). Use Tools-->Auto Format to fix the indentation.

It should belong to the first else. I can fix the brackets to show that

francism4:
It should belong to the first else.

There is only one "else". My question was which "if"? Did you mean to say the first "if"?
Are you sure? Because you said:

the servo rotate from 0 to 180 with the press of a button and then with the second press to rotate it back from 180 to 0.

So should the servo return to 0 when you press the button a second time, or the moment you release the button?

Also I'd be surprised if that servo.write(190) did anything at all. The range for write() is 0 to 180 and anything outside that is treated as writeMicroseconds() provided it is over 544, which of course 190 isn't.

Steve