/*
Temperature and Distance Monitoring
Measures the distance and temperature every 30 secs, and turn on the red LED when the distance is less than 10 cm.
You will need to download the NewPing and Liquid Crystal libary from libary manager.
*/
// Unltra Sonice test
#include <NewPing.h> //Libary used for the ultra sonic
#include <LiquidCrystal.h> //Libary used for the LCD screen
//Define Constants
#define echoPin 4 // attach pin D4 Arduino to pin Echo of HC-SR04
#define trigPin 3 // attach pin D3 Arduino to pin Trigger of HC-SR04
#define Max_DISTANCE 400 // Set the max distance for the Ultra sonic
#define ledPin 2 // LED pin D2
NewPing sonar(trigPin,echoPin, Max_DISTANCE);
//Define Variables, Variables will change as the program is ran:
float duration, distance;
int iterations = 10;
int tempPin = 0;
int ledState = LOW; // ledState used to set the LED at the start of the program
// BS E D4 D5 D6 D7
LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // Pins for the LCD screen. These can be changed
void setup()
{
//Set Pins and serial port printout
Serial.begin(9600); // Serial Communication is starting with 9600 of baudrate speed
Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial Monitor
Serial.println("with Arduino UNO R3");
// Set the row and columns for the LCD screen
lcd.begin(16, 1); // Set column for distance display
lcd.begin(16, 2); // Set column for Temp display
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
}
// Main Program Loop this will run until program is halted
void loop()
{
//Begin Ultra Sonic Code and print to LCD screen
duration = sonar.ping_median(iterations);
distance = (duration / 2) * 0.0343;
// Displays the Output
lcd.setCursor(0, 1); // set the column on the LCD
lcd.print ("Dist: cm"); // Displays the distance on the LCD
/* Checks that the distance is greater than or equal to 400 cm,
or if the distance is less than or equal to 5 cm
*/
if (distance>= 400 || distance<= 5) {
lcd.setCursor(0, 1);
lcd.print("Out of range"); // Displays the out of range on the LCD
}
else {
lcd.setCursor(5, 1); // set the column on the LCD
lcd.print (distance); // Displays the distance on the LCD
}
// Serial monitor display this is mainly for debugging
Serial.print(" Duration: "); // Displays the distance on the Serial Monitor
Serial.print(duration); // Displays the distance on the Serial Monitor
Serial.print(" Dist: "); // Displays the distance on the Serial Monitor
Serial.print(distance); // Displays the distance on the Serial Monitor
Serial.println(" cm"); // Displays the distance on the Serial Monitor
// TEMP monitoring code
int tempReading = analogRead(tempPin);
// Temp conversion formula
double tempK = log(10000.0 * ((1024.0 / tempReading - 1)));
tempK = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * tempK * tempK )) * tempK ); // Temp Kelvin
float tempC = tempK - 273.15; // Convert Kelvin to Celcius
float tempF = (tempC * 9.0) / 5.0 + 32.0; // Convert Celcius to Fahrenheit
/* replaced
float tempVolts = tempReading * 5.0 / 1024.0;
float tempC = (tempVolts - 0.5) * 10.0;
float tempF = tempC * 9.0 / 5.0 + 32.0;
*/
// Display Temperature in C or F
lcd.setCursor(0, 0);
//lcd.print("Temp C ");
// Display Temperature in F
lcd.print("Temp F ");
lcd.setCursor(6, 0);
// Display Temperature in C
//lcd.print(tempC);
// Display Temperature in F
lcd.print(tempF); // prints temperature to LCD screen
delay(30000);
// the LED is off turn if the distance is greater than 10cm
if (distance<= 10) { // Checks the distance
ledState = HIGH; //Set LED to on if less than 10 cm
} else {
ledState = LOW; //Set LED to off
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
Comments
Please log in or sign up to comment.