/********* 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 LED Control"
	- Source Code compiled for: Arduino Nano
	- Source Code created on: 2024-01-22 14:50:30

********* 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 */
/****** SYSTEM REQUIREMENT 2 *****/
	/* 500 millisecond distance update */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include <Arduino.h>
#include <Ultrasonic.h>

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t distance_HC_SR04_Echo_PIN_D5 = 5;

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t svitlo_LED_PIN_D3 = 3;
const uint8_t distance_HC_SR04_Trigger_PIN_D4 = 4;

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
Ultrasonic ultrasonic(distance_HC_SR04_Trigger_PIN_D4, distance_HC_SR04_Echo_PIN_D5);

void setup(void)
{
  // put your setup code here, to run once:
  pinMode(distance_HC_SR04_Echo_PIN_D5, 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:
  unsigned int distance = ultrasonic.read(CM); // Read the distance in centimeters

  // System Requirement 1
  if (distance >= 20)
  {
    digitalWrite(svitlo_LED_PIN_D3, LOW); // Turn off all LEDs
  }
  else if (distance >= 15)
  {
    digitalWrite(svitlo_LED_PIN_D3, HIGH); // Turn on 1 LED
  }
  else if (distance >= 10)
  {
    digitalWrite(svitlo_LED_PIN_D3, HIGH); // Turn on 2 LEDs
  }
  else if (distance >= 5)
  {
    digitalWrite(svitlo_LED_PIN_D3, HIGH); // Turn on 3 LEDs
  }
  else if (distance >= 2)
  {
    digitalWrite(svitlo_LED_PIN_D3, HIGH); // Turn on 4 LEDs
  }
  else
  {
    digitalWrite(svitlo_LED_PIN_D3, HIGH); // Turn on all 5 LEDs
  }

  // System Requirement 2
  delay(500); // Wait for 500 milliseconds before updating the distance
}
