/********* Pleasedontcode.com **********
Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- Terms and Conditions:
You have a non-exclusive, revocable, worldwide, royalty-free license
for personal and commercial use. Attribution is optional; modifications
are allowed, but you're responsible for code maintenance. We're not
liable for any loss or damage. For full terms,
please visit pleasedontcode.com/termsandconditions.
- Project: "Distance-controlled Fan"
- Source Code compiled for: Arduino Uno
- Source Code created on: 2024-04-05 18:22:50
********* Pleasedontcode.com **********/
/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
/* Turn on the fan of the command is 1 and turn off */
/* the fan of the command is 0 and also turn off the */
/* util any object near the ultrasonic sensor and */
/* again turn on the fan when the object moves away */
/****** END SYSTEM REQUIREMENTS *****/
/****** DEFINITION OF LIBRARIES *****/
#include // Include the Ultrasonic library
#include // Include the DHT library
/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
/* Turn on the fan if the command is 1 and turn off */
/* the fan if the command is 0. Also, turn off the */
/* fan if any object is near the ultrasonic sensor */
/* and turn on the fan when the object moves away */
/****** END SYSTEM REQUIREMENTS *****/
/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void); // Add function prototype for updateOutputs function
/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t Temp_DHT11_DOUT_PIN_D2 = 2;
const uint8_t Ult_HC_SR04_Echo_PIN_D4 = 4;
/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t Ult_HC_SR04_Trigger_PIN_D3 = 3;
/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool Ult_HC_SR04_Trigger_PIN_D3_rawData = 0;
/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float Ult_HC_SR04_Trigger_PIN_D3_phyData = 0.0;
/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
Ultrasonic ultrasonic(Ult_HC_SR04_Trigger_PIN_D3, Ult_HC_SR04_Echo_PIN_D4); // Create an instance of the Ultrasonic class
DHT dht(Temp_DHT11_DOUT_PIN_D2, DHT11); // Create an instance of the DHT class with DHT11 sensor type
bool fanStatus = false; // Variable to store the current status of the fan
void setup(void)
{
// put your setup code here, to run once:
pinMode(Temp_DHT11_DOUT_PIN_D2, INPUT_PULLUP);
pinMode(Ult_HC_SR04_Echo_PIN_D4, INPUT);
pinMode(Ult_HC_SR04_Trigger_PIN_D3, OUTPUT);
dht.begin(); // Initialize the DHT sensor
}
void loop(void)
{
// put your main code here, to run repeatedly:
updateOutputs(); // Refresh output data
// Read the distance from the ultrasonic sensor
unsigned int distance = ultrasonic.read();
// Check if an object is detected within a certain distance
if (distance < 20 && !fanStatus) {
// Turn on the fan
Ult_HC_SR04_Trigger_PIN_D3_rawData = 1;
fanStatus = true;
} else if (distance >= 20 && fanStatus) {
// Turn off the fan
Ult_HC_SR04_Trigger_PIN_D3_rawData = 0;
fanStatus = false;
}
// Add your code here to read other sensor data and update variables
delay(1000); // Wait for 1 second before checking again
}
void updateOutputs()
{
digitalWrite(Ult_HC_SR04_Trigger_PIN_D3, Ult_HC_SR04_Trigger_PIN_D3_rawData);
}