Controlling a fan using a press button

Hello everyone,

I am attempting to make a small desktop fan and I seem to be having some trouble in my IF statements. The main problem I have right now is that a couple of seconds after turning the fan on with the button it will turn off again. Looking at the Serial monitor I see that at the reason for the fan shutting down is that the program somehow passes the if (buttoncounter % 2 == 0) statement. The plan is to get it working as a solid on off switch and then figure out adjustable speed for the motor, but for now it seems I am only able to have one or the other. Any ideas on what I have done wrong are appreciated, thanks!

const int buttonpin = 13;
const int FanPin = 11;
const int PotentPin = A0;
int buttonstate = 0;
int lastbuttonstate = 0;
int FanSpeed = 0;
int buttoncounter = 0;
void setup() {
 
Serial.begin(9600);
pinMode(buttonpin,INPUT);
pinMode(FanPin,OUTPUT);
 
}

void loop() {
  int Potent = map(analogRead(PotentPin),0,1023,0,255);
  int FanSpeed = Potent;
  buttonstate = digitalRead(buttonpin);

 if (buttonstate != lastbuttonstate){
   if (buttonstate == HIGH){
   analogWrite(FanPin,FanSpeed);
   buttoncounter = buttoncounter + 1;
   Serial.println("Fan On");
 } else if (buttoncounter % 2 == 0) {
   analogWrite(FanPin,0);
   Serial.println("Fan Off");
 } 
   Serial.println(buttonstate);
   Serial.println(buttoncounter);
   Serial.println(lastbuttonstate);
   lastbuttonstate = buttonstate;
   delay(50);
}
}

button_practice.ino (837 Bytes)

pinMode(buttonpin,INPUT);

I notice that you are using regular INPUT for the pinMode and you are expecting that the button will read backwards, HIGH when pressed and LOW when not pressed. Do you have an external pull-down resistor to make that work?

More common would be to wire the button so it connects to ground and reads LOW when pressed. Then you can use the internal pull-ups with pinMode INPUT_PULLUP and not need any external components.

Thanks for the quick reply. I changed the input type for the button to INPUT_PULLUP. The code still runs but I am getting the same problem with it shutting itself off again, the weird part is the time is takes to shut itself down is random it seems. Always within 10 seconds though.

Did you also change the wiring to match the new code?

Delta_G:
Did you also change the wiring to match the new code?

I believe so, the button works to turn the fan on and off it just also has the problem of shutting itself off. At first I thought it was the physical button itself at fault because it seemed to turn off if the circuit was moved at all, however, the fan has never mysteriously turned itself on.

How are you powering the fan? Can you post a quickie schematic or wiring diagram?

Delta_G:
How are you powering the fan? Can you post a quickie schematic or wiring diagram?

I will make an attempt! I dont know of any software that would have let me draw it out with the arduino and a bread board... sorry. Let me know if this helps you out at all.

Hand drawn is ok. Can you label the things there? Are you using a relay to switch the fan? How is the relay powered? Flyback diodes?