title: continuous rotation servo (arduino) responding to button press
body: <p>I am trying to make a continous rotation servo move clockwise if button on <code>pin2</code> is pressed, and counterclockwise if button on <code>pin3</code> is pressed. I want the servo to keep moving in the direction set according to the button until the button is released. This is the code I have so far (I am new to arduino):</p>
  
  <pre><code>#include &lt;Servo.h&gt;
  
  Servo myservo; // create servo object to control a servo
  
  // CONSTANTS
  
      // PINS
      const int crServo = 12; // sets pin 12 as servo
      const int buttonPinCW = 2; // sets pin 2 as button; CW =&gt; clockwise =&gt; FOCUS FAR
      const int buttonPinCC = 3; // sets pin 3 as button; CC =&gt; counterclockwise =&gt; FOCUS NEAR
      const int ledPin = 10; // sets pin 10 as LED
  
      // SERVO PROPERTIES
      const int crSpeedDefault = 1500; // 1500 is the stay still position, motor should not turn
      const int crSpeedCW = 1300; // 1300 turns the motor full speed clockwise
      const int crSpeedCC = 1700; // 1700 turns the motor full speed counter-clockwise
      const int crStepDefault = 2;
  
  // SET BUTTON STATES
      int buttonStateCW = 0; //sets button 1 as off
      int buttonStateCC = 0; // sets button 2 as off
  
  void setup()
  {
      myservo.attach(crServo); // attaches the servo on pin 12 to the servo object
      pinMode (buttonPinCW, INPUT); // sets button as input
      pinMode (buttonPinCC, INPUT); // sets button as input
      pinMode (ledPin, OUTPUT); // sets led as output
      myservo.write(crSpeedDefault); // default servo to crSpeedDefault
  }
  
  int slowFocusPull(int x){
    int result;
    result = abs(x - crSpeedDefault) / crStepDefault;
    return result;
  }
  
  void loop()
  {
      buttonStateCW = digitalRead(buttonPinCW);
      buttonStateCC = digitalRead(buttonPinCC);
      // clockwise rotation
      if (buttonStateCW == HIGH) {
          digitalWrite(ledPin, HIGH);
          myservo.write(slowFocusPull(crSpeedCW));
      // counterclockwise rotation
      } else if (buttonStateCC == HIGH) {
          digitalWrite(ledPin, HIGH);
          myservo.write(slowFocusPull(crSpeedCC));
      } else {
          digitalWrite(ledPin, LOW);
      }
  }
  </code></pre>
  
  <p>The issue lies in the function <code>slowFocusPull</code>. Basically I just want to be able to adjust the <code>speed</code> with just modifying the constant. Without this function everything works fine.</p>
  
  <hr>
  
  <p>UPDATE: final loop for reference</p>
  
  <pre><code>void loop()
  {
    buttonStateCW = digitalRead(buttonPinCW);
    buttonStateCC = digitalRead(buttonPinCC);
    // clockwise rotation
    if (buttonStateCW == HIGH) {
      digitalWrite(ledPinR, HIGH);
      float speed = crSpeedCW;
      Serial.print("CLOCKWISE-ROTATION \n");
      for (int i = 0; i &lt; t * 5; i++) {
        speed += ((float)crSpeedDefault - speed)/ 10;
        Serial.print(speed);
        Serial.print("\n");
        myservo.write((int)speed);
        delay(100);
      }
      myservo.write(crSpeedCW);
    } 
    else if (buttonStateCC == HIGH) {
        digitalWrite(ledPinG, HIGH);
        float speed = crSpeedCC;
        Serial.print("COUNTER-CLOCKWISE-ROTATION \n");
        for (int i = 0; i &lt; t * 5; i++) {
          speed += ((float)crSpeedDefault - speed) / 10;
          Serial.print(speed);
          Serial.print("\n");
          myservo.write((int)speed);
          delay(100);
        }
        myservo.write(crSpeedCC);
      } 
    else {
      myservo.write(crSpeedDefault);
      digitalWrite(ledPinR, LOW); 
      digitalWrite(ledPinG, LOW);     // turn the LED off by making the voltage LOW
    }
  }
  </code></pre>