Advertisement
pleasedontcode

"Arduino Distance Control" rev_01

Jan 4th, 2024
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "Arduino Distance Control"
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-01-04 21:45:44
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Uses "Ultrasonic distance sensor" in pin 3 and */
  21.     /* "NeoPixel RGB Ring 24" in pin 2. It is required */
  22.     /* that as the object approaches the sensor, starting */
  23.     /* from 300cm to 40cm clockwise, the light diodes */
  24.     /* light up and as the object moves away from the */
  25.     /* sensor fro */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <Arduino.h>
  30. #include <Ultrasonic.h>
  31. #include <Adafruit_NeoPixel.h>
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36.  
  37. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  38. const uint8_t disSen_HC_SR04_Echo_PIN_D3 = 3; // Replace "-" with "_"
  39.  
  40. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  41. const uint8_t disSen_HC_SR04_Trigger_PIN_D2 = 2; // Replace "-" with "_"
  42.  
  43. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  44. Ultrasonic ultrasonic(disSen_HC_SR04_Trigger_PIN_D2, disSen_HC_SR04_Echo_PIN_D3); // Create Ultrasonic object
  45. Adafruit_NeoPixel neopixel(24, 2, NEO_GRB + NEO_KHZ800); // Create NeoPixel object
  46.  
  47. void setup(void)
  48. {
  49.   // put your setup code here, to run once:
  50.   pinMode(disSen_HC_SR04_Echo_PIN_D3, INPUT);
  51.   pinMode(disSen_HC_SR04_Trigger_PIN_D2, OUTPUT);
  52.  
  53.   neopixel.begin(); // Initialize NeoPixel
  54.   neopixel.show(); // Turn off all NeoPixel LEDs
  55. }
  56.  
  57. void loop(void)
  58. {
  59.   // put your main code here, to run repeatedly:
  60.   unsigned int distance = ultrasonic.read(); // Read distance from ultrasonic sensor
  61.  
  62.   // Check if the object is within the range
  63.   if (distance >= 40 && distance <= 300) {
  64.     int numLEDs = map(distance, 40, 300, 0, 24); // Map the number of LEDs to light up based on the distance
  65.  
  66.     // Light up the LEDs
  67.     for (int i = 0; i < numLEDs; i++) {
  68.       neopixel.setPixelColor(i, neopixel.Color(255, 255, 255)); // Set color to white for each LED
  69.     }
  70.  
  71.     // Turn off the remaining LEDs
  72.     for (int i = numLEDs; i < 24; i++) {
  73.       neopixel.setPixelColor(i, neopixel.Color(0, 0, 0)); // Set color to black for each LED
  74.     }
  75.  
  76.     neopixel.show(); // Display the updated LED colors
  77.   } else {
  78.     neopixel.clear(); // Turn off all LEDs
  79.     neopixel.show(); // Display the changes
  80.   }
  81.  
  82.   delay(100); // Delay for smoother LED transition
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement