/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-lm35-temperature-sensor
*/
#define ADC_VREF_mV 5000.0 // in millivolt
#define ADC_RESOLUTION 1024.0
#define PIN_LM35 A0
#define ECHO_PIN 2
#define TRIG_PIN 3
int inputPin = 5; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0;
int LedRed = 13;
int LedBlue = 12;
int LedGreen = 11;
void setup() {
Serial.begin(9600);
//leds
pinMode(LedRed, OUTPUT);
pinMode(LedBlue, OUTPUT);
pinMode(LedGreen, OUTPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(inputPin, INPUT);
}
float readDistanceCM() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
int duration = pulseIn(ECHO_PIN, HIGH);
return duration * 0.034 / 2;
}
void loop() {
//sensor ultrasonido
float distance = readDistanceCM();
bool isNearby = distance < 30; //centimetros
val = digitalRead(inputPin);
// get the ADC value from the temperature sensor
int adcVal = analogRead(PIN_LM35);
// convert the ADC value to voltage in millivolt
float milliVolt = adcVal * (ADC_VREF_mV / ADC_RESOLUTION);
// convert the voltage to the temperature in Celsius
float tempC = milliVolt / 10;
// convert the Celsius to Fahrenheit
float tempF = tempC * 9 / 5 + 32;
// print the temperature in the Serial Monitor:
Serial.print("Temperature: ");
Serial.print(tempC); // print the temperature in Celsius
Serial.print("°C");
Serial.print(" ~ "); // separator between Celsius and Fahrenheit
Serial.print(tempF); // print the temperature in Fahrenheit
Serial.println("°F");
Serial.print("Measured distance: ");
Serial.println(readDistanceCM());
delay(100);
if(isNearby){
digitalWrite(LedGreen, HIGH);
delay(100);
if(tempC>20 && tempC<=30){
digitalWrite(LedBlue, HIGH);
delay(100);
if(val == HIGH){
digitalWrite(LedRed, HIGH); // turn LED ON
if (pirState == LOW) {
// we have just turned on
Serial.println("Movimiento detectado!");
// We only want to print on the output change, not state
delay(100);
pirState = HIGH;
}
else {
digitalWrite(LedRed, LOW); // turn LED OFF
if (pirState == HIGH) {
// we have just turned of
Serial.println("Fin de movimiento!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}
}
}
else{
digitalWrite(LedRed, HIGH);
delay(100);
if(tempC>30){
digitalWrite(LedGreen, HIGH);
delay(100);
}
}
delay(1000);
}