CCCPaste

"Arduino Distance Control" rev_01

/********* 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: "Arduino Distance Control"
	- Source Code compiled for: Arduino Uno
	- Source Code created on: 2024-01-04 21:45:44

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Uses "Ultrasonic distance sensor" in pin 3 and */
	/* "NeoPixel RGB Ring 24" in pin 2. It is required */
	/* that as the object approaches the sensor, starting */
	/* from 300cm to 40cm clockwise, the light diodes */
	/* light up and as the object moves away from the */
	/* sensor fro */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 
#include 
#include 

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

/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t disSen_HC_SR04_Echo_PIN_D3 = 3; // Replace "-" with "_"

/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t disSen_HC_SR04_Trigger_PIN_D2 = 2; // Replace "-" with "_"

/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
Ultrasonic ultrasonic(disSen_HC_SR04_Trigger_PIN_D2, disSen_HC_SR04_Echo_PIN_D3); // Create Ultrasonic object
Adafruit_NeoPixel neopixel(24, 2, NEO_GRB + NEO_KHZ800); // Create NeoPixel object

void setup(void)
{
  // put your setup code here, to run once:
  pinMode(disSen_HC_SR04_Echo_PIN_D3, INPUT);
  pinMode(disSen_HC_SR04_Trigger_PIN_D2, OUTPUT);

  neopixel.begin(); // Initialize NeoPixel
  neopixel.show(); // Turn off all NeoPixel LEDs
}

void loop(void)
{
  // put your main code here, to run repeatedly:
  unsigned int distance = ultrasonic.read(); // Read distance from ultrasonic sensor

  // Check if the object is within the range
  if (distance >= 40 && distance <= 300) {
    int numLEDs = map(distance, 40, 300, 0, 24); // Map the number of LEDs to light up based on the distance

    // Light up the LEDs
    for (int i = 0; i < numLEDs; i++) {
      neopixel.setPixelColor(i, neopixel.Color(255, 255, 255)); // Set color to white for each LED
    }

    // Turn off the remaining LEDs
    for (int i = numLEDs; i < 24; i++) {
      neopixel.setPixelColor(i, neopixel.Color(0, 0, 0)); // Set color to black for each LED
    }

    neopixel.show(); // Display the updated LED colors
  } else {
    neopixel.clear(); // Turn off all LEDs
    neopixel.show(); // Display the changes
  }

  delay(100); // Delay for smoother LED transition
}