/********* 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 Measurement"
- Source Code compiled for: Arduino Uno
- Source Code created on: 2023-12-29 21:26:37
********* Pleasedontcode.com **********/
/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
/* turn rgb led light to red and print "Out of Range" */
/* on display when sensor isn't detecting any objects */
/* and green when it detects objects with printing */
/* the distance of object from sensor in mm */
/****** END SYSTEM REQUIREMENTS *****/
/****** DEFINITION OF LIBRARIES *****/
#include
#include
#include
#include
/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t sensor_HC_SR04_Echo_PIN_D3 = 3;
/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t indicator_LEDRGB_Red_PIN_D2 = 2;
const uint8_t indicator_LEDRGB_Green_PIN_D4 = 4;
const uint8_t indicator_LEDRGB_Blue_PIN_D5 = 5;
/***** DEFINITION OF I2C PINS *****/
const uint8_t display_LCD1602I2C_I2C_PIN_SDA_A4 = A4;
const uint8_t display_LCD1602I2C_I2C_PIN_SCL_A5 = A5;
const uint8_t display_LCD1602I2C_I2C_SLAVE_ADDRESS = 39;
/****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
Ultrasonic ultrasonic(sensor_HC_SR04_Echo_PIN_D3);
LiquidCrystal_I2C lcd(display_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2); // Initialize LCD object
void setup(void)
{
pinMode(sensor_HC_SR04_Echo_PIN_D3, INPUT);
pinMode(indicator_LEDRGB_Red_PIN_D2, OUTPUT);
pinMode(indicator_LEDRGB_Green_PIN_D4, OUTPUT);
pinMode(indicator_LEDRGB_Blue_PIN_D5, OUTPUT);
lcd.begin(16, 2); // Initialize LCD
lcd.backlight(); // Turn on LCD backlight
}
void loop(void)
{
unsigned int distance = ultrasonic.read(); // Read the distance from the ultrasonic sensor
if (distance == 0) {
// No object detected
digitalWrite(indicator_LEDRGB_Red_PIN_D2, HIGH); // Turn on red LED
digitalWrite(indicator_LEDRGB_Green_PIN_D4, LOW); // Turn off green LED
lcd.clear(); // Clear the LCD display
lcd.setCursor(0, 0);
lcd.print("Out of Range");
} else {
// Object detected
digitalWrite(indicator_LEDRGB_Red_PIN_D2, LOW); // Turn off red LED
digitalWrite(indicator_LEDRGB_Green_PIN_D4, HIGH); // Turn on green LED
lcd.clear(); // Clear the LCD display
lcd.setCursor(0, 0);
lcd.print("Distance: ");
lcd.print(distance);
lcd.print(" mm");
}
delay(1000); // Wait for 1 second before taking the next reading
}