Homemade fan - motor madness i hit the forums again with my homemade fan idea, this time i got the right servo. everything works fine until i try turn up to the 2nd speed of the servo, the buttons just go mad and the response in the serial monitor is ridiculous. I press the button once, x goes to 1 the motor starts spinnin' at its lowest speed, all fine, i hit the button one more time and "x" goes mad, in the serial monitor it shows that it goes to infinte, it just adds and adds and adds and never stops. Any suggestions? I appreciate all the help you guys gave me so far and I'd really like to finish this project

Here's the code:

const int button1Pin = 2;
const int button2Pin = 3;
const int ledPin1 = 13;
const int ledPin2 = 12;
const int ledPin3 = 11;
const int motorPin = 9;


int x = 0;
int button1State = 0;
int lastButton1State = 0;
int button2State = 0;
int lastButton2State = 0;



void setup() {
  pinMode(motorPin, OUTPUT);
  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
    int Speed1 = 50;
    int Speed2 = 100;
    int Speed3 = 200;
    int Speed4 = 0;
    button1State = digitalRead(button1Pin);
    button2State = digitalRead(button2Pin);

     if (button1State != lastButton1State)
     {
        if (button1State == HIGH) 
          {
             x = x + 1;
            Serial.println("on1");
            Serial.print("Speed:  ");
            Serial.println(x);
          }
        else 
          {
            Serial.println("off1");  
          }
     }

    if (button2State != lastButton2State)
      {    
        if (button2State == HIGH) 
          {
            x = x - 1;
            Serial.println("on2");
            Serial.print("Speed:  ");
            Serial.println(x);
          }
        else 
          {
            Serial.println("off2"); 
          }
      }



lastButton1State = button1State;
lastButton2State = button2State;

  if( x == 0 )
    {
      analogWrite(motorPin, Speed4);
    }

  else if(x == 1)
    {
      digitalWrite(ledPin1, HIGH);
      digitalWrite(ledPin2, LOW);
      digitalWrite(ledPin3, LOW);
      analogWrite(motorPin, Speed1);
    }
   else if (x == 2)
    {
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, HIGH);
      digitalWrite(ledPin3, LOW);
      analogWrite(motorPin, Speed2);
    }
   else 
    {
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, LOW);
      digitalWrite(ledPin3, HIGH);
      analogWrite(motorPin, Speed3);
    }
}
When debouncing buttons it is easiest to think of a 4 state machine.


The button is open and you will not jump to the next state until you see the button closed.
You saw the button closed.  Now you need to wait until the button stops bouncing open and close.  After about 100ms, if the button is still closed, jump to the next state.  Otherwise go back to state 1.
You are sure the button is closed.  Do the stuff you want to do when the button is pressed.  Now wait in this state until you see the button is open.
You saw the button open.  Now you need to wait until the button stops bouncing open and close.  After about 100ms, if the button is still open, jump to the first state.  Otherwise go back to state 3.


It is good practice to code this up.  However, there is an existing Arduino (de)bounce library to do this for you.