0% found this document useful (0 votes)
16 views

Code For The Park and Temperature

The document describes code for an Arduino project that uses ultrasonic sensors and temperature sensors to display distance and temperature readings on LCD screens and control LEDs and a buzzer based on distance thresholds. The code initializes the sensors and LCD screens, reads the sensor values, calculates the readings, and displays the results while controlling the output devices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Code For The Park and Temperature

The document describes code for an Arduino project that uses ultrasonic sensors and temperature sensors to display distance and temperature readings on LCD screens and control LEDs and a buzzer based on distance thresholds. The code initializes the sensors and LCD screens, reads the sensor values, calculates the readings, and displays the results while controlling the output devices.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <Wire.

h>

#include <LiquidCrystal_I2C.h>

// Define the I2C addresses of the LCDs

const int DISTANCE_LCD_ADDR = 0x27; // Change this according to your LCD's configuration

const int TEMP_LCD_ADDR = 0x23; // Change this according to your LCD's configuration

// Define the number of columns and rows for the LCDs

const int LCD_COLS = 16;

const int LCD_ROWS = 2;

// Initialize the LCDs

LiquidCrystal_I2C distanceLCD(DISTANCE_LCD_ADDR, LCD_COLS, LCD_ROWS);

LiquidCrystal_I2C tempLCD(TEMP_LCD_ADDR, LCD_COLS, LCD_ROWS);

// Define the pins for the ultrasonic sensor

const int trigPin = 2;

const int echoPin = 3;

// Define the pins for LEDs and buzzer

const int greenPin = 4;

const int bluePin = 5;

const int redPin = 6;

const int buzzerPin = 7;

// Define thresholds for distance categories

const int LONG_DISTANCE_THRESHOLD = 30;

const int MEDIUM_DISTANCE_THRESHOLD = 20;

const int SHORT_DISTANCE_THRESHOLD = 10;


void setup() {

// Begin serial communication

Serial.begin(9600);

// Set ultrasonic sensor pins as input and output

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

// Set LED and buzzer pins as output

pinMode(greenPin, OUTPUT);

pinMode(bluePin, OUTPUT);

pinMode(redPin, OUTPUT);

pinMode(buzzerPin, OUTPUT);

// Initialize the LCDs

distanceLCD.init();

distanceLCD.backlight();

tempLCD.init();

tempLCD.backlight();

// Print initial message on the LCDs

distanceLCD.setCursor(0, 0);

distanceLCD.print("Parking Assist");

tempLCD.setCursor(0, 0);

tempLCD.print("Ambient Temp");

tempLCD.setCursor(0, 1);

tempLCD.print("Temp:");
// Wait for a moment to allow the LCDs to initialize

delay(1000);

void loop() {

// Read distance from ultrasonic sensor

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

float duration = pulseIn(echoPin, HIGH);

int distance = round(duration * 0.034 / 2); // Convert duration to distance in cm and round off

// Calculate number of blocks to display

int numBlocks = map(distance, 0, 30, 16, 0);

numBlocks = constrain(numBlocks, 0, 16);

// Clear LCDs

distanceLCD.clear();

tempLCD.clear();

// Print distance and blocks on distanceLCD

distanceLCD.setCursor(0, 0);

distanceLCD.print("Distance: ");

distanceLCD.print(distance);

distanceLCD.print(" cm");

distanceLCD.setCursor(0, 1);
for (int i = 0; i < numBlocks; i++) {

distanceLCD.write(255); // Display a black box

// Read the analog value from the temperature sensor

int sensorValue = analogRead(A0);

// Convert the analog value to voltage

float voltage = sensorValue * (5.0 / 1023.0);

// Calculate resistance of the thermistor

float resistance = 10000.0 * (5.0 / voltage - 1.0);

// Calculate temperature using Steinhart-Hart equation

float steinhart;

steinhart = log(resistance / 10000.0) / 3950.0 + 1.0 / (25 + 273.15);

steinhart = 1.0 / steinhart - 273.15; // Convert to Celsius

// Print temperature on tempLCD

tempLCD.setCursor(0, 0); // Set cursor to the first line

tempLCD.print("Ambient Temp");

tempLCD.setCursor(0, 1); // Set cursor to the second line

tempLCD.print("Temp: ");

tempLCD.print(steinhart, 1); // Display temperature with 1 decimal point

tempLCD.print((char)223); // Degree symbol

tempLCD.print("C");

// Control LEDs and buzzer based on distance

if (distance > LONG_DISTANCE_THRESHOLD) {


// No LED, no buzzer

digitalWrite(greenPin, LOW);

digitalWrite(bluePin, LOW);

digitalWrite(redPin, LOW);

noTone(buzzerPin);

} else if (distance <= LONG_DISTANCE_THRESHOLD && distance > MEDIUM_DISTANCE_THRESHOLD) {

// Green LED, long beep

digitalWrite(greenPin, HIGH);

digitalWrite(bluePin, LOW);

digitalWrite(redPin, LOW);

tone(buzzerPin, 1000, 1000); // Frequency 1000Hz, duration 1000ms (1 second)

} else if (distance <= MEDIUM_DISTANCE_THRESHOLD && distance > SHORT_DISTANCE_THRESHOLD) {

// Blue LED, medium beep

digitalWrite(greenPin, LOW);

digitalWrite(bluePin, HIGH);

digitalWrite(redPin, LOW);

tone(buzzerPin, 1500, 500); // Frequency 1500Hz, duration 500ms (0.5 second)

} else {

// Red LED, short beep

digitalWrite(greenPin, LOW);

digitalWrite(bluePin, LOW);

digitalWrite(redPin, HIGH);

tone(buzzerPin, 2000, 200); // Frequency 2000Hz, duration 200ms (0.2 second)

// Wait for a short time before taking the next reading

delay(1000);

You might also like