Devices & Components
Arduino Uno Rev3
Standard LCD - 16x2 White on Blue
Resistor 4.75k ohm
Ultrasonic Sensor - HC-SR04 (Generic)
Jumper wires (generic)
Buzzer
Single Turn Potentiometer- 10k ohms
DS18B20 Programmable Resolution 1-Wire Digital Thermometer
Pushbutton switch 12mm
Breadboard (generic)
Resistor 221 ohm
LEDs
Software & Tools
Arduino IDE
Project description
Code
Untitled file
c_cpp
1#include <LiquidCrystal.h>//Load Liquid Crystal Library 2#include <OneWire.h> 3#include <DallasTemperature.h> 4#define ONE_WIRE_BUS 6 // Data wire is plugged into pin 6 on the Arduino 5 6OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices 7// (not just Maxim/Dallas temperature ICs) 8DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature. 9 10LiquidCrystal LCD(12, 11, 5, 4, 3, 2); //Create Liquid Crystal Object called LCD 11 12int trigPin = 9; //Sensor Trip pin connected to Arduino pin 9 13int echoPin = 7; //Sensor Echo pin connected to Arduino pin 7 14int myCounter = 0; //declare your variable myCounter and set to 0 15 16float pingTime; //time for ping to travel from the sensor to the target and return 17float targetDistance; //Distance to Target in Centimeters 18float speedOfSound = 776.5; //Speed of sound in miles per hour 19 20 21 22void setup() { 23 24 Serial.begin(9600); 25 pinMode(trigPin, OUTPUT); 26 pinMode(echoPin, INPUT); 27 LCD.begin(16, 2); //Tell Arduino to start your 16x2 LCD 28 LCD.setCursor(0, 0); //Set LCD cursor to upper left corner, column 0, row 0 29 LCD.print("Distance:"); //Print Message on First Row 30 31 sensors.begin(); // Start up the library 32 33 pinMode(8, OUTPUT); //pin 8, 10, 13 output pins 34 pinMode(10, OUTPUT); 35 pinMode(13, OUTPUT); 36} 37void loop() { 38 39 40 digitalWrite(trigPin, LOW); //Set trigger pin low 41 delayMicroseconds(2000); //Let signal settle 42 digitalWrite(trigPin, HIGH); //Set trigPin high 43 delayMicroseconds(15); //Delay in high state 44 digitalWrite(trigPin, LOW); //ping has now been sent 45 delayMicroseconds(10); //Delay in high state 46 47 pingTime = pulseIn(echoPin, HIGH); //pingTime in microceconds 48 pingTime = pingTime / 1000000; //convert pingTime to seconds by dividing by 1000000 (microseconds in a second) 49 pingTime = pingTime / 3600; //convert pingtime to hours by dividing by 3600 (seconds in an hour) 50 targetDistance = speedOfSound * pingTime; //This will be in miles, since we declared the speed of sound as kilometers per hour; although we're going to convert it back to centimeters 51 targetDistance = targetDistance / 2; //Remember ping travels to the target and back from the target, so you must divide by 2 for actual target distance. 52 targetDistance = targetDistance * 160934.4; //Convert miles to centimeters by multipling by 160934,4 53 sensors.requestTemperatures(); // Send the command to get temperature readings 54 55 56 LCD.setCursor(10, 0); //Set the cursor to the tenth column of the first row 57 LCD.print(" "); //Print blanks to clear the row 58 LCD.setCursor(10, 0); //Set Cursor again to the tenth column of the first row 59 LCD.print((int) (targetDistance + 0.5)); //Print measured distance 60 LCD.print("cm "); //Print your units 61 LCD.setCursor(0, 1); //Set the cursor to the first column of the second row 62 LCD.print(" "); //Print blanks to clear the row 63 LCD.setCursor(0, 1); //Set Cursor again to the first column of the second row 64 65 66 if (targetDistance > 40) { 67 LCD.print("LEV LOW "); 68 LCD.print(sensors.getTempCByIndex(0)); 69 LCD.print((char)223); 70 LCD.print ("C"); 71 } 72 else if (targetDistance < 40 && targetDistance > 20) { 73 LCD.print("LEV MED "); 74 LCD.print(sensors.getTempCByIndex(0)); 75 LCD.print((char)223); 76 LCD.print ("C"); 77 78 } 79 else (targetDistance < 20); { 80 LCD.print ("LEV HIGH "); 81 LCD.print(sensors.getTempCByIndex(0)); 82 LCD.print((char)223); 83 LCD.print ("C"); 84 } 85 86 delay(2000); 87 88 89 if (targetDistance > 40) { 90 digitalWrite (13, HIGH); //Green LED ON 91 } 92 else { 93 digitalWrite(13, LOW); //Green LED OFF 94 } 95 96 if (targetDistance < 40 && targetDistance > 20) { 97 digitalWrite (8, HIGH); //Yellow LED ON 98 } 99 else { 100 digitalWrite(8, LOW); //Yellow LED OFF 101 } 102 103 if (targetDistance < 20) { 104 digitalWrite(10, HIGH); //Red LED ON 105 delay(2000); 106 digitalWrite(10, LOW); //Red LED OFF 107 } 108 109 else { 110 digitalWrite(10, LOW); //Red LED OFF 111 } 112 113 114 } 115 116 117
Downloadable files
lcd_pot_led_buzz_pb_distsens_temp_EHVdF6S1L7.fzz
lcd_pot_led_buzz_pb_distsens_temp_EHVdF6S1L7.fzz
lcd_pot_led_buzz_pb_distsens_temp_EHVdF6S1L7.fzz
lcd_pot_led_buzz_pb_distsens_temp_EHVdF6S1L7.fzz
Comments
Only logged in users can leave comments