/********* 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 Display
- Source Code NOT compiled for: Arduino Uno
- Source Code created on: 2024-07-25 04:13:25
********* Pleasedontcode.com **********/
/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
/* Arduino program calculates distance with SR04 */
/* displayed in LCD I2C in the form of numbers and */
/* bar graphs */
/****** END SYSTEM REQUIREMENTS *****/
/****** DEFINITION OF LIBRARIES *****/
#include
#include //https://github.com/marcoschwartz/LiquidCrystal_I2C
/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs(void);
float readDistance(void);
void displayDistance(float distance);
/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t sr04_HC_SR04_Echo_PIN_D3 = 3;
/***** DEFINITION OF DIGITAL OUTPUT PINS *****/
const uint8_t sr04_HC_SR04_Trigger_PIN_D2 = 2;
/***** DEFINITION OF I2C PINS *****/
const uint8_t lcd_LCD1602I2C_I2C_PIN_SDA_A4 = A4;
const uint8_t lcd_LCD1602I2C_I2C_PIN_SCL_A5 = A5;
const uint8_t lcd_LCD1602I2C_I2C_SLAVE_ADDRESS = 39;
/****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
// Initialize the LiquidCrystal_I2C object with the I2C address and dimensions
LiquidCrystal_I2C lcd(lcd_LCD1602I2C_I2C_SLAVE_ADDRESS, 20, 4); // 20 columns and 4 rows
void setup(void)
{
// Set up the pins for the ultrasonic sensor
pinMode(sr04_HC_SR04_Echo_PIN_D3, INPUT);
pinMode(sr04_HC_SR04_Trigger_PIN_D2, OUTPUT);
// Initialize the LCD
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
}
void loop(void)
{
// Read the distance from the ultrasonic sensor
float distance = readDistance();
// Update the LCD display with the distance
displayDistance(distance);
// Wait for a short period before the next reading
delay(1000); // Delay for 1 second
}
float readDistance()
{
// Clear the trigger pin
digitalWrite(sr04_HC_SR04_Trigger_PIN_D2, LOW);
delayMicroseconds(2);
// Set the trigger pin high for 10 microseconds
digitalWrite(sr04_HC_SR04_Trigger_PIN_D2, HIGH);
delayMicroseconds(10);
digitalWrite(sr04_HC_SR04_Trigger_PIN_D2, LOW);
// Read the echo pin
long duration = pulseIn(sr04_HC_SR04_Echo_PIN_D3, HIGH);
// Calculate the distance in cm (speed of sound is 34300 cm/s)
float distance = (duration * 0.0343) / 2; // Divide by 2 for the round trip
return distance;
}
void displayDistance(float distance)
{
// Clear the LCD before displaying new data
lcd.clear();
// Display the distance in cm
lcd.setCursor(0, 0);
lcd.print("Distance: ");
lcd.print(distance);
lcd.print(" cm");
// Display a simple bar graph based on the distance
lcd.setCursor(0, 1);
lcd.print("Bar: ");
int barLength = map(distance, 0, 200, 0, 16); // Map distance to bar length (max 16)
for (int i = 0; i < barLength; i++) {
lcd.write(255); // Print a filled block character
}
}
/* END CODE */