3-speed DC fan control with Uno I have built this MOSFET circuit, which is supposed to run 2 DC fans using an Arduino Uno.



I am trying to get the two DC fans running with four settings: OFF, LOW, MED and HIGH. I have been having issues with the MED option, because when its option is selected by the rotary switch (5 V pulse detected by the analog pin A2) the speed of the fan is not the constant 75% duty cycle (191) as defined on line 75 in the code below:

int motorPin1 = 3;
int motorPin2 = 9;
int DCfanSpeed = 0;
int voltageZero = 0;
int voltageOne = 1;
int voltageTwo = 2;
int voltageThree = 3;    

void setup() {
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  digitalWrite(voltageZero, INPUT_PULLUP);
  digitalWrite(voltageOne, INPUT_PULLUP);
  digitalWrite(voltageTwo, INPUT_PULLUP);
  digitalWrite(voltageThree, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  int voltageReadingZero = analogRead(voltageZero); // OFF SWITCH
  int voltsZero = voltageReadingZero / 204.6;
  if (voltsZero == 5) {
    delay(300);
    if (voltsZero == 5) DCfanSpeed = 0;
    // else DCfanSpeed = DCfanSpeed;
    while (voltsZero == 5) {
      analogWrite(motorPin1, DCfanSpeed);
      analogWrite(motorPin2, DCfanSpeed);
      voltageReadingZero = analogRead(voltageZero);
      voltsZero = voltageReadingZero / 204.6;
    }
  }
  int voltageReadingOne = analogRead(voltageOne); // LOW SWITCH
  int voltsOne = voltageReadingOne / 204.6;
  if (voltsOne == 5) {
    delay(300);
    if (voltsOne == 5) DCfanSpeed = 128;
    //else DCfanSpeed = DCfanSpeed;
    while (voltsOne == 5) {
      analogWrite(motorPin1, DCfanSpeed);
      analogWrite(motorPin2, DCfanSpeed);
      voltageReadingOne = analogRead(voltageOne);
      voltsOne = voltageReadingOne / 204.6;
    }
  }
  int voltageReadingTwo = analogRead(voltageTwo); //  MED SWITCH
  int voltsTwo = voltageReadingTwo / 204.6;
  if (voltsTwo == 5) {
    delay(300);
    if (voltsTwo == 5) DCfanSpeed = 191;
    //else DCfanSpeed = DCfanSpeed;
    while (voltsTwo == 5) {
      analogWrite(motorPin1, DCfanSpeed);
      analogWrite(motorPin2, DCfanSpeed);
      voltageReadingTwo = analogRead(voltageTwo);
      voltsTwo = voltageReadingTwo / 204.6;
    }
  }
  int voltageReadingThree = analogRead(voltageThree);  //  HIGH SWITCH
  int voltsThree = voltageReadingThree / 204.6;
  if (voltsThree == 5) {
    delay(300);
    if (voltsThree == 5) DCfanSpeed = 255;
    //else DCfanSpeed = DCfanSpeed;
    while (voltsThree == 5) {
      analogWrite(motorPin1, DCfanSpeed);
      analogWrite(motorPin2, DCfanSpeed);
      voltageReadingThree = analogRead(voltageThree);
      voltsThree = voltageReadingThree / 204.6;
    }
  }
}


Excuse the poor formatting but the basic rundown of the medium option is such that the voltage across the analog pin 2 is measured and converted into volts and if it is 5 Volts (rotary turned to the medium position) then it enters a while loop where the voltage is monitored such that if there is any change then there must be a speed setting change. This is the only option for which I get this pulsating speed on the fans instead of smooth and continuos operation.

Any ideas about how I could improve this? I have taken away one fan to see if one was faulty but this didn't work.
Solved simply by condensing code and getting rid of unnecessary variable declarations.

Corrected program:

    int motorPin1 = 3;
    int motorPin2 = 9;
    int DCfanSpeed = 0; 




    void setup() 
    { 
      pinMode(motorPin1, OUTPUT);
      pinMode(motorPin2, OUTPUT);
      digitalWrite(A0, INPUT_PULLUP);
      digitalWrite(A1, INPUT_PULLUP);
      digitalWrite(A2, INPUT_PULLUP);
      digitalWrite(A3, INPUT_PULLUP);
      Serial.begin(9600);
    } 


    void loop() 
     { 


       int voltsZero = analogRead(A0)/204.6; // OFF SWITCH
       if(voltsZero == 5)
        {
          delay(300);
          if(voltsZero == 5) DCfanSpeed = 0;

           while(voltsZero == 5) 
            {
              analogWrite(motorPin1, DCfanSpeed);
              analogWrite(motorPin2, DCfanSpeed);
              voltsZero = analogRead(A0)/204.6;

            }
         }

      int voltsOne = analogRead(A1)/204.6;    // LOW SWITCH
      if(voltsOne == 5)
        {
          delay(300);
          if(voltsOne == 5) DCfanSpeed = 128;
          while(voltsOne == 5)
           {
             analogWrite(motorPin1, DCfanSpeed);
             analogWrite(motorPin2, DCfanSpeed); 
             voltsOne = analogRead(A1)/204.6;
           }
        }

       int voltsTwo = analogRead(A2)/204.6;  //  MED SWITCH
       if(voltsTwo == 5)
        {
          delay(300);
          if(voltsTwo == 5) DCfanSpeed = 191;
          while(voltsTwo == 5)
           {
             analogWrite(motorPin1, DCfanSpeed);
             analogWrite(motorPin2, DCfanSpeed);
             voltsTwo = analogRead(A2)/204.6;

           }
        }

      int voltsThree = analogRead(A3)/204.6;  //  HIGH SWITCH
      if(voltsThree == 5)
        {
          delay(300);
          if(voltsThree == 5) DCfanSpeed = 255;
          while(voltsThree == 5) 
           {
             analogWrite(motorPin1, DCfanSpeed);
             analogWrite(motorPin2, DCfanSpeed);
             voltsThree = analogRead(A3)/204.6;
           }
         }

    }