Created
December 7, 2013 08:29
-
-
Save carlosveliz/7838638 to your computer and use it in GitHub Desktop.
LCD+Tem+Distance+Lux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Wire.h> | |
#include <LCD.h> | |
#include <LiquidCrystal_I2C.h> | |
#define I2C_ADDR 0x3F | |
#define BACKLIGHT_PIN 3 | |
#define En_pin 2 | |
#define Rw_pin 1 | |
#define Rs_pin 0 | |
#define D4_pin 4 | |
#define D5_pin 5 | |
#define D6_pin 6 | |
#define D7_pin 7 | |
#define trigPin 13 | |
#define echoPin 12 | |
#define led 11 | |
#define led2 10 | |
const int sensorPin = 0; | |
int lightLevel, high = 0, low = 1023; | |
const int temperaturePin = 1; | |
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin); | |
void setup() { | |
Serial.begin (9600); | |
pinMode(trigPin, OUTPUT); | |
pinMode(echoPin, INPUT); | |
pinMode(led, OUTPUT); | |
pinMode(led2, OUTPUT); | |
lcd.begin (20,4,LCD_5x8DOTS); | |
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); | |
} | |
void loop() { | |
lcd.setBacklight(HIGH); | |
lcd.home (); | |
// Temperatura | |
lcd.setCursor(0,3); | |
float voltage, degreesC; | |
voltage = getVoltage(temperaturePin); | |
degreesC = (voltage - 0.5) * 100.0; | |
lcd.print("Temperatura: "); | |
lcd.print(degreesC); | |
lcd.print(" C"); | |
// Luminosidad | |
lightLevel = analogRead(sensorPin); | |
lightLevel = map(lightLevel, 1023, 0, 0, 255); | |
lightLevel = constrain(lightLevel, 0, 255); | |
long duration, distance; | |
digitalWrite(trigPin, LOW); // Added this line | |
delayMicroseconds(2); // Added this line | |
digitalWrite(trigPin, HIGH); | |
// delayMicroseconds(1000); - Removed this line | |
delayMicroseconds(10); // Added this line | |
digitalWrite(trigPin, LOW); | |
duration = pulseIn(echoPin, HIGH); | |
distance = (duration/2) / 29.1; | |
if (distance < 4) { // This is where the LED On/Off happens | |
digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off | |
digitalWrite(led2,LOW); | |
} | |
else { | |
digitalWrite(led,LOW); | |
digitalWrite(led2,HIGH); | |
} | |
if (distance < 20 && distance > 5){ | |
lcd.setCursor(0,0); | |
lcd.print("Cuidado!!! "); | |
} | |
if (distance < 5 && distance > 0){ | |
lcd.setCursor(0,0); | |
lcd.print("Para ctm!!! "); | |
} | |
if (distance > 20){ | |
lcd.setCursor(0,0); | |
lcd.print(" "); | |
} | |
lcd.setCursor(0,1); | |
if (distance < 200) { | |
lcd.print("Distancia "); | |
lcd.print(distance); | |
lcd.print(" cm "); | |
} | |
else { | |
lcd.print("Fuera de Rango... "); | |
} | |
lcd.setCursor(0,2); | |
lcd.print("Luminosidad: "); | |
lcd.print(lightLevel); | |
lcd.print(" Lux"); | |
delay(50); | |
} | |
float getVoltage(int pin) | |
{ | |
return (analogRead(pin) * 0.004882814); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment