/********* 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: Ultrasonic Indicator
- Source Code compiled for: Arduino Uno
- Source Code created on: 2024-04-17 14:09:14
********* Pleasedontcode.com **********/
/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
/* water level indicator */
/****** END SYSTEM REQUIREMENTS *****/
/****** DEFINITION OF LIBRARIES *****/
#include // https://github.com/ErickSimoes/Ultrasonic
/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void);
void checkWaterLevel(unsigned int distance);
/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t US_HC_SR04_Echo_PIN_D3 = 3;
/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t US_HC_SR04_Trigger_PIN_D2 = 2;
const uint8_t WATER_LEVEL_INDICATOR_PIN = 4;
/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool US_HC_SR04_Trigger_PIN_D2_rawData = 0;
/***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
/***** used to store data after characteristic curve transformation *****/
float US_HC_SR04_Trigger_PIN_D2_phyData = 0.0;
/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
Ultrasonic ultrasonic(US_HC_SR04_Trigger_PIN_D2, US_HC_SR04_Echo_PIN_D3);
void setup(void)
{
// put your setup code here, to run once:
pinMode(US_HC_SR04_Echo_PIN_D3, INPUT);
pinMode(US_HC_SR04_Trigger_PIN_D2, OUTPUT);
pinMode(WATER_LEVEL_INDICATOR_PIN, OUTPUT);
Serial.begin(9600); // Initialize serial communication
}
void loop(void)
{
// put your main code here, to run repeatedly:
updateOutputs(); // Refresh output data
// Read distance from ultrasonic sensor
unsigned int distance = ultrasonic.read();
// Print distance in centimeters
Serial.print("Distance in CM: ");
Serial.println(distance);
// Check water level and update the indicator
checkWaterLevel(distance);
delay(1000);
}
void updateOutputs(void)
{
digitalWrite(US_HC_SR04_Trigger_PIN_D2, US_HC_SR04_Trigger_PIN_D2_rawData);
}
void checkWaterLevel(unsigned int distance)
{
// Define the threshold distance for water level
const unsigned int WATER_LEVEL_THRESHOLD = 10;
if (distance <= WATER_LEVEL_THRESHOLD)
{
// Water level is low, turn on the indicator
digitalWrite(WATER_LEVEL_INDICATOR_PIN, HIGH);
}
else
{
// Water level is normal, turn off the indicator
digitalWrite(WATER_LEVEL_INDICATOR_PIN, LOW);
}
}