Control a household fan that can change speed, direction of rotation, swivel bet
ID: 2305125 • Letter: C
Question
Control a household fan that can change speed, direction of rotation, swivel between 0 and 180 d auto shutoff. Build a circuit and write an Arduino program to do the following: The system requires 2 green LEDs, 3 push buttons (PB1, PB2 and PB3), a DC motor, a servo motor a photocell sensor and other components (20 pts) PBl is used to turn ON/OFF and change speed level of the DC motor. a. 1st press ? turn DC motor ON and at level one (50% of max speed), one LED lit up. b, 2nd press-) level two (100% of max speed) and two LEDs lit up 3d press -turn DC motor OFF and all LEDs off (system ready to receive 1st press again) c. pts) PB2 is used to change direction of the DC motor. Whenever PB2 is pressed, the DC motor direction is changed from forward to reverse and vice versa, but maintaining the speed level (10 pts) PB3 is used to make the servo motor rotate back and forth between 0 and 180 degrees t simulate the swivel of the fan. Press PB3 one time will make the servo start rotating. The servo wil (10 continuously rotate if PB3 is not pressed another time. If PB3 is pressed another time, the servo wi stop rotating right away. If PB3 is pressed another time after that, the servo will resume rotation from the previous stop position. PB3 is only active when the DC motor is ON (10 pts) The photocell is used to automatically shut off the fan. The photocell is only active wh the DC motor is ON. If the photocell detects dark condition (test it by covering the photocell with your hand) for more than 10 second, the system will turn the DC motor OFF. If the lighting condition goes back to normal, the system is ready to receive signal from push buttons.Explanation / Answer
#include <Servo.h>
const int SW_PB1 = 13;
const int S = 9;
const int SW_PB2 = 12;
const int M_A = 6;
const int SW_PB3 = 11;
const int LED1 = 10;
const int LED2 = 7;
const int M_en = 5;
const int M_B = 4;
const int PC = A5;
int PB3_val=0,PB2_val, PB1_val=0;
int time_cap=0, m_speed = 0, angle = 0, time_temp = 0,photo = 0,motor_state=0;
boolean s_dir = LOW, m_dir = HIGH, s_en = LOW;
Servo S_ctrl;
void setup() {
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(SW_PB1, INPUT);
pinMode(SW_PB2, INPUT);
pinMode(SW_PB3, INPUT);
pinMode(M_en, OUTPUT);
pinMode(M_A, OUTPUT);
pinMode(M_B, OUTPUT);
S_ctrl.attach(S);
}
void loop() {
PB1_val = digitalRead(SW_PB1);
PB2_val = digitalRead(SW_PB2);
PB3_val = digitalRead(SW_PB3);
photo = analogRead(PC);
if (PB1_val==HIGH)
{
motor_state=motor_state+1;
if (motor_state==3)
{
motor_state=0;
}
}
if (PB2_val==HIGH)
{
m_dir=!m_dir;
}
if (PB3_val==HIGH)
{
s_en = !s_en;
}
if(motor_state==1)
{
m_speed = 127;
digitalWrite(LED1,HIGH);
digitalWrite(LED2,LOW);
}
else if (motor_state==2)
{
m_speed = 255;
digitalWrite(LED1,HIGH);
digitalWrite(LED2,HIGH);
}
else
{
m_speed = 0;
digitalWrite(LED1,LOW);
digitalWrite(LED2,LOW);
}
if ((m_speed>0) && (s_en == HIGH))
{
if(s_dir==LOW)
{
angle = angle + 1;
}
else if(s_dir==HIGH)
{
angle = angle - 1;
}
if((angle==180) || (angle==0))
{
s_dir = !s_dir;
}
delay(15);
S_ctrl.write(angle);
}
if (( photo >500) and (m_speed > 0))
{
if (time_cap==0)
{
time_temp=millis();
time_cap=1;
}
if (time_temp-millis()>10000)
{
time_temp=0;
time_cap=2;
}
}
if (photo<500)
{
time_cap=0;
}
if (time_cap==2)
{
m_speed = 0;
}
analogWrite(M_en,m_speed);
digitalWrite(M_A,m_dir);
digitalWrite(M_B,!m_dir);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.