Introduction: Toggeable Fan
This will teach you how to build a Toggleable Fan using an Arduino UNO
Supplies
Motor, Breadboard, Arduino UNO, NPN Transistor, Resistors (1k,330), Propeller
Step 1: Get Arduino and Breadboard
This is where you will be building the machine
Step 2: Add Motor
Step 3: Add Pushbutton
Step 4: Add RGB Led
Step 5: Code
const int buttonPin = 2; // Pushbutton pin
const int motor = 13; // Motor control pin
const int red = 10; // Red LED pin
const int green = 9; // Green LED pin
bool spin = false;
bool lastButtonState = LOW;
bool currentButtonState = LOW;
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 50;
void setup() {
pinMode(motor, OUTPUT);
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis(); // reset the debounce timer
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// If the button state has changed
if (reading != currentButtonState) {
currentButtonState = reading;
// Only toggle if the new state is HIGH (button pressed)
if (currentButtonState == HIGH) {
spin = !spin;
}
}
}
lastButtonState = reading;
Serial.print("Spin: ");
Serial.println(spin);
if (spin) {
digitalWrite(motor, HIGH);
setColor(255, 0); // Red
} else {
digitalWrite(motor, LOW);
setColor(0, 255); // Green
}
}
void setColor(int redValue, int greenValue) {
analogWrite(red, redValue);
analogWrite(green, greenValue);
}

