2 Button/2 Servo Control

Hi, I feel that this is a pretty easy fix but for some reason its been difficult. I’m trying to get a button to activate servo1 and another button to activate servo2. However every single time I push one or the other it either doesn’t quite move smoothly or it stops along the way and needs to reset. Here is my working code so far.

#include <Servo.h> // include the servo library
Servo servo1, servo2; // creates an instance of the servo object to control a servo
int analogPin1 = A0; // the analog pin that the sensor is on
int analogPin2 = A2;
int analogValue1, analogValue2 = 0; // the value returned from the analog sensor

void setup() {
servo1.attach(2); // attaches the servo on pin 2 to the servo object
servo2.attach(4);
pinMode (2, OUTPUT);
pinMode (A0, INPUT);
pinMode (4, OUTPUT);
pinMode (A2, INPUT);
}
void loop()
{

analogValue1 = analogRead(analogPin1); // read the analog input
analogValue1 = map(analogValue1, 0, 100, 170, 0); // map the analog value to the angle of the servo
analogValue2 = analogRead(analogPin2);
analogValue2 = map(analogValue2, 0, 100, 170, 0);

if(analogPin1 == HIGH){
//If button is turned on:
digitalWrite(analogValue1, 0);
} else{
//Neutral Servo
digitalWrite(analogValue1, 100);}

if(analogPin2 == HIGH){
//If button is turned on:
digitalWrite(analogValue2, 0);
} else{
//Neutral Servo
digitalWrite(analogValue2, 100);}

servo1.write(analogValue1); // write the new mapped analog value to set the position of the servo
servo2.write(analogValue2);
Serial.println(analogValue2); //print value to serial port
delay(20); // waits for the servo to get there
}

//end of program

You should use [code][/code] tags when writing to the forum, as it will make it easier for people to help you. The code in between the tags will get turned into a window where the code can easily be cut/paste into the IDE.

You don't mention how your buttons are wired, but this statement makes it seem like they're wired to A0 and A2:

   if(analogPin1 == HIGH){
    //If button is turned on:

This statement, however, seems like you have the servo sensors on A0 and A2:

analogValue1 = analogRead(analogPin1);             // read the analog input                      
analogValue1 = map(analogValue1, 0, 100, 170, 0); // map the analog value to the angle of the servo

Can you show a diagram of your project so that it's clear what's connected to each pin?

Aside from this, you seem to be mixed up with your digital and analog values.

The analogRead() function is going to return a value between 0-1023. LOW is equivalent to the number 0 and HIGH is equivalent to the number 1. Although you could use map to convert the 0-1023 range to LOW-HIGH, I wouldn't recommend it and I'm not sure that the buttons are even on those pins.

The digitalWrite() function is going to accept a pin number as its first argument and LOW or HIGH as the second argument. Therefore digitalWrite(analogValue1, 100) makes little sense as analogValue1 doesn't hold your pin number, and 100 isn't a value of 0 (LOW) or 1 (HIGH).