Toggeable Fan

by 782497 in Teachers > 10

22 Views, 0 Favorites, 0 Comments

Toggeable Fan

Capture.PNG

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

Get Arduino and Breadboard

CaptureNOTHING.PNG

This is where you will be building the machine

Add Motor

CaptureNOBUTTON.PNG

Add Pushbutton

CaptureNORGB.PNG

Add RGB Led

Capture.PNG

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);

}

Build in Real Life

1000006539.jpg