Motor Fun
the amount of times you press the button should change the rotation speed of the fan. Pressing it once will cause it to rotate slowly, while pressing it three times will cause it to rotate quicly, and pressing it four times will cause it to stop.
hardware
- Arduino Board
- Motor Fun
- Driver Board
- Button Board
wire
Button Board
- SIG 2
- VCC S
- GND -
Driver Board
- + VCC
- - GND
- IN4 10
- IN3 9
- B
- C
const int buttonPin = 2;
const int ledPin = 13;
const int motorIn1 = 9;
const int motorIn2 = 10;
int stat = 0;
#define rank1 150
#define rank2 200
#define rank3 250
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the pervious reading from the input pin
// the following variables are LONG's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if th eoutput flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(motorIn1, OUTPUT);
pinMode(motorIn2, OUTPUT);
Serial.begin(9600);
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis(); // reset the debouncing timer
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH); // turn on the LED
stat = stat + 1;
if (stat >= 4) {
stat = 0;
}
} else {
digitalWrite(ledPin, LOW);
}
}
}
switch (stat) {
case 1:
clockwise(rank1);
break;
case 2:
clockwise(rank2);
break;
case 3:
clockwise(rank3);
break;
default:
clockwise(0);
}
lastButtonState = reading;
}
void clockwise(int Speed) {
analogWrite(motorIn1, 0);
analogWrite(motorIn2, Speed);
}