user46529: I have a code problem. I need to turn a servo clockwise or counter clockwise using 2 microswitches.
The problem is that my code is fine on startup, but when i press the button the servo continues to spin.
How do i make this so it only spins while the buttons are pressed and turn off when they are not pressed?
#include <Servo.h>

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 => clockwise => FOCUS FAR
const int buttonPinCC = 3; // sets pin 3 as button; CC => counterclockwise => FOCUS NEAR

// SERVO PROPERTIES
const int crSpeedDefault = 0; // is the stay still position, motor should not turn
const int crSpeedCW = 110; // turns the motor full speed clockwise
const int crSpeedCC = 70; // turns the motor full speed counter-clockwise

// 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 (crServo, OUTPUT);
  pinMode (buttonPinCW, INPUT); // sets button as input
  pinMode (buttonPinCC, INPUT); // sets button as input
  myservo.write(crSpeedDefault); // default servo to crSpeedDefault
}

void loop()
{
  crServo = digitalRead(crSpeedDefault);
  buttonStateCW = digitalRead(buttonPinCW);
  buttonStateCC = digitalRead(buttonPinCC);
  // clockwise rotation
  if (buttonStateCW == HIGH) {
    myservo.write(crSpeedCW);
    // counterclockwise rotation
  } 
  if (buttonStateCC == HIGH) {
    myservo.write(crSpeedCC);
  }
}


user46556: This solution is similar to @Maximilian Gerhardt's solution. So his comments on the pull-up/down resistors apply.
I assume that you only want to turn either CW or CC and not oscillate between the two.
Then you should cater for 4 button conditions:
None pressed, CW only press, CC only press, both pressed.  
We only need to worry about the "only pressed" conditions and a default state when coding.
Here is my solution:
void loop()
{
  buttonStateCW = digitalRead(buttonPinCW);
  buttonStateCC = digitalRead(buttonPinCC);

  // CW button pressed only
  if (buttonStateCW == HIGH && buttonStateCC == LOW) {
    myservo.write(crSpeedCW); // clockwise rotation
  }
  // CC button pressed only
  else if (buttonStateCW == LOW && buttonStateCC == HIGH) {
    myservo.write(crSpeedCC); // counterclockwise rotation
  }
  // Default condition
  else {
    myservo.write(crSpeedDefault);
  }
}


user30175: If you want the logic "if none of the two buttons are pressed, stop the servo immediately", then you'd express this in your loop as a if expression where you check if both buttons read LOW:
void loop()
{
  buttonStateCW = digitalRead(buttonPinCW);
  buttonStateCC = digitalRead(buttonPinCC);
  // clockwise rotation
  if (buttonStateCW == HIGH) {
    myservo.write(crSpeedCW);
    // counterclockwise rotation
  } 
  if (buttonStateCC == HIGH) {
    myservo.write(crSpeedCC);
  }

  //Are both buttons not pressed? 
  if(buttonStateCC == LOW && buttonStateCW == LOW) {
    //Stop the servo.
    myservo.write(0);
  }
}

This assumes your buttons are with pull-down resistors to GND on one side and with +5V on the other side, as detailed in the Arduino tutorial. In short this means that an unpressed button reads as 0 and a pressed on reads as 1 (HIGH). This is unusual because we usually use the internal pullup resistors via the INPUT_PULLUP pinMode parameter with a switched logic (1 = unpressed, 0 = pressed).
I've also removed the line crServo = digitalRead(crSpeedDefault); because that doesn't make sense in my opinion, as I've detailed in my comment.

46529: <p>I have a code problem. I need to turn a servo clockwise or counter clockwise using 2 microswitches.</p>

<p>The problem is that my code is fine on startup, but when i press the button the servo continues to spin.</p>

<p>How do i make this so it only spins while the buttons are pressed and turn off when they are not pressed?</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

// SERVO PROPERTIES
const int crSpeedDefault = 0; // is the stay still position, motor should not turn
const int crSpeedCW = 110; // turns the motor full speed clockwise
const int crSpeedCC = 70; // turns the motor full speed counter-clockwise

// 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 (crServo, OUTPUT);
  pinMode (buttonPinCW, INPUT); // sets button as input
  pinMode (buttonPinCC, INPUT); // sets button as input
  myservo.write(crSpeedDefault); // default servo to crSpeedDefault
}

void loop()
{
  crServo = digitalRead(crSpeedDefault);
  buttonStateCW = digitalRead(buttonPinCW);
  buttonStateCC = digitalRead(buttonPinCC);
  // clockwise rotation
  if (buttonStateCW == HIGH) {
    myservo.write(crSpeedCW);
    // counterclockwise rotation
  } 
  if (buttonStateCC == HIGH) {
    myservo.write(crSpeedCC);
  }
}
</code></pre>
 46556: <p>This solution is similar to @Maximilian Gerhardt's solution. So his comments on the pull-up/down resistors apply.</p>

<p>I assume that you only want to turn either CW or CC and not oscillate between the two.</p>

<p>Then you should cater for 4 button conditions:<br>
None pressed, CW only press, CC only press, both pressed.  </p>

<p>We only need to worry about the "only pressed" conditions and a default state when coding.</p>

<p>Here is my solution:</p>

<pre><code>void loop()
{
  buttonStateCW = digitalRead(buttonPinCW);
  buttonStateCC = digitalRead(buttonPinCC);

  // CW button pressed only
  if (buttonStateCW == HIGH &amp;&amp; buttonStateCC == LOW) {
    myservo.write(crSpeedCW); // clockwise rotation
  }
  // CC button pressed only
  else if (buttonStateCW == LOW &amp;&amp; buttonStateCC == HIGH) {
    myservo.write(crSpeedCC); // counterclockwise rotation
  }
  // Default condition
  else {
    myservo.write(crSpeedDefault);
  }
}
</code></pre>
 30175: <p>If you want the logic "if none of the two buttons are pressed, stop the servo immediately", then you'd express this in your <code>loop</code> as a <code>if</code> expression where you check if both buttons read <code>LOW</code>:</p>

<pre><code>void loop()
{
  buttonStateCW = digitalRead(buttonPinCW);
  buttonStateCC = digitalRead(buttonPinCC);
  // clockwise rotation
  if (buttonStateCW == HIGH) {
    myservo.write(crSpeedCW);
    // counterclockwise rotation
  } 
  if (buttonStateCC == HIGH) {
    myservo.write(crSpeedCC);
  }

  //Are both buttons not pressed? 
  if(buttonStateCC == LOW &amp;&amp; buttonStateCW == LOW) {
    //Stop the servo.
    myservo.write(0);
  }
}
</code></pre>

<p>This assumes your buttons are with pull-down resistors to GND on one side and with +5V on the other side, as detailed in the <a href="https://www.arduino.cc/en/Tutorial/Button" rel="nofollow noreferrer">Arduino tutorial</a>. In short this means that an unpressed button reads as <code>0</code> and a pressed on reads as <code>1</code> (<code>HIGH</code>). This is unusual because we usually use the internal pullup resistors via the <code>INPUT_PULLUP</code> <code>pinMode</code> parameter with a switched logic (<code>1</code> = unpressed, <code>0</code> = pressed).</p>

<p>I've also removed the line <code>crServo = digitalRead(crSpeedDefault);</code> because that doesn't make sense in my opinion, as I've detailed in my comment.</p>
