Another On-Off servo problem - simple

Hey all, I just recently started working with Arduino for an engineering class. Right now, I'm tasked with building a small desktop fan using a servo for oscillation. The breadboard has a pot to control fan speed, and a button to shut off both fan and motor. This is pretty simple and I've got it working mostly correctly. I have a small problem with my on-off button and the operation of the servo. I've spent probably five hours trying to figure out what I've done wrong here, and it's driving me crazy at this point.

When I press my button to turn off the fan, both the fan motor and the servo shut off. However, before the servo shuts off, it quickly moves to it's initial position of 0 degrees. When I press the button to turn it back on, it moves back to the position it was in before it shut off. This isn't so big of a deal really, I just want it to work the way I think it should, which would be: shut off where you are, turn on where you are.

My second post contains my sketch. Any advice given would be excellent.

#include <Servo.h>
Servo fanservo; //creates a servo object to control the servo
int buttonPin = 2; //set button pin to digital 2
int potPin = 0; //set potentiometer pin to analog 0
int motorPin = 3; //set fan motor pin to digital 3
int fanValue = 0; //variable to store potentiometer value
int pos = 0; //variable to store servo position
int buttonState = 0; //variable to store button state
int buttonPushCounter = 0; //variable to store button push count
int lastButtonState = 0; //variable to store previous button state
int servoPin = 11; //set servo pin to digital 11
void setup(){

pinMode(servoPin, OUTPUT); //declares servoPin an output
pinMode(motorPin, OUTPUT); //declares motorPin an output
fanservo.attach(11); //attach servo to digital pin 11

void loop(){

int add = 1; //variable to compare servo position

while (1) {

buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
lastButtonState = buttonState;

int newValue = analogRead(potPin)/4; //sets variable newValue to potentiometer value divided by 4
if (buttonPushCounter % 2 == 0) { //if button pu
if (newValue != fanValue) {
analogWrite(motorPin, newValue); //write newValue to fan motor
fanValue = newValue; //writes newValue to fanValue

if (pos == 180) { //if servo position is 180, add value is -1
add = -1;
if (pos == 0) { //if servo position is 0, add value is 1
add = 1;
pos += add; //pos equals pos plus add

fanservo.write(pos); //write value pos to servo, move servo to position
else {
analogWrite(motorPin, 0);
digitalWrite(servoPin, LOW);

void loop(){

 int add = 1;                               //variable to compare servo position

 while (1) {

"loop" already does this - why did you think you needed another infinite loop?

If you’re referring to “while (1){”, I think my reasoning here was that I needed the pot to adjust the fan speed during the servo loop, and adding “while” was the first method that I tried that accomplished it. Before, I had the servo looping from 0 to 180 and back, and when I would adjust the pot, the fan speed wouldn’t change until the loop restarted. I’m not sure if this is the correct way to do it, but it worked for me.

If you are have the servo instance manage the servo, you should not be manipulating that pin using digitalWrite, too. The servo will stop moving if you stop writing different values to it.