/********* 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 Control
- Source Code compiled for: Arduino Nano
- Source Code created on: 2024-01-22 14:32:19
********* Pleasedontcode.com **********/
/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
/* if the distance to the detector is 20 cm, 1 LED */
/* lights up, if the distance is 15 cm, 2 LEDs light */
/* up, if the distance is 10 cm, 3 LEDs light up, if */
/* the distance is 5 cm, 4 LEDs light up, if the */
/* distance is 2 cm, all 5 LEDs light up and the */
/* piezo sque */
/****** END SYSTEM REQUIREMENTS *****/
/****** DEFINITION OF LIBRARIES *****/
#include
#include
/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
/* if the distance to the detector is 20 cm, 1 LED */
/* lights up, if the distance is 15 cm, 2 LEDs light */
/* up, if the distance is 10 cm, 3 LEDs light up, if */
/* the distance is 5 cm, 4 LEDs light up, if the */
/* distance is 2 cm, all 5 LEDs light up and the */
/* piezo sque */
/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void lightUpLED(int ledCount);
void turnOffLEDs();
/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t distance_HC_SR04_Echo_PIN = 5;
/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t svitlo_LED_PIN_D3 = 3;
const uint8_t distance_HC_SR04_Trigger_PIN_D4 = 4;
/***** CONSTANTS FOR DISTANCE RANGES *****/
const int DISTANCE_RANGE_1 = 20; // Distance range for 1 LED
const int DISTANCE_RANGE_2 = 15; // Distance range for 2 LEDs
const int DISTANCE_RANGE_3 = 10; // Distance range for 3 LEDs
const int DISTANCE_RANGE_4 = 5; // Distance range for 4 LEDs
const int DISTANCE_RANGE_5 = 2; // Distance range for all 5 LEDs
/****** DEFINITION OF LIBRARY CLASS INSTANCES*****/
Ultrasonic ultrasonic(distance_HC_SR04_Trigger_PIN_D4, distance_HC_SR04_Echo_PIN);
void setup(void)
{
// put your setup code here, to run once:
pinMode(distance_HC_SR04_Echo_PIN, INPUT);
pinMode(svitlo_LED_PIN_D3, OUTPUT);
pinMode(distance_HC_SR04_Trigger_PIN_D4, OUTPUT);
}
void loop(void)
{
// put your main code here, to run repeatedly:
int distance = ultrasonic.read();
if (distance >= DISTANCE_RANGE_1)
{
lightUpLED(1);
}
else if (distance >= DISTANCE_RANGE_2)
{
lightUpLED(2);
// Add code to light up the second LED
}
else if (distance >= DISTANCE_RANGE_3)
{
lightUpLED(3);
// Add code to light up the third LED
}
else if (distance >= DISTANCE_RANGE_4)
{
lightUpLED(4);
// Add code to light up the fourth LED
}
else if (distance >= DISTANCE_RANGE_5)
{
lightUpLED(5);
// Add code to light up all five LEDs
// Add code for the piezo sound
}
else
{
turnOffLEDs();
// Add code to turn off the piezo sound
}
}
void lightUpLED(int ledCount)
{
digitalWrite(svitlo_LED_PIN_D3, HIGH);
// Add code to light up the specified number of LEDs
}
void turnOffLEDs()
{
digitalWrite(svitlo_LED_PIN_D3, LOW);
// Add code to turn off all LEDs
}