【Arduino】Simple fan with speed control and oscillating function
Imagine that you’re enjoying the breeze made from yourself in the blistering Summer. Couldn’t be better! Just with three handy gadgets, you can make a simple fan with speed control and oscillating function.
Materials:
one DC motor
one servo
one rotary encoder module
circuit diagram:
Working principle:
when we rotate the rotary encoder, we get a digital quantity. By rotating left and right, this digital quantity can be increased or decreased. We read this digital quantity and convert it into three gears of the fan. The module also has a button, when it is pressed, the oscillating function will be turned on until the next time it is pressed.
Experimental code:
/***********************************************************
File name: _26_DCMotorModule.ino
Description: The state of DC motor includes its forward, reverse,
acceleration, deceleration and stop. And you will
see the data on the serial monitor
Website: www.adeept.com
E-mail: support@adeept.com
Author: Tom
Date: 2017/03/18
***********************************************************/
#include<Servo.h>
const int DC_APin = 10; //DC motor module A pin connected to digital 11 pin
const int DC_BPin = 11; //DC motor module B pin connected to digital 10 pin
int DCmotorspeed = 60; //motor speed 0~255
const int APin= 2; //Set the digital 2 to A pin
const int BPin= 3; //Set the digital 3 to B pin
const int SPin= 4 ;//Set the digital 4 to S pin
int i=1;
int encoderVal = 0;
int angle = 90;
Servo s1;
int flag = LOW;
int flag_1 = 0;
int getRotaryEncoder(void)
{
static int oldA = HIGH; //set the oldA as HIGH
static int oldB = HIGH; //set the oldB as HIGH
int result = 0;
int newA = digitalRead(APin); //read the value of APin to newA
int newB = digitalRead(BPin); //read the value of BPin to newB
if (newA != oldA || newB != oldB)//if the value of APin or the BPin has changed
{
if (oldA == HIGH && newA == LOW)// something has changed
{
result = (oldB * 2 - 1);
}
}
oldA = newA;
oldB = newB;
return result;
}
void clockwise(int Speed) //the function to drive motor rotate clockwise
{
analogWrite(DC_APin,Speed); //set the speed of motor
analogWrite(DC_BPin,0); //stop the B pin of motor
}
void counterclockwise(int Speed) //the function to drive motor rotate counterclockwise
{
analogWrite(DC_APin,0); //stop the A pin of motor
analogWrite(DC_BPin,Speed); //set the speed of motor
}
void setup()
{
pinMode(DC_APin,OUTPUT); //initialize the A pin as output
pinMode(DC_BPin,OUTPUT); //initialize the B pin as output
pinMode(APin, INPUT);//initialize the A pin as input
pinMode(BPin, INPUT);//initialize the B pin as input
pinMode(SPin, INPUT);//initialize the S pin as input
s1.attach(A4);
s1.write(angle);
analogWrite(DC_APin,0);
analogWrite(DC_BPin,0);
Serial.begin(115200); //opens serial port, sets data rate to 9600 bps
}
int j;
void loop()
{
int change = getRotaryEncoder();
encoderVal = encoderVal - change;
if(encoderVal>=72){
encoderVal = 72;
}
if(encoderVal<=0){
encoderVal = 0;
DCmotorspeed = 0;
}
if(encoderVal>0&&encoderVal<24)
DCmotorspeed = 60;
if(encoderVal>24&&encoderVal<48)
DCmotorspeed = 80;
if(encoderVal>48&&encoderVal<72)
DCmotorspeed = 120;
if(digitalRead(SPin) == LOW)
flag = !flag;
if(i!=encoderVal){
Serial.write(encoderVal);
i = encoderVal;
}
counterclockwise(DCmotorspeed);
s1.write(angle);
j++;
if(flag &&j > 500)
{
j = 0;
if(flag_1 == 0)
{
angle +=3;
}
else
{
angle -=3;
}
if(angle > 150)
{
flag_1 = 1;
}
if(angle < 30)
{
flag_1 = 0;
}
Serial.print(angle);
}
}