For a school assignment, I will be creating something that essentially is a fan that saves power.
Here is what I am doing:
I will be creating a type of fan that uses components such as the HC-SR04 Ultrasonic Sensor to detect nearby persons and measure how far they are, depending on this the fan will spin at a certain intensity (The further they are, the faster it will spin but the closer they are the slower it will spin). It will display The intensity and the distance measured on an LCD 16x2 display, and if a person does not want to look at the LCD Display, 2 LEDs will give a visual representation of the intensity. The further the object detected is, the brighter the red LED would be, but the closer the object gets the dimmer it would become and the blue LED will start to brighten up indicating power saved/lower intensity.
Components: 2 LEDs (Red, Blue)
DC Motor
Breadboard
LCD 16x2 Display
HC-SR04 Ultrasonic Sensor
220 Ohm resistors
Arduino UNO
Jumper wires
NPN Transistor
Anyways, I have a few questions about my code. The fan would only turn on when something is detected in between 0 cm and 30 cm.
if(distance >= 0 && distance <= 30){
Serial.print("ACTIVE"); }
else{
analogWrite(9, 0);
delay(5);
analogWrite(10, 0);
delay(5);
}
(analog pin 9 and 10 are the two leds that scale)
Now what my question is that would this be correct? I'm just testing to see if a person is within the range of the sensor, and then writing "ACTIVE" to the serial monitor.
I also have another major concern, I am using many, many if statements after this initial one (around 7-8) to cause the dc motor to spin at different intensities or light up differently.
For example:
if(distance >= 21 && distance <= 23){
analogWrite(9, 190);
delay(5);
analogWrite(10, 60);
delay(5);
But my question is that ALL my end brackets are at the end of the code, not after each if statement, and are all under the initial if statement. Would this be right at all?
I still have yet to code the LCD Display and a few other components into this as well, but any answers would be greatly appreciated.
Here is the whole code if needed:
#define echoPin 7
#define trigPin 8
int analogPinled1 = 9;
int analogPinled2 = 10;
long duration;
long distance;
void setup() {
Serial.begin(9600);
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delay(2);
digitalWrite(trigPin, HIGH);
delay(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration/29.155/2;
Serial.println(distance);
if(distance >= 0 && distance <= 30){
Serial.print("ACTIVE");
}
else{
analogWrite(9, 0);
delay(5);
analogWrite(10, 0);
delay(5);
}
if(distance >= 27 && distance <= 30){
analogWrite(9, 230);
Serial.print("1");
delay(5);
analogWrite(10, 20);
delay(5);
if(distance >= 24 && distance <= 26){
analogWrite(9, 210);
delay(5);
analogWrite(10, 40);
delay(5);
if(distance >= 21 && distance <= 23){
analogWrite(9, 190);
delay(5);
analogWrite(10, 60);
delay(5);
if(distance >= 20 && distance <= 22){
analogWrite(9, 170);
delay(5);
analogWrite(10, 80);
delay(5);
if(distance >= 19 && distance <= 21){
analogWrite(9, 150);
delay(5);
analogWrite(10, 100);
delay(5);
if(distance >= 16 && distance <= 18){
analogWrite(9, 130);
delay(5);
analogWrite(10, 120);
delay(5);
if(distance >= 14 && distance <= 17){
analogWrite(9, 110);
delay(5);
analogWrite(10, 160);
delay(5);
if(distance >= 10 && distance <= 13){
analogWrite(9, 90);
delay(5);
analogWrite(10, 180);
delay(5);
if(distance >= 7 && distance <= 9){
analogWrite(9, 70);
delay(5);
analogWrite(10, 200);
delay(5);
if(distance >= 5 && distance <= 6){
analogWrite(9, 50);
delay(5);
analogWrite(10, 220);
delay(5);
}
}
}
}
}
}
}
}
}
}